steveohara / j2mod

Enhanced Modbus library implemented in the Java programming language
Apache License 2.0
265 stars 111 forks source link

Serial port errors are swallowed. #72

Closed ghost closed 6 years ago

ghost commented 6 years ago

Running the debugger to see why j2mod doesn't work, I can see that errors about serial port are sent to AbstractModbusListener and readable via getErrors() but the listener is not accessible through ModbusSlave.

By extending ModbusSlave (I've called it ListenedSlave, sounds surreal) and making getListener() public, It's possible to get the error message, and perhaps printing it every 1 second. Raising an exception early sound like a better idea. Or perhaps I'm missing something?

By the way, nice job! j2mod is a great re-architecture of the old library.

steveohara commented 6 years ago

The whole serial slave implementation is not ideal and there are several things about it that could benefit from a refactor. Internal components like the listeners should be private, along with quite a few other aspects of the codebase. However, time is a problem so I've delayed doing too much on correcting this for the time being.

I've added a getError method to ModbusSlave which will return the listener error, if there is one.

Something to bear in mind, the getError will only return an error on slave startup i.e. the value is only set during the initialisation of the slave and the instigation of the listener thread. Values are not continuously written to it when errors occur on the listening thread once it starts.

steveohara commented 6 years ago

Can you take a look at the latest snapshot and let me know if this solves your problem please

ghost commented 6 years ago

My other project is due tomorrow. I'll get back to this one ASAP and let you know.