lua-users home
lua-l archive

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

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').

local e, p =
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)))
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)

My question about the above snippet is, shouldn't the last assert be like this instead ?
assert(err == serial.RS232_ERR_NOERROR)

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 ?