ModbusMaster does not notice changing

Topics about the Software of Revolution Pi
Post Reply
RMeissnerCC
Posts: 58
Joined: 03 Dec 2019, 10:29

ModbusMaster does not notice changing

Post by RMeissnerCC »

Dear all,
I am using the ModbusMaster virtual device with an external adapter. In the past we have made the - very rare - experience that the usb hub of the RevPi Core 3+ fails such that the usb port changes, i.e. ttyUSB0 changes to ttyUSB1 or the like. Before using the virtual device we were able to take care of this, because modbus will just yield exceptions direclty. After switching to the virtual device, however, we cannot check this problem. So far the process image flags of Modbus_Master_Status and Modbus_Action_Status_XX have shown all other errors except for that one.

Basically, if we loose connection to the modbus adapter, the modbus master will not tell me. I also see nothing in the journalctl.

Is there another flag or source of information taking care of this kind of information? Otherwise to us the virtual ModbusMaster is - I hate to say it - a piece of junk.

Best, Robert
RMeissnerCC
Posts: 58
Joined: 03 Dec 2019, 10:29

Re: ModbusMaster does not notice changing

Post by RMeissnerCC »

I checked more.

I have looked at the Modbus_Action_Status_XX and the Modbus_Master_Status flag. At the beginning, when the adapter is connected and everything runs smoothly, the flags are all 0 (=good). Then I unplug the connector, all flags stay 0. I reconnect the connector (the usb port changed now to ttyUSB2, formerly ttyUSB0), the flags are 0.
Only after resetting piControl with "piTest -x" does the Modbus_Master_Status change to 17 (hex 11). My code would now handle and resolve the issue succesfully. But since "piTest -x" takes seconds it is just too slow.

Basically, what I search for is something that tells me whether the modbus master was able to successfully connect to the adapter and read a value. That is, for some reason, only happening if I reset piControl.
User avatar
lukas
Expert
Posts: 186
Joined: 13 Feb 2017, 10:29

Re: ModbusMaster does not notice changing

Post by lukas »

Somewhat tangentially: It is possible to get a persistent device name for a specific USB serial adapter by creating a file in /lib/udev/rules.d/. The file needs to contain a udev rule which matches for the serial number of the adapter and creates a symbolic link to the actual device. Let's say you've got an adapter with serial number FT4M9NIU. You can match for this serial number to create a symbolic link /dev/ttyXYZ which always points to the respective /dev/ttyUSB* device. See here for example rules and see here for an in-depth guide.

That said, it sounds like a bug that the Modbus_Action_Status_XX and Modbus_Master_Status flags are not updated on unplug. It may be necessary for piModbusMaster / piModbusSlave to listen for udev events so that it gets notified on unplug and can update the status flags accordingly. We'll assign a team member to look into this. Thanks for the report.
RMeissnerCC
Posts: 58
Joined: 03 Dec 2019, 10:29

Re: ModbusMaster does not notice changing

Post by RMeissnerCC »

Thank you Lukas for the reply.
Sadly, we have exactly three adapters with the same chip inside. We already use udev rules, but cannot distinguish them based on device and vendor id. I will check how far I can use your solution.

Regarding the flags: thank you very much, I look forward to your feedback. If you need more information, a minimum working example or more detailled description please contact me.

Best, Robert
User avatar
lukas
Expert
Posts: 186
Joined: 13 Feb 2017, 10:29

Re: ModbusMaster does not notice changing

Post by lukas »

Usually the USB serial adapters have a separate serial number each, so that gives you a unique piece of data to match against even if the vendor and device ID are identical across multiple attached USB devices.
Post Reply