C program for reading serial port


I have written a small c program for reading data on serial port
on Beagle Board running Angstrom Linux..

I am using a usb to serial adaptor which works just fine I have
searched a lot and found lots of programs to access serial port in
but almost all the programs are giving me some problem or the other..
i have tested the serial port through minicom and it works fine both
ways i.e receiving as well as sending..

I wanted a program by which is wanted to read each byte received on
serial port
by beagle board.

i also want a non blocking read or if it is interrupt based read it
will be better
I have found interrupt based code but not working

has anybody worked on c program to read serial port on beagle board
and able to send and receive data successfully.

I’ve done quite a bit of work with the serial port in recent weeks with the beagleboard running angstrom.

I’ve got an RF data acquisition system connected to /dev/ttyS1 that I pull data from.

I used http://www.linux.com/learn/docs/ldp/745-Serial-Programming-HOWTO and http://www.easysw.com/~mike/serial/serial.html as my primary references to get started. I’d had serial code working in windows before, so have been familiar with many of the problems of programming serial ports, just not familiar with the linux API for dealing with serial ports.

I’m still not overly pleased with my code, but I’ve got stuff working reliably enough, and recovering from errors if I get dropped characters.

If you’ve got information on how to properly take over /dev/ttyS2 from getty when no one is logged into it, I’d love a pointer to that documentation.



I have encountered this problem is long back, even code used to work on linux PC, butfailed to work on beagleboard . I suggest you to use USB to serial converter for serial communication.I do not know exact problem
but the problem could be serial port configured to display boot messages of beagleboard.


What do your masks for setting up the serial port look like?



I do not remember exactly, go with USB to port converter.It should work

good luck
Srikanth Parupati

I have written many serial programs for other platforms, but not the
beagle board yet or with USB in the mix.
You have got the minicom to work.
The same dev should be used by your program.
All the same settings should be used by your program.
Have you gotten this program to work on other systems?
I would be interested in your results, as to why it did not work,
what did you discover?

May the force be with you.



An <anuj.tanksali@gmail.com> [2010-12-18 05:47:17]:

I wanted a program by which is wanted to read each byte received on serial
port by beagle board.


maybe it's not exactly what are you looking for, but there are recipes for
librs232(C library) and lua-rs232(Lua bindings) in OpenEmbeedded(Angstrom), so
building/using it on Beagle should be easy, you just need to build the
package on your host PC:

bitbake lua-rs232

then just transfer the packages(librs232 and lua-rs232) to Beagle, install them
with opkg and then you just need some small Lua test script which will
read/echo every received byte:

root@beagle$: cat test.lua

  rs232 = require("luars232")

  port = "/dev/ttyUSB0"
  read_timeout = 200
  read_len = 1

  function ts() return os.date('[%d.%m.%Y %H:%M:%S] ') end
  function log(...) io.stdout:write(ts(), string.format(...):gsub('\r\n', ''), '\n') end
  function hex(s) return string.gsub(s, ".", function(x) return string.format('%02x', string.byte(x)) end) end

  function main()
    local ret, p = rs232.open(port)
    if ret ~= rs232.RS232_ERR_NOERROR then
      log("[!] error open() : '%s' (%d)", rs232.error_tostring(ret), ret)


    log("[*] port open...")

    local i = 1
    while true do
      local ret, data, len = p:read(read_len, read_timeout)
      if len > 0 then
        log("[*] read: '%s'(%s) len: %d", data, hex(data), len)


Believe me, for prototyping it's always faster and easier to do it in some
scripting language. Anyway, the same code can be written in C with librs232
easily also. The source code of librs232[1] is available on GitHub and it's MIT

1. https://github.com/ynezz/librs232

-- ynezz