-- -- -- example configured for IIC module -- -- LUA rs232 -- LCD display has address 0xC6 -- IIC module requires command -- -- https://github.com/ynezz/librs232/blob/master/src/rs232_windows.c -- -- http://www.robot-electronics.co.uk -- http://www.robot-electronics.co.uk/acatalog/USB_I2C.html -- -- iic module: -- http://www.robot-electronics.co.uk/htm/usb_i2c_tech.htm -- -- display: -- http://www.robot-electronics.co.uk/htm/Lcd02tech.htm -- http://www.robot-electronics.co.uk/htm/Lcd03tech.htm -- -- rs232 = require("luars232") -- Linux -- port_name = "/dev/ttyS0" -- Windows port_name = "COM9" local out = io.stderr -- open port local e, p = rs232.open(port_name) if e ~= rs232.RS232_ERR_NOERROR then -- handle error out:write(string.format("can't open serial port '%s', error: '%s'\n", port_name, rs232.error_tostring(e))) return end -- set port settings assert(p:set_baud_rate(rs232.RS232_BAUD_19200) == rs232.RS232_ERR_NOERROR) assert(p:set_data_bits(rs232.RS232_DATA_8) == rs232.RS232_ERR_NOERROR) assert(p:set_parity(rs232.RS232_PARITY_NONE) == rs232.RS232_ERR_NOERROR) assert(p:set_stop_bits(rs232.RS232_STOP_1) == rs232.RS232_ERR_NOERROR) assert(p:set_flow_control(rs232.RS232_FLOW_OFF) == rs232.RS232_ERR_NOERROR) out:write(string.format("OK, port open with values '%s'\n", tostring(p))) -- write with timeout 100 msec local timeout = 100 -- in miliseconds err, len_written = p:write( string.char( 0x5a,0x01,0x0F,0x01 ), timeout) assert(e == rs232.RS232_ERR_NOERROR) -- read with timeout local read_len = 1 -- read one byte local timeout = 100 -- in miliseconds local err, data_read, size = p:read(1, timeout) assert(e == rs232.RS232_ERR_NOERROR) print( err,string.byte(data_read), 1 ) err, len_written = p:write( string.char( 0x5a,0x10,0x0F,0x01 ), timeout) assert(e == rs232.RS232_ERR_NOERROR) -- read with timeout local read_len = 1 -- read one byte local timeout = 100 -- in miliseconds local err, data_read, size = p:read(1, timeout) assert(e == rs232.RS232_ERR_NOERROR) print( err,string.byte(data_read), 1 ) -- write without timeout -- err, len_written = p:write( string.char( 0x5a,0x10,0x01,0x01 ) ) -- assert(e == rs232.RS232_ERR_NOERROR) -- write with timeout 100 msec err, len_written = p:write(string.char( 0x5a,0x10,0x00,0x01 ), timeout) assert(e == rs232.RS232_ERR_NOERROR) -- read with timeout local read_len = 1 -- read one byte local timeout = 100 -- in miliseconds local err, data_read, size = p:read(1, timeout) assert(e == rs232.RS232_ERR_NOERROR) print( err,string.byte(data_read), 1 ) -- write without timeout -- err, len_written = p:write( string.char( 0x5a,0x10,0x0F,0x01 ) ) -- assert(e == rs232.RS232_ERR_NOERROR) -- write with timeout 100 msec err, len_written = p:write(string.char( 0x5a,0x11,0x00,0x01 ), timeout) assert(e == rs232.RS232_ERR_NOERROR) -- read with timeout local read_len = 1 -- read one byte local timeout = 100 -- in miliseconds local err, data_read, size = p:read(1, timeout) assert(e == rs232.RS232_ERR_NOERROR) print( err,string.byte(data_read), 1 ) cnt = 0 while cnt < 100 do print( cnt ) -- write without timeout -- err, len_written = p:write( string.char( 0x5a,0x10,0x0F,0x01 ) ) -- assert(e == rs232.RS232_ERR_NOERROR) -- write with timeout 100 msec err, len_written = p:write(string.char( 0x5a,0x10,( cnt % 16 ) ,0x01 ), timeout) assert(e == rs232.RS232_ERR_NOERROR) -- read with timeout local read_len = 1 -- read one byte local timeout = 100 -- in miliseconds local err, data_read, size = p:read(1, timeout) assert(e == rs232.RS232_ERR_NOERROR) print( err,string.byte(data_read), 1 ) -- read with timeout -- This is using the timeout to delay the program as there is no byte to be received local read_len = 1 -- read one byte local timeout = 300 -- in miliseconds local err, data_read, size = p:read(1, timeout) assert(e == rs232.RS232_ERR_NOERROR) -- print( err,string.byte(data_read), 1 ) cnt = cnt + 1 end -- close assert(p:close() == rs232.RS232_ERR_NOERROR)