Lua and Serial Port

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Lua and Serial Port

Helen Fornazier
Hello!

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.

There is any other way to read from a serial port ?

Thank you for your help, I appreciate it.

--
Helen Fornazier
http://www.wizzilab.com/
Reply | Threaded
Open this post in threaded view
|

RE: Lua and Serial Port

Thijs Schreijer
> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]] On
> Behalf Of Helen Fornazier
> Sent: vrijdag 29 maart 2013 12:04
> To: [hidden email]
> Subject: Lua and Serial Port
>
> Hello!
>
> 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.
>
>
> There is any other way to read from a serial port ?

Have you tried the serial libraries? See https://github.com/ynezz/librs232/ 
Alternatively, I read somewhere that the upcoming release of LuaSocket also supports serial connections. See https://github.com/diegonehab/luasocket 

> Thank you for your help, I appreciate it.
>
> --
> Helen Fornazier
> http://www.wizzilab.com/

Reply | Threaded
Open this post in threaded view
|

Re: Lua and Serial Port

Philipp Janda
In reply to this post by Helen Fornazier
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




Reply | Threaded
Open this post in threaded view
|

Re: Lua and Serial Port

Marc Lepage
In reply to this post by Thijs Schreijer
I have successfully used ynezz to talk to a serial port on an Arduino, about a year ago.

Sent from my new BlackBerry Z10

From: Thijs Schreijer
Sent: Friday, March 29, 2013 7:13 AM
To: Lua mailing list
Reply To: Lua mailing list
Subject: RE: Lua and Serial Port

> -----Original Message-----

> From: [hidden email] [mailto:[hidden email]] On
> Behalf Of Helen Fornazier
> Sent: vrijdag 29 maart 2013 12:04
> To: [hidden email]
> Subject: Lua and Serial Port
>
> Hello!
>
> 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.
>
>
> There is any other way to read from a serial port ?

Have you tried the serial libraries? See https://github.com/ynezz/librs232/
Alternatively, I read somewhere that the upcoming release of LuaSocket also supports serial connections. See https://github.com/diegonehab/luasocket

> Thank you for your help, I appreciate it.
>
> --
> Helen Fornazier
> http://www.wizzilab.com/

Reply | Threaded
Open this post in threaded view
|

Re: Lua and Serial Port

Helen Fornazier
Thank you very much for all of your answers.

I tried to install ynezz/librs232, but when I execute :

> ./configure --build=i686-pc-linux-gnu --host=mips-openwrt-linux-uclibc --target=mips-openwrt-linux-uclibc --prefix=$PWD/dist

I got this message :

configure: WARNING:
*** Lua (>=5.0) headers and/or librairies couldn't be found in your system.
*** Try to install liblua, liblualib and liblua-dev with your software package manager.
*** librs232 will be built without Lua bindings library.
./configure: line 11854: 1: command not found

But I have all this packages installed. Do you know if there is something that I am missing?
I check the configure file, it checks if I have lua in my system, but as I am cross compiling I think it should just get the headers and not the compiling flags.


Now I am testing ser2net, it works really well, I tested with telnet. I just have a simple (maybe stupid) question, I am really new in Lua : In my Lua code, when I write:

> require "nixio"
> nixio.connect(localhost, 2001, "inet", "stream")
> so = nixio.connect(localhost, 2001, "inet", "stream")
> print(so)
nixio socket 4
> so.send("test")
stdin:1: bad argument #1 to 'send' (nixio.socket expected, got string)
stack traceback:
    [C]: in function 'send'
    stdin:1: in main chunk
    [C]: ?
>

the variable so is not a nixio.socket ?

Thanks!

Helen Fornazier


On Fri, Mar 29, 2013 at 2:43 PM, <[hidden email]> wrote:
I have successfully used ynezz to talk to a serial port on an Arduino, about a year ago.

Sent from my new BlackBerry Z10

From: Thijs Schreijer
Sent: Friday, March 29, 2013 7:13 AM
To: Lua mailing list
Reply To: Lua mailing list
Subject: RE: Lua and Serial Port

> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]] On
> Behalf Of Helen Fornazier
> Sent: vrijdag 29 maart 2013 12:04
> To: [hidden email]
> Subject: Lua and Serial Port
>
> Hello!
>
> 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.
>
>
> There is any other way to read from a serial port ?

Have you tried the serial libraries? See https://github.com/ynezz/librs232/
Alternatively, I read somewhere that the upcoming release of LuaSocket also supports serial connections. See https://github.com/diegonehab/luasocket

> Thank you for your help, I appreciate it.
>
> --
> Helen Fornazier
> http://www.wizzilab.com/


Reply | Threaded
Open this post in threaded view
|

Re: Lua and Serial Port

Helen Fornazier
Sorry about my last question, it is just the dot that is not a dot

so:send("test")


On Fri, Mar 29, 2013 at 6:45 PM, Helen Fornazier <[hidden email]> wrote:
Thank you very much for all of your answers.

I tried to install ynezz/librs232, but when I execute :

> ./configure --build=i686-pc-linux-gnu --host=mips-openwrt-linux-uclibc --target=mips-openwrt-linux-uclibc --prefix=$PWD/dist

I got this message :

configure: WARNING:
*** Lua (>=5.0) headers and/or librairies couldn't be found in your system.
*** Try to install liblua, liblualib and liblua-dev with your software package manager.
*** librs232 will be built without Lua bindings library.
./configure: line 11854: 1: command not found

But I have all this packages installed. Do you know if there is something that I am missing?
I check the configure file, it checks if I have lua in my system, but as I am cross compiling I think it should just get the headers and not the compiling flags.


Now I am testing ser2net, it works really well, I tested with telnet. I just have a simple (maybe stupid) question, I am really new in Lua : In my Lua code, when I write:

> require "nixio"
> nixio.connect(localhost, 2001, "inet", "stream")
> so = nixio.connect(localhost, 2001, "inet", "stream")
> print(so)
nixio socket 4
> so.send("test")
stdin:1: bad argument #1 to 'send' (nixio.socket expected, got string)
stack traceback:
    [C]: in function 'send'
    stdin:1: in main chunk
    [C]: ?
>

the variable so is not a nixio.socket ?

Thanks!

Helen Fornazier


On Fri, Mar 29, 2013 at 2:43 PM, <[hidden email]> wrote:
I have successfully used ynezz to talk to a serial port on an Arduino, about a year ago.

Sent from my new BlackBerry Z10

From: Thijs Schreijer
Sent: Friday, March 29, 2013 7:13 AM
To: Lua mailing list
Reply To: Lua mailing list
Subject: RE: Lua and Serial Port

> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]] On
> Behalf Of Helen Fornazier
> Sent: vrijdag 29 maart 2013 12:04
> To: [hidden email]
> Subject: Lua and Serial Port
>
> Hello!
>
> 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.
>
>
> There is any other way to read from a serial port ?

Have you tried the serial libraries? See https://github.com/ynezz/librs232/
Alternatively, I read somewhere that the upcoming release of LuaSocket also supports serial connections. See https://github.com/diegonehab/luasocket

> Thank you for your help, I appreciate it.
>
> --
> Helen Fornazier
> http://www.wizzilab.com/



Reply | Threaded
Open this post in threaded view
|

Re: Lua and Serial Port

Steve Litt
In reply to this post by Helen Fornazier
On Fri, 29 Mar 2013 12:04:20 +0100
Helen Fornazier <[hidden email]> wrote:

> Hello!
>
> 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.
>
> There is any other way to read from a serial port ?
>
> Thank you for your help, I appreciate it.

Hi Helen,

You didn't say what OS, but I'm betting it's Linux, because that's the
default way all Linux reads act. Here's the way I switched the keyboard
(stdin) to read a character at a time:

==========================================
function getch_unix()
        os.execute("stty cbreak </dev/tty >/dev/tty 2>&1")
        local key = io.read(1)
        os.execute("stty -cbreak </dev/tty >/dev/tty 2>&1");
        return(key);      
