Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serial communication through USB port with Python stop working after a few reconnects

I have a simple Python code that connects to an Arduino board using serial communication through the serial port.

I am using the Python library "pyserial". It is Python2.7

I create a connection basically doing something like

 ser = serial.Serial("/dev/ttyACM0", 115200)

After this I send commands to my arduino board using serial.write. I have a homemade firmware that reads the commands and moves some motors.

This code is working, it is working at the moment. Though I day I connect and disconnect several times to do stuff with my robot. I always flush everything (serial.flush). At some point it simply fails. It seems to connect, but is not executing anything. It's like if at some point the serial port is corrupted.

Once I reboot the computer, everything works fine.

Any idea how can I fix it without rebooting the computer?

Unplugging the USB cables don't work.

like image 762
Dr Sokoban Avatar asked Nov 11 '22 18:11

Dr Sokoban


1 Answers

A problem that I had was that I did not close the serial connection:

ser.close()

This lead to the point that the python process did not close and blocked any access to the serial connection. It could be that pyserial keeps the process from dying since it starts a thread in the background.

like image 62
User Avatar answered Nov 14 '22 23:11

User