void setup() {
Serial.begin(9600);
Serial.setTimeout(10);
}
void loop() {
if (Serial.available()) {
char str[10];
int amount = Serial.readBytesUntil(';', str, 10);
str[amount] = NULL;
Serial.print("Command received ");
}
}
It is connected to a linux device with usb cable. On the last one I try to use this pythone code:
import serial
import time
port = '/dev/ttyUSB0'
s = serial.Serial(port, timeout=0.15)
time.sleep(10)
s.write(b'0,-5;')
while True:
r = s.read()
print(r)
The problem is that r is always b''. How can I read from serial?
In Arduino IDE serial monitor I recieve correct messages.
Did you, on the device running Python do a loopback test to see if the port is open and to test the Python code?
Also, some sort of syncronizing is needed to properly transmit/receive serial data. For instance if 10001 was transmitted 00011 could be received. Instead use serial sentences such as <X,10001> would have a data begin marker, the X could be a data descriptor, and there is a data end marker.
I tested port by using serial monitor in arduino ide on that device.
I used this python code on other device with same architecture and os. Write is working but not read.
I use ';' in the end of a command as a marker. Isn't it enough? Still it should print something in response to a command according to sketch.
I was stupid >.<
At first I forgot ';' here s.write(b'0,-5;')
When I remembered about it - I forgot that I removed time.sleep(10) before write during many experiments which I tried.
When I put everything together like in head-post it started working.