-- -- example configured for IIC module and display -- -- 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 --[==[ -- example sending three bytes to display err, len_written = p:write( string.char( 0x55,0xC6,0,0x03,0x31,0x32,0x33 ), 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 ) ]==] txt = string.char(12).."Hello Doug\rHow are you?"..string.char(5) txtLen = txt:len() print( txt, txtLen ) err, len_written = p:write( string.char( 0x55,0xC6,0,txtLen )..txt, timeout) assert(e == rs232.RS232_ERR_NOERROR) -- read with timeout local read_len = 1 -- read one byte local timeout = 1000 -- in miliseconds -- only one byte to be read, but read 2 for a time out. local err, data_read, size = p:read(2, timeout) assert(e == rs232.RS232_ERR_NOERROR) print( err,string.byte(data_read), 1 ) txt = "\rI am fine"..string.char(5) txtLen = txt:len() print( txt, txtLen ) err, len_written = p:write( string.char( 0x55,0xC6,0,txtLen )..txt, 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 ) -- close assert(p:close() == rs232.RS232_ERR_NOERROR)