Python Serial Writes to Arduino is different from Arduino's Serial Monitor's Serial Writes -
i have python script writes string test
arduino serial port. if arduino receives test
string, should reply string ok
, led 13 should up..
problem: when arduino serial monitor used write test
serial port, arduino replies ok
expected , led #13 lights up.
however when python script writes test
same serial port, nothing happens. arduino not reply serial port , led #13 not light up.
any ideas how python script can fixed ok
response arduino , led 13 light up?
arduino sketch
int ledpin = 13; void setup() { serial.begin(9600); pinmode(ledpin, output); } void loop() { while(serial.available() == 0) { } if(serial.readstring() == "test\r\n") { serial.print("ok\r\n"); digitalwrite(ledpin, high); } readstring = ""; // clear recieved buffer delay(100); }
python script
port = 'com5' ser = serial.serial( port=port, baudrate=9600, timeout=5 ) serial.write("test\r\n") response = serial.readline() print response
port = 'com5' ser = serial.serial( port=port, baudrate=9600, timeout=5 ) # need sleep after opening port few seconds time.sleep(5) # arduino takes few seconds ready ... #also should write instance ser.write("test\r\n") # , give arduino time respond time.sleep(0.5) response = self.serial.readline() print response
if dont want wait fixed number of seconds need wait ser.cts
(clear send)
Comments
Post a Comment