@adamgronewold I agree with @ptillisch on the dtr I think a reset and some way of synchronizing the startup is usually a good idea.
There is a post in the "Introductory Tutorials" that's an interesting read and I would recommend you check it out.
I modified both your Arduino code and Python code to include synchronization.
In the Arduino code there is an extra line just in case the dtr is not having the desired effect.
In Python I modified what happens when the port is opened and I modified how the exceptions are handled. When opening the port you should always see the message "Start"
This is not complete and only intended as something for you to play around with and improve on.
ARDUINO
const int period=8;
unsigned long lastTime=0;
void setup() {
delay(2000);
Serial.begin(9600);
delay(100);
Serial.println("Start");
}
void loop() {
if ((millis() -lastTime)>period) {
lastTime=millis();
Serial.println("The message");
}
if (Serial.available() > 0){Serial.end();setup();}
}
PYTHON
import serial
import time
def main(args=None):
ser = serial.Serial()
ser.port='COM5'
ser.baudrate=9600
ser.timeout = 0.5
ser.dtr=True
ser.open()
ser.write('\n'.encode())
sync_start = False
while (True):
if ser.inWaiting():
try:
inp=ser.readline().decode('utf-8').strip()
if 'Start' in inp:
sync_start = True
print(inp,'\n')
elif sync_start:
print(inp)
except UnicodeDecodeError as e:
pass
except KeyboardInterrupt:
ser.close()
break
if __name__ == "__main__":
main()
EDIT I left the PC com port in there from when I tested the code, obviously you will need to change that