lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Sat, Sep 14, 2013 at 8:47 PM, Thijs Schreijer <thijs@thijsschreijer.nl> wrote:


> -----Original Message-----
> From: lua-l-bounces@lists.lua.org [mailto:lua-l-bounces@lists.lua.org] On
> Behalf Of Jayanth Acharya
> Sent: vrijdag 13 september 2013 21:45
> To: Lua mailing list
> Subject: code snippet check
>
> Just realized that my error checking might not have been working while I
> was troubleshooting for first lua application using the "luars232"
> library. Firstly, a snippet in question, which is pretty much lifted from
> the sample available as part of library, with just 1 variable-name changed
> ('rs232' changed to 'serial').
>
> [code]
> local e, p = serial.open(port)
> if e ~= serial.RS232_ERR_NOERR then
>     -- handle error (TODO: change to logging)
>     out:write(string.format("Can't open serial port '%s', error: '%s'\n",
>         port, serial.error_tostring(e)))
>     return
>   end
>
> assert(p:set_baud_rate(serial.RS232_BAUD_115200) ==
> serial.RS232_ERR_NOERROR
> assert(p:set_data_bits(serial.RS232_DATA_8) == serial.RS232_ERR_NOERROR)
> assert(p:set_parity(serial.RS232_PARITY_NONE) == serial.RS232_ERR_NOERROR)
> assert(p:set_stop_bits(serial.RS232_STOP_1) == serial.RS232_ERR_NOERROR)
> assert(p:set_flow_control(serial.RS232_FLOW_OFF) ==
> serial.RS232_ERR_NOERROR)
>
> -- handle error (TODO: change to logging)
> out:write(string.format("OK, port open with values '%s'\n", tostring(p)))
>
> -- read with timeout
> local read_len = 1 -- read one byte
> local timeout = 100 -- in miliseconds
> local err, data_read, size = p:read(read_len, timeout)
> assert(e == serial.RS232_ERR_NOERROR)
> [/code]
> My question about the above snippet is, shouldn't the last assert be like
> this instead ?
> [code]
> assert(err == serial.RS232_ERR_NOERROR)
> [/code]
> i.e. 'err' instead of 'e' ? Since both 'e' and 'err' are in the same
> scope, and 'e' is defined/used prior to 'err', the assert wouldn't fail,
> if port open succeeded! right ?

Seems to me you are right.
 
Thanks Thijs.
 
Btw: the assert for 'RS232_BAUD_115200' is missing a closing parenthesis

Mmm, no so sure about that. I rechecked, and the parenthesis do tally ! Perhaps an optical illusion :)