Help for coding:read from console and put into a file

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

Help for coding:read from console and put into a file

Hi! My router is pre-loaded with lua functions. I'm trying to read from console what I type and put it into a file in my router. The lua only have basic input/output functions like file:read,file:write,io:open,etc. It does not have rs232 or urat library.

I wrote the code below:
       inp = assert(io.open("/dev/ttyS1","r"))
       out = assert(io.open("rtest.txt","w"))
    while true do      
       data = inp:read("*all")
       data = string.gsub(data, "\r\n", "\n")
       out:write(data)
     end
     assert(out:close())

The code does not work. Can anybody help and advise?

Tan
Martin Guy Martin Guy
Reply | Threaded
Open this post in threaded view
|

Re: Help for coding:read from console and put into a file

On 19 July 2013 05:31, tan <[hidden email]> wrote:

> Hi! My router is pre-loaded with lua functions. I'm trying to read from
> console what I type and put it into a file in my router. The lua only have
> basic input/output functions like file:read,file:write,io:open,etc. It does
> not have rs232 or urat library.
>
> I wrote the code below:
>        inp = assert(io.open("/dev/ttyS1","r"))
>        out = assert(io.open("rtest.txt","w"))
>     while true do
>        data = inp:read("*all")
>        data = string.gsub(data, "\r\n", "\n")
>        out:write(data)
>      end
>      assert(out:close())
>
> The code does not work. Can anybody help and advise?

Hi
  Well, you should check the values of inp and out to see if they are
nil, and also the value of "data", before and after the "gsub", but
"It does not work" is too vague to know what to suggest.
  Can you say what happens exactly, and how this is different from
what you expected to happen? A transcript of what happens on your
screen might be useful.

For a more complete guide on how to ask technical questions in mailing
lists with precision, and how to get the right answer quickly, there
is a useful article here:
http://www.catb.org/esr/faqs/smart-questions.html  Its tone is rather
rude, but it does say some useful things that are not obvious. It's
also been translated into many languages.

Thanks

    M
_______________________________________________
eLua-dev mailing list
[hidden email]
https://lists.berlios.de/mailman/listinfo/elua-dev
BogdanM BogdanM
Reply | Threaded
Open this post in threaded view
|

Re: Help for coding:read from console and put into a file

In reply to this post by tan
Hi Tan,


On Fri, Jul 19, 2013 at 6:31 AM, tan <[hidden email]> wrote:
Hi! My router is pre-loaded with lua functions. I'm trying to read from
console what I type and put it into a file in my router. The lua only have
basic input/output functions like file:read,file:write,io:open,etc. It does
not have rs232 or urat library.

I wrote the code below:
       inp = assert(io.open("/dev/ttyS1","r"))
       out = assert(io.open("rtest.txt","w"))
    while true do
       data = inp:read("*all")
       data = string.gsub(data, "\r\n", "\n")
       out:write(data)
     end
     assert(out:close())

The code does not work. Can anybody help and advise?

This is the eLua list, so not the best place to ask questions about Lua running on Linux (it is Linux, right?), our libraries are quite different. However, the short answer is this: although you can open a serial port in Linux like you open any regular file, that's just an abstraction; in reality, the serial port does not behave like a file. In particular:

1. you need to set up quite a few parameters of the serial port (baud rate, number of data bits, start bits, parity + other things in Linux)
2. there is no EOF on a serial port, all you get from it is a raw stream of bytes; thus, 'read('*all')' simply can't work on a serial port, because there's no way to know when the byte stream ends. You could try to replace '*all' of a number (say 1) and check if you get any data at all, but even then I doubt it will work because of number 1 above.

In conclusion, you really need luars232 or an equivalent library if you want to read data from a serial port. Personally, I'd try to find a way to compile luars232 for my router.

Best,
Bogdan
 

Tan



--
View this message in context: http://elua-development.2368040.n2.nabble.com/Help-for-coding-read-from-console-and-put-into-a-file-tp7578094.html
Sent from the eLua Development mailing list archive at Nabble.com.
_______________________________________________
eLua-dev mailing list
[hidden email]
https://lists.berlios.de/mailman/listinfo/elua-dev


_______________________________________________
eLua-dev mailing list
[hidden email]
https://lists.berlios.de/mailman/listinfo/elua-dev