Pyserial can't read any data

I have an arduino sketch with this code:

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.

shouldn't this just be a com port, a serial interface?

I used the same port which works with serial monitor inside arduino IDE.

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.

Serial Input basics.

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.

Yes if you tested the loopback and found the loopback to be working for the Python program it should be working.

isn't that a "com" port? not a usb device?

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.

Sorry to everybody for bothering >.<

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.