[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lua and Serial Port
- From: Philipp Janda <siffiejoe@...>
- Date: Fri, 29 Mar 2013 14:38:43 +0100
Am 29.03.2013 12:04 schröbte Helen Fornazier:
Hello!
Hi!
I am new in Lua and I just start developing for OpenWRT in a Dragino
platform.
I want to develop an application that reads data from Uart and print it in
a Luci based web page.
But I am having a problem that I described here :
http://stackoverflow.com/questions/15701694/lua-io-read-sends-me-an-echo-back-when-i-read-from-serial-port-why
When I execute the code :
print("Dragino Uart Test started\n")while 1 do
serialin=io.open("/dev/ttyS0","r")
print(serialin:read()) --print the data
serialin:close()end
I read the serial port but I receive an echo too and the read() waits
until a new line character to return.
Apart from the wrong call to read() which is already mentioned in lhf's
stackoverflow answer, it seems you need to set some terminal attributes
via tcsetattr[1]. Unfortunately neither nixio nor luaposix has support
for this function. Typically you could execute the stty program to set
those attributes from the commandline, but unfortunately busybox (at
least on my openwrt) is compiled without support for stty. There is this
module[2] and as Thijs already mentioned a version of luasocket which
supports serial connections, but you would need to compile those
yourself as well.
[1]: http://linux.die.net/man/3/tcsetattr
[2]: http://lua-users.org/lists/lua-l/2011-10/msg00989.html
There is any other way to read from a serial port ?
If you don't want to touch a compiler I suggest the ser2net[3] daemon
which can be installed via opkg. It handles mappings between terminal
files and tcp sockets and forwards data between them. A quick glance at
the source shows that it does "the right thing" in its terminal
configuration, which is to disable echo and line buffering. You could
then use the default nixio[4] (which comes bundled with luci) or
luasocket modules to connect to the tcp port and read/write data ...
[3]: http://linux.die.net/man/8/ser2net
[4]: http://luci.subsignal.org/api/nixio/index.html
Thank you for your help, I appreciate it.
HTH,
Philipp