Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

How to retrieve data from serial port in linux?

Moderators: larsivi kris

Posted: 10/16/09 02:10:37 Modified: 10/19/09 02:32:23

Hi 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))
buf =
print "retrieved %d bytes, data: %s" % (len(buf), buf)

But the similar code is failed sometimes by using d

auto sio = new SerialPort("/dev/ttyUSB0");
sio.speed = 19200;
char[8] buf; // 8 bytes is enough in my case
size_t len;
while (true)
    len =;
    if (len == sio.Eof)
        Cout(Format("retrieved {} bytes, data: {}", len, buf));

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:16

I 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.