feat(modbus): implement COM port sharing and robust connection management (#211)#2200
feat(modbus): implement COM port sharing and robust connection management (#211)#2200lookmydog wants to merge 7 commits intofrangoteam:masterfrom
Conversation
…tion logic (frangoteam#211) Refactored the communication layer to allow a single COM port to be shared across multiple slave devices. Key enhancements: ConnectionManager, Reference Counting, Skip Mechanism, Queue System, Timeout Management, and Concurrency Control. Fixes frangoteam#211
|
Wow, great job, TCP could also need queue in case that serial devices are using a shared gateway. |
Regarding the TCP 'reuseserial' mode, I've implemented a handling mechanism identical to RTU, as the device-side hardware is RTU-based. This has been included in the current Pull Request. However, I must apologize for refactoring the mutex implementation you used into a queue-based mechanism. |
|
Don't need apologies, I just copied the strategy from the SocketReuse of the ModbusTcp. |
|
@lookmydog Thanks a lot for this work, the overall direction is great and the refactor is a strong step forward for Modbus connection robustness and shared COM/TCP handling. I did a brief review with Codex support and found a couple of small improvements before merge. I also want to be transparent: I could not test COM sharing deeply, because I don’t currently use RTU hardware in my setup. |
|
I've tested this on my modbus RTU hardware and it is working quite well, it has prevented the crashing I was experiencing when using a slider to send lots of commands very quickly. However I still see lots of CRC errors and data length errors, which I am getting on the latest fuxa release too. I don't know if this is just my device or hardware setup, but I don't see these errors when I use QModbus for example. |
|
Thanks for the feedback! It is interesting that QModbus works fine; this suggests the hardware itself can handle the communication, but the timing logic in FUXA might need further tuning. These errors could be caused by serial packet fragmentation (sticky packets) or improper frame segmentation. To help us analyze and reproduce the issue, could you please provide the following information?
This will help us determine if the issue is related to Node.js event loop latency or the way the serial buffer is being parsed. Thanks! |
|
@lookmydog When I change to RTUBufferedPort, then it works fine, no errors reported in logs. Perhaps that is my mistake and I should have been using that anyway? I can't actually try multiple modbus masters on the same network because although I have 2, I only have 1 power supply atm :( |
|
One thing I wanted to get an opinion on, is 2 different approaches to improving the modbus device in FUXA. You have implemented a queue, which results in minimum time to see the change in real life, however, when I spam lots of commands, the eventual delay that builds up in the queue can be several seconds long. Seperately, I also implemented a different approach, which is storing a "desired state" and "actual state". Any set tags commands changes the desired state, and this is synchronised to the device on the polling interval. Any read tags, come from the actual state which is the last read value from the actual device. This means there is a consistent delay inbetween clicking a button on the UI and a result happening, which is roughly the polling time. However it also means there cannot be a lag building up between commands sent and the result. AFAIK this is generally how it is implemented in PLCs. What are your opinions? particularly @unocelli because I guess this is a philosophical question about how you want your app to work. @lookmydog would you be interested in combining our approaches perhaps? |
Regarding RTUBufferedPort:You are absolutely right. Based on my experience with the modbus-serial library, RTUBufferedPort consistently provides better stability and data integrity for serial communications compared to the standard SerialPort. It has proven to be a much more robust choice in practical applications. Regarding State-based Control ("desired state" and "actual state"):To be honest, I am not entirely familiar with the implementation details of a "State-based" architecture. However, if this model can effectively resolve the communication congestion and latency issues we are facing in FUXA's high-frequency environments, I am more than willing to give it a try and evaluate its performance. Next Steps:I am very interested in exploring this further. Could you share more of your specific ideas or perhaps some pseudo-code? I would be happy to collaborate with you, @unocelli to see how we can combine our approaches and improve the Modbus communication module together. |
|
I tested it, and still only one slave is working properly! Could it be that it wasn't configured correctly? Can you take a screenshot of the configuration? |
|
Did you download the source code from the main branch? The fixes haven't been merged into main yet, so the issue still persists there. If that's not the case, please provide more details so I can look into it. Thanks! |
|
v1.3.0-2727
|
You will need to go to this repo https://github.com/lookmydog/FUXA/tree/fix/issue-211 and clone that and run it, or at least take the modbus implementation out and put it in your local fuxa. |
|
if I connect mbusd to my serial device and then connect both tcp devices in fuxa to it, even with "Reuse" they are also erroring. |
I'll investigate the root cause. (Since my environment doesn't use such fast polling times and baudrates, I might have missed these specific cases.)
For TCP connections, the queuing mechanism relies on the ReuseSerial option. Could you please try with that enabled? |
|
💡 Modbus RTU Testing Report & Observations 🛠 Environment & Configuration Architecture: PC -> USB-to-RS232 -> RS232/RS485 Converter -> 4x Modbus RTU AI Modules Device Setup:
Operation: Polling Read only (no write operations). 📊 Preliminary Testing Results 📝 Notes on Device Response On Polling Time: For devices that might take a bit longer to respond, slightly extending the polling interval seems to provide better consistency. Regarding Reliability: This also helps avoid the automatic "Skip" logic, ensuring that each device remains active and updated without being temporarily sidelined by the driver due to timeout. 🔍 Physical Environment Considerations Cable Length: Long RS485 runs can lead to signal attenuation or reflection. Signal Interference: Electromagnetic interference (EMI) from nearby high-voltage lines or motors. Wiring Quality: It might be worth checking the termination resistors or shielding if the timeouts persist. Just sharing these observations as I continue to evaluate the current PR. If you have more data or logs you'd like me to look at, I'm happy to discuss further! |
|
@lookmydog I have 4 tags on both id=1 and id=2. I tried the different Socket Reuse options, nothing, Reuse and ReuseSerial. Socket Reuse: nothingNo issues, polls both with no errors appearing in logs. Socket Reuse: ReuseWhen switching both onto this setting, sometimes need to turn one off for a few seconds then enable again to get errors to go away, but generally works fine. Socket Reuse: ReuseSerialWhen switching to this, start to see a lot of warnings: Fix is to disable one for a few seconds and then enable again, then it seems stable. Will test again with the RTU system when I can, overall seems ok, with potentially some buggy state on initial connection |
|
Hi everyone, This commit addresses connection stability issues and introduces new mechanisms to improve the Modbus driver's reliability and user experience. 🐛 Bug Fixes
✨ New Features
|
alext-extracellular
left a comment
There was a problem hiding this comment.
Is there a reason for commenting out all of the console outputs? I think it is quite useful to have for logging
|
I commented those out because I'm aiming to transition to FUXA's built-in logging system, as I think console.log might be a bit messy for the PR. They are still there if someone needs them for local debugging. Also, thanks for the inspiration with the progress checklist! It’s a very clear way to visualize the development stages, and I'll be using that moving forward. |
Sometimes device connection flapping during auto-recovery after power cycle tests.








Description
This PR refactors the Modbus communication layer to support multiple slave devices sharing a single COM port. It introduces a centralized management system to ensure stability and efficiency in industrial environments.
Key Mechanisms Implemented:
Impact
Related Issue
Fixes #211
✅ Checklist
state-syncmechanismconnect-skippedstatus and UI visual indicatorconsole.logwith FUXA built-in loggerconnect-skippedstate without blocking other healthy devices on the same shared port. Reconnect the device and verify it automatically recovers after the skip duration ends.ConnectionManagerhandles concurrency perfectly without cross-interference.