I am facing a peculiar issue while writing characters to Arduino using python serial communication on macOS 10.14.
The Arduino is programmed to read a string, parse it and take PWM action to run a car.
Ardiuno's serial communication channel is configured to receive the strings in the format < A, B, C, D > where ABCD are numbers which denote car direction, speed, steering direction and steering position.
The problem is, when I send a string from the serial monitor or through the Python Development environment the string is received, parsed properly and command executed successfully.
However if I write a simple program in a file write.py and execute it from the command line, nothing happens.
import serial
ser = serial.Serial('/dev/cu.usbmodem14301', 9600)
data = '<1,150,0,0>'
ser.write(data.encode())
If I run this script from the macOS terminal using the command:
python write.py
nothing happens. What am I missing here?
解决方案
A new USB connection with ser=serial.Serial('/dev/cu.usbmodem14301',9600) resets the Arduino. The data sent right after connection are lost because the Arduino boots.