-- -- 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 -- Sonar: -- http://www.robot-electronics.co.uk/htm/srf02tech.htm -- http://www.robot-electronics.co.uk/htm/srf02techI2C.htm -- http://www.robot-electronics.co.uk/htm/srf02techSer.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))) --[==[ Writing to I2C devices with a 1 byte internal address register This includes almost all I2C devices. Following the I2C_AD1 command you send the device I2C address, then the devices internal register address you want to write to and the number of bytes you're writing. The maximum number of data bytes should not exceed 64 so as not to overflow the USB-I2C's internal buffer. Primary USB-I2C command Device Address + R/W bit Device internal register Number of data bytes The data bytes Byte Type I2C_AD1 Addr+R/W Reg Byte Count Data Example 0x55 0xE0 0x00 0x01 0x51 Meaning Primary USB-I2C command SRF08 I2C address SRF08 command Reg One command byte follows Start ranging in cm ]==] cnt = 0 while cnt < 100 do local timeout = 1000 -- in miliseconds -- 81 0x51 Real Ranging Mode - Result in centimeters err, len_written = p:write( string.char( 0x55,0xE0,0x00,0x01,0x51 ), timeout) assert(e == rs232.RS232_ERR_NOERROR) -- read with timeout local read_len = 2 -- read one byte, and another for a timeout. local timeout = 100 -- in miliseconds -- 6 bytes to be read. local err, data_read, size = p:read(read_len, timeout) assert(e == rs232.RS232_ERR_NOERROR) -- print( "read: ",err, string.byte( data_read ), size ) -- Now read ranging results. -- 81 0x51 Real Ranging Mode - Result in centimeters err, len_written = p:write( string.char( 0x55,0xE1,0x00,0x06 ), timeout) assert(e == rs232.RS232_ERR_NOERROR) -- read with timeout local read_len = 6 -- read one byte, and another for a timeout. local timeout = 150 -- in miliseconds -- 6 bytes to be read. local err, data_read, size = p:read(read_len, timeout) assert(e == rs232.RS232_ERR_NOERROR) -- print( "read: ",err, size ) --[==[ Location Read Write 0 Software Revision Command Register 1 Unused (reads 0x80) N/A 2 Range High Byte N/A 3 Range Low Byte N/A 4 Autotune Minimum - High Byte N/A 5 Autotune Minimum - Low Byte N/A ]==] swVer = string.byte(data_read,1) rng = string.byte(data_read,3)*256+string.byte(data_read,4) min = string.byte(data_read,5)*256+string.byte(data_read,6) print( " result:", "swVer:", swVer , rng , "cm" , min , "cm" ) cnt = cnt + 1 end -- close assert(p:close() == rs232.RS232_ERR_NOERROR)