end
==========================================

So in your case, instead of doing the stty commands and read on file
number 1, you'll do it on the file number for your serial port. And you
might need to change /dev/tty to whatever device the serial port is.

There's a BSD style one that's a little different, so if mine doesn't
work I'll try to dig that up for you.

HTH,

SteveT

Steve Litt                *  http://www.troubleshooters.com/
Troubleshooting Training  *  Human Performance

Reply | Threaded
Open this post in threaded view
|

Re: Lua and Serial Port

Petr Štetiar
In reply to this post by Helen Fornazier
Helen Fornazier <[hidden email]> [2013-03-29 18:45:24]:

Hi,

> I tried to install ynezz/librs232, but when I execute :

> > ./configure --build=i686-pc-linux-gnu --host=mips-openwrt-linux-uclibc
> --target=mips-openwrt-linux-uclibc --prefix=$PWD/dist

You need to set include and lib paths also, something like:

CPPFLAGS='-I/opt/devel/openwrt.git/staging_dir/target-arm_v4t_uClibc-0.9.33.2_eabi/usr/include' \
LDFLAGS='-L/opt/devel/openwrt.git/staging_dir/target-arm_v4t_uClibc-0.9.33.2_eabi/usr/lib' \
./configure ...

Here's package for the OpenWRT:

        https://github.com/ynezz/openwrt/commit/d6b27eb64e3ac380cd5e6752b12f431cc8627194

> I check the configure file, it checks if I have lua in my system, but as I
> am cross compiling I think it should just get the headers and not the
> compiling flags.

Yes, exactly.

-- ynezz

Reply | Threaded
Open this post in threaded view
|

Re: Lua and Serial Port

Helen Fornazier
Hello!

Thanks for all your replies.


On Sat, Mar 30, 2013 at 7:44 PM, Petr Štetiar <[hidden email]> wrote:
Helen Fornazier <[hidden email]> [2013-03-29 18:45:24]:

Hi,

> I tried to install ynezz/librs232, but when I execute :

> > ./configure --build=i686-pc-linux-gnu --host=mips-openwrt-linux-uclibc
> --target=mips-openwrt-linux-uclibc --prefix=$PWD/dist

You need to set include and lib paths also, something like:

CPPFLAGS='-I/opt/devel/openwrt.git/staging_dir/target-arm_v4t_uClibc-0.9.33.2_eabi/usr/include' \
LDFLAGS='-L/opt/devel/openwrt.git/staging_dir/target-arm_v4t_uClibc-0.9.33.2_eabi/usr/lib' \
./configure ...

Here's package for the OpenWRT:

        https://github.com/ynezz/openwrt/commit/d6b27eb64e3ac380cd5e6752b12f431cc8627194

Thank you, I didn't had the time to retry yet but when I do I'll tell you the results.
 

> I check the configure file, it checks if I have lua in my system, but as I
> am cross compiling I think it should just get the headers and not the
> compiling flags.

Yes, exactly.

-- ynezz





> Hi Helen,
>
> You didn't say what OS, but I'm betting it's Linux, because that's the
> default way all Linux reads act. Here's the way I switched the keyboard
> (stdin) to read a character at a time:
>
> ==========================================
> function getch_unix()
>         os.execute("stty cbreak </dev/tty >/dev/tty 2>&1")
>         local key = io.read(1)
>         os.execute("stty -cbreak </dev/tty >/dev/tty 2>&1");
>         return(key);
> end
> ==========================================
>
> So in your case, instead of doing the stty commands and read on file
> number 1, you'll do it on the file number for your serial port. And you
> might need to change /dev/tty to whatever device the serial port is.

Hi Steve,

I am using Linux OpenWRT. Thanks for your reply, I'll try your solution when I have more time. For instance I am using ser2net and it is working really well.

>
> There's a BSD style one that's a little different, so if mine doesn't
> work I'll try to dig that up for you.
>
> HTH,
>
> SteveT

Helen Fornazier