Hello and thanks in advance for the help. My problem is similar to others described on the forum, but these discussions are old, have since become inactive, and a reliable solution to my problem doesn't seem to exist.
I am trying to perform basic serial communication between an Arduino and a Linux machine via USB on the DUE programming port. After connecting the Arduino to my Linux machine, I check to make sure the device exists under 'lsusb' and 'ls /dev/ttyACM*'. Once connected, I run a python script to open the appropriate serial port '/dev/ttyACM1' and I attempt to read data arriving from the Arduino and print it out, line by line, to the terminal. This works perfectly after I first connect my Arduino to the Linux machine.
I then terminate my python script and run it again. Suddenly the data is no longer visible on the Linux side. The device remains connected under 'lsusb' and 'ls /dev/tty*', but attempting to print the arriving data only gives b' '. Ideally, I'd like to resolve this issue for my Arduino DUE, however I have tried other DUEs, and an UNO without resolve.
My minimal code to produce this problem is as follows:
Arduino Side
const int period=8
unsigned long lastTime=0;
void setup() {
Serial.begin(9600);
delay(100);
}
void loop() {
if ((millis() -lastTime)>period) {
lastTime=millis();
Serial.println("The message");
}
}
Basically this code is meant to simple puke data over serial at a set rate of 125 Hz, regardless of whether or not another device is there to read it.
Python/Linux Side
import serial
import time
def main(args=None):
ser = serial.Serial(port='/dev/ttyACM1', baudrate=9600, timeout=0.01)
print(ser.isOpen())
while (True):
try:
print(ser.readline().decode('utf-8'))
except UnicodeDecodeError:
print(ser.readline())
except KeyboardInterrupt:
ser.close()
time.sleep(2)
print(ser.isOpen())
time.sleep(2)
if __name__ == "__main__":
main()
Nothing too crazy here. As mentioned, the python side reads the Arduino's data perfectly after I first connect the Arduino. In the case above python will just print "The message" to terminal repeatedly. But, if I close with 'Ctrl+C' and rerun my python from terminal all I will see is blank lines (or b' ' if I remove the decode part of my readline). I have tried a variety of methods to fix the problem, including placing flush() or reset_input_buffer() in various places. ser.isOpen() always returns 1 when the port is open and 0 after I close the port, regardless of whether its the first time executing the code or the second. in_waiting() returns 0 the second execution as well. I am confident this isn't an issue with baudrate, parity, stopbits, etc., because as mentioned, it works on my first execution of the python code.
In fact, I have a Teensy 4.1 I am using basically in the exact same way and it works fine. Only substantial difference is that it is operating on '/dev/ttyACM0'. I can run and interrupt the receiving side python code for this Teensy as many times as I want without an issue. Another forum suggested there is a bug with pyserial that causes my issue, but seeing that I can get this to work on the Teensy tells me there is something else at play here.
I can also see from watching the lights on my DUE that the device is not power cycling when I interrupt the python side code, so in theory it should still be continuing to dump lines of data over serial despite the fact that the port isn't open with Python. I'd like to get to a point where I can receive the serial data without having to unplug the Arduino every time.
Thanks again for your help.