LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

simulate multiple modbus slaves on one PC

Good day,

 

I am trying to emulate multiple modbus slaves on one PC. In the visa session there is no slave / unit address, its only setting baud rate etc. So my plan was then to read VISA and fish out the first 2 bytes where the address is in and then use a case structure for the individual units. But I guess that is rather slow, by the time the decision has been made, the whole command is perhaps gone. I understand the unit address is fed into the modbus master and slave vis on initialisation and cannot be changed later. Just to make sure the daemon always responds as soon something comes along.

I managed to listen in and get one modbus command, arrives just as per standard

0101 000A 0008 1DCE 010F 0000
010F 0000 0008 0100 FE95 0101 000A 0008 1DCE

The unit addresses are something like 1 , 2, 3, 20, 21, 30, 31 so I cannot iterate straight through either. And the commands will be addressed at random, so each time something comes in, the selection as to which case this goes, needs to be made.

To make things worse its modbus 2 wire, so no clear signal what is question or response. But its for a simulator, maybe if its better to get XON/XOF control to see what is question and response perhaps there are some gadgets that can do 2 to 4 wire conversion

 

I attached 2 vis, once I inserted a VISA read, the slave does not respond. Or just responds at random.I attached 2 versions each, the older versions respond. Need some USB to RS485 gadgets to test and 2 free USB ports....

0 Kudos
Message 1 of 7
(1,488 Views)

made a view changes, still the slave just responds randomly. Even though cycle time is 1 ms and master cycle time is a few ms only as well

Download All
0 Kudos
Message 2 of 7
(1,412 Views)

I can't open your latest VIs. But in the first mail the slave 5 looks sort of okish (it's likely the one that works, but slave 6 is utter madness. You can't in parallel go and read from the serial port that is used to communicate Modbus, since that removes data that the Modbus master sent. Even if you add some loop in the master to try to inject some arbitrary data, there is NO way the two loops in the slave can decide which part to read, your upper loop will simply grab whatever is currently in the receive buffer, almost certainly stealing away data that the Modbus Read vi in the lower loop expects. Missing that data it will fail to parse the Modbus message and generate an error.

Rolf Kalbermatter
My Blog
0 Kudos
Message 3 of 7
(1,390 Views)

Hi Rolf,

 

thanks for your reply. So VISA read reads the modbus command and then deletes it?

I attached the vis in LV2015.

I changed it to state machines. Master works, addresses multiple slaves. But slaves respond randomly.

So if I was to use VISA read to extract the slave address (first 2 bytes) and then switch a case structure to the slave in question it will not work as VISA read then clears whatever it just read? Seems also it deletes the slave reply. Or gets stuck in timeout. WHatever it is, I manage to send something to a slave, as in set coils. But reading coils back it replies randomly. If I increase baudrate chances to get a reply increase, but its still randomly. I went to 38400 baudrate and 1ms loop iteration, still almost takes 1s to get some reply. For a simple read 8 coils command, so that should get through in no time.

Or is having 2 slaves on 1 VISA input just not possible and I need lets say a USB hub to have 4 or so "physical" COM ports? COM port splitting software did not work either. As in setting 2 virtual COM ports, the virtual port did not see anything

Download All
0 Kudos
Message 4 of 7
(1,356 Views)

@Steffen01 wrote:

Hi Rolf,

 

thanks for your reply. So VISA read reads the modbus command and then deletes it?

I attached the vis in LV2015.


The Modbus VIs use internally VISA Read too. A serial port is logically simply a queue for the read and the write direction. If you read the data from the receive queue at some place, it can't be read at a different place again. Otherwise you would read the same data over and over again, instead of the next one.

Rolf Kalbermatter
My Blog
0 Kudos
Message 5 of 7
(1,339 Views)

so I cannot have 2 slaves on the same port? The 2nd slave only gets data if the first one is still digesting the previous command? This is why unit 1 responds all the time and unit 2 just some time?

0 Kudos
Message 6 of 7
(1,329 Views)

ok, got a USB hub with 4 USBs, now I got physical COM ports. Using 3 USB to RS485 adapters. Then I have 2 servers to 2 VISA reads and 1 client. All on the same PC, so it can talk to itself

Steffen01_0-1698194165336.png

 

0 Kudos
Message 7 of 7
(1,300 Views)