Dr Sokoban
Dr Sokoban

Reputation: 1638

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.

Upvotes: 5

Views: 2564

Answers (1)

User
User

Reputation: 14873

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.

Upvotes: 1

Related Questions