Forum Navigation
How to retrieve data from serial port in linux?
Posted: 10/16/09 02:10:37 Modified: 10/19/09 02:32:23Hi All,
I retrieve data from serial port in linux using python.
import serial sio = serial.Serial("/dev/ttyUSB0", 19200, timeout = 1, stopbits = serial.STOPBITS_TWO) sio.write(chr(0x55) + chr(0x91) + chr(0x00) + chr(0x01)) sio.write("\n") buf = sio.read() print "retrieved %d bytes, data: %s" % (len(buf), buf) sio.close()But the similar code is failed sometimes by using d
auto sio = new SerialPort("/dev/ttyUSB0"); sio.speed = 19200; sio.write(\x55\x91\x00\x01\n) char[8] buf; // 8 bytes is enough in my case size_t len; while (true) { len = sio.read(buf); if (len == sio.Eof) { Cout(Format("retrieved {} bytes, data: {}", len, buf)); return; } } sio.close();A temperature sensor is attached to this serial port. Sometimes I can read the current temperature but sometimes a zero.
Have I done sometime thing wrong?
BTW: Is it possible to change non-blocking mode to blocking mode?
Author Message
Posted: 10/19/09 02:24:16I have solved the problem now. The problem seems to be at
if (len == sio.Eof)If I change it to
if (len <= buf.length)It works then.