[Home]Serial-Lua

HomePage | RecentChanges | Preferences | My Website home page

Serial-Lua

LUA is a good language. It is reasonably simple to understand.

It is possible to use bindings, and now there is a suitable RS232 binding

There is a windows dll for COM1 to COM9. The

http://www.robot-electronics.co.uk/htm/usb_gpio12_tech.htm

http://www.robot-electronics.co.uk/htm/usb_iss_tech.htm

LUA libuary for serial that is easy to use

This library allows RS232 from LUA scripts.

 https://github.com/ynezz/librs232/downloads

There is a windows dll for COM1 to COM9. The 1.0.1 version supports up to COM255, but needs building using VC++ 2005.

Example.

 http://www.dougrice.plus.com/dev/exampleSolarLogger.lua.txt

using robot-electronics's kit:

 http://www.robot-electronics.co.uk/htm/usb_i2c_tech.htm

 http://www.dougrice.plus.com/dev/exampleIIC.lua.txt
 http://www.dougrice.plus.com/dev/exampleIIC_atod.lua.txt
 http://www.dougrice.plus.com/dev/exampleIICdiaplay.lua.txt

 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

C++ Example for Bloodshed C++ http://www.phanderson.com/tm128/dev-cpp_tm128_1.html

http://www.robot-electronics.co.uk/htm/usb_i2c_tech.htm

Assuming buffered RS232, you send commands, wait for text to come back.

If you do a read with time out, you can wait for the text to dry up. You have to buffer the received text until you have enough to process.

It is not possible to supply a string to look for. You have to write this.

  -- flush i/P buffer
  read( number of bytes wanted, timeout )
  -- if timed out assume no new text
  write()
  wait()
  read()

  repeat reads until enough text or timeout

  process()

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


HomePage | RecentChanges | Preferences | My Website home page
This page is read-only | View other revisions
Last edited October 11, 2011 9:11 pm by Doug
Search: