Multiple Serial Slaves on one com port

79 views
Skip to first unread message

mj

unread,
Oct 27, 2009, 8:07:26 AM10/27/09
to NModbus
Hi,

Is there any way of getting more than on serial slaves answering on
one com port?

Currently it seems for me that com port and slaveID are tightly
coupled and there is no way, even by using ModbusReceivedMessage event
(just occuring when slaveID is matching with requested one) or
ComPortReceivedData event (multiple sources cannot read same port
buffer) to switch between the slave id's!!!

Are there any plans to provide this functionality in future as from a
brief view into the source code it seems not to be impossible!


Thanks for your help,
mj

Scott Alexander

unread,
Oct 27, 2009, 12:46:29 PM10/27/09
to nmodbus...@googlegroups.com
I have no plans to implement this functionality.

Scott

TimM

unread,
Oct 27, 2009, 12:56:25 PM10/27/09
to NModbus
I will need to test this later but would this not work?
I ran it with no crash but need to test with actual data.

using (SerialPort slavePort = new SerialPort("COM1"))
{
// configure serial port
slavePort.BaudRate = 9600;
slavePort.DataBits = 8;
slavePort.Parity = Parity.None;
slavePort.StopBits = StopBits.One;
slavePort.Open();

// create modbus slave
ModbusSlave slave = ModbusSerialSlave.CreateRtu(1,
slavePort);
slave.DataStore =
DataStoreFactory.CreateDefaultDataStore();

ModbusSlave slave2 = ModbusSerialSlave.CreateRtu(2,
slavePort);
slave2.DataStore =
DataStoreFactory.CreateDefaultDataStore();


slave.Listen();
slave2.Listen();
}

where your slave ids are 1 and 2.

Tim-

mj

unread,
Oct 28, 2009, 10:45:46 AM10/28/09
to NModbus
Hi TimM,

I also tried that, but on my side it was immediately crashing... But
it is good to know that it ran without any crash on your side, I will
again try that.

My next idea: I would like to mirror the serial com port to another
port, so that I can use a com port data receive event to change the
slave id's before my modbus slave is replying... Let's see if I can
find any tool which is providing me that functionality.

Thanks for your comment...
mj

TimM

unread,
Oct 28, 2009, 2:01:54 PM10/28/09
to NModbus
mj,

My apologies, my suggestion would certainly not work. Once one slave
does a Read on the serial buffer it would clear it making it
inaccessible to the other. I would think the best way to support this
would be to modify the slave functionality so that the SlaveID was
associated with a DataStore<ushort, DataStoreFactory> Dictionary
instead of associating the SlaveID with the ModbusSlave itself.

ModbusSlave slave = ModbusSerialSlave.CreateRtu(slavePort);
slave.DataStore.Add(SlaveID, DataStoreFactory.CreateDefaultDataStore
());

And handle the incoming/outgoing messages accordingly.

Tim-
> > > mj- Hide quoted text -
>
> - Show quoted text -

mj

unread,
Oct 30, 2009, 4:22:06 AM10/30/09
to NModbus
Hi Tim,

yes, in my initial statement I mentioned the problem with the serial
buffer, I was so surprised that you told me that it is working that I
thought that there would be some internal resource management :-)))

But, in the meantime I have also almost same solution found as you
described above.

Modifications:
- The listen() function is always reading the request and in case of
correct slaveID the response is getting created independent of the
slaveID itself, so by removing the if case where the slaveID is
checked, the slave(s) always response correctly :-)
- But I also added a slaveID array, so that my slave is just
responding to configured slave id's
- Next step planned was actually like you described a DataStore array
for all the configured slave id's, but I found another solution. By
using the ModbusSlaveRequestReceived() event I can switch dependent on
the slave id between defined datastores. So the datastores are
currently not part of the slave, but it is working properly so far.
But, maybe I will also add the datastore array to have it completed.


Thanks Tim for your contribution, any more ideas :-)
Regards,
mj
Reply all
Reply to author
Forward
0 new messages