Search code examples
javaserial-portmodbus

what's the better than reading data from modbus serial port , close or keep a connection open


Hello I'm use modbus device and use java with j2mod library , my aim say i need read multiple registry every 10 s from serial port about modbus device ,then aggregate the data then uplode to database , in this case After each time when the code reads the data(readMultipleRegisters) via ModbusSerialMaster .Do I close the connection every reading end and reopen it at the beginning of a new reading


Solution

  • Unless you have a good reason to close the port (the only thing I can think of is if you need to share it for some other purpose but I doubt you will be doing that since you say you want to send register queries in 10-second intervals), just keep it open.

    On the con side, depending on your setup opening and closing the port might eat up some of the time in between readings. If you don't need to share the port I cannot think of any pros you could gain by closing the port after reading.

    To be honest, I cannot think of any particular factors related to Modbus, so the guidance and rules on any conventional serial communication apply. If you search SO or most other places you'll find the most common advice is to keep the port open.