0

Working on raspberry with Arduino connected over USB. Communicating over python with minimalmodbus library Trying to make reconnection when Arduino was unplugged for a while. I tried to repeat the connection code again on error, didn't helped. Is there any function to reconnect on the serial connection lost?

minimalmodbus.baudrate = 9600
instr = minimalmodbus.Instrument('COM5', 1)
instr.serial.baudrate=9600
instr.debug=False

1 Answers1

1

You may be facing this problem because you are not closing the COM port. Therefore, you can try first closing that by using below command:

instrument.close_port_after_each_call= True

Your code should look like this:

try:
    minimalmodbus.baudrate = 9600
    instr = minimalmodbus.Instrument('COM5', 1)
    instr.serial.baudrate=9600
    instr.debug=False
    instr.close_port_after_each_call= True 
except:
    print('please check your connection')

More Here

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83