Hello,
I am just starting to build an app on AAF. My first step is to write a stream of data out the serial port while waiting for any data to be received. If any data is received on the serial port I want to break and handle it. I imagine this should be done with emitters and events but I cannot find any documentation on how to implement these only that they exist.
My source is pretty simple right now, open the serial port, start a loop that writes to the serial(dummy data for now), and then ideally wait for any data to be received on the same port. You can find the code below.
Thanks!
local sched = require 'sched'
-- Load the module and store it inside a local variable
local lib = require 'my_module'
--- code from sample serial open, write and read
local devicetree = require 'devicetree'
local system = require 'system'
local serial = require "serial" -- Serial lib
local os = require "os" -- Operating system lib
local serdev
-- Variable path to be used
local RESERVE_SER0 = "system.aleos.reserve.ser0"
--- Check if the serial port has been reserved for our use
-- if not, reserve it and reboot to gain control
function reserve_serial_port (asset)
local serial_available = assert (devicetree.get(RESERVE_SER0))
if serial_available ~= 1 then
print ("WARNING","serial port not available, reserving it now...")
local result = assert (devicetree.set(RESERVE_SER0, 1))
print ("WARNING","Serial port Reserved ok, rebooting now...")
system.reboot()
sched.wait(30) -- stop here until the reset occurs
end
print ("Serial Port has been reserved for our use.")
end
--------------------------------------------------------------------------------
-- open and configure the serial port
function open_serial_port (asset)
local portname = "/dev/ttyS0"
local serial_config =
{
baudRate=19200,
flowControl = 'none',
numDataBits=8,
parity='none',
numStopBits=1
}
local serialdev = assert (serial.open(portname, serial_config), "Serial Port OPEN Failed!")
return serialdev
end
--------------------------------------------------------------------------------
-- Write to the serial port
function write_to_serial_port (sdevice, sdata)
local write_cnt = assert (sdevice:write (sdata), "Serial Port WRITE Failed!")
assert (sdevice:flush()) --- Added by ME in testing ...
print ('Number of characters written to serial port: ', write_cnt)
end
--------------------------------------------------------------------------------
-- Read a line from the serial port
function read_serial_port (sdevice)
-- assert (sdevice.settimeout (5,b)) -- set a timeout
local read_data, Error = sdevice:read(1) -- read a line
if Error then
print ("Serial Port Read Failed because: ", Error)
end
return read_data
end
--------------------------------------------------------------------------------
-- Serial Sample - Examples of the serial port API usage
--
local function main()
assert(devicetree.init())
assert(system.init())
sched.wait(1)
reserve_serial_port ()
sched.wait(1)
serdev = open_serial_port ()
sched.wait(1)
write_to_serial_port (serdev, 'Serial port open, please enter data.\r\n')
sched.wait(2)
sched.run(WriteLoop(serdev))
sched.run(ReadLoop(serdev))
end
function WriteLoop(serial)
while true do
print('writeloop')
write_to_serial_port(serial, 'WriteLoop')
sched.wait(2)
end
end
function ReadLoop(serial)
local char = read_serial_port(serial)
if(char == "\000")
then
print ('break')
end
print (char)
end
sched.run(main)
--sched.run(WriteLoop(serdev))
sched.loop()