lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Sun, Apr 3, 2011 at 5:22 PM, soly man <luascript2010@gmail.com> wrote:
> but I want the code to run in a loop and till it find the word RING came ,
> then it will exit the loop and run an application and exit
> so how  I can check (read ) the word RING came from the COM3 port

I think p:read can return the error code (e) of
rs232.RS232_ERR_TIMEOUT if the timeout is reached.  Maybe try
something like this (untested):

local data = ''
local timeout = 1000 -- in miliseconds
while data ~= 'RING' do
  local err, data_read, size = p:read(1, timeout)
  if e == rs232.RS232_ERR_TIMEOUT then
    io.stdout:write '[T]'
  else
    assert(e == rs232.RS232_ERR_NOERROR, e)
    io.stdout:write(data_read)
    data = (data .. data_read):sub(1, 4)
  end
end
io.stdout:write 'OK\n'