lua-users home
lua-l archive

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


We have a module 'csp' (Characater Sequence Parser) for this, where you can define sequences to be detected in data streams and call a function if the sequence pattern gets detected:

local p = csp.new(defaultHandler)

p:sequence('\027*', reset)
p:sequence('\027!', softReset)
p:sequence('\027B0', b0)
p:sequence('\027B1', b1)
p:sequence('\013', function () print('newline') end)
p:sequence('\029A0', b0)
p:sequence('\029A3', b1)
p:sequence('\027B%c-', b)
p:sequence('\029B%c', b)
p:sequence('\027=%c%c', pos)
p:sequence('\x05%S\x0d', function (s) print('string', s) end)

-- decimals

p:sequence('\027[%d', decimal)
p:sequence('\027p%d;', decimal)

p:dump()

p:parse('\027[45x\n')
p:parse('\027p42;abc\n')

We used it to decode ESC/POS printer commands.

Nut sure though if this would match you usecase.

Am 07.12.2019 um 03:38 schrieb Tim McCracken <Tim.McCracken@Utelety.com>:

 
Does anyone have a suggestion for parsing the data or even an existing project? Any and all input is welcome.
 
Thanks!
Russell
 
 
Russell,
I have been working on a “smart buffer” for just this issue, although it is not complete yet. However, it might give you some ideas on how to proceed. It is NOT pure Lua, but rather consists of two userdata object – the “smart buffer” and the serial port driver that takes the smart buffer as an argument.
 
The serial port driver simply reads and writes binary data from the smart buffer. Then in Lua, the application can read and write various sized variables from the smart buffer. For example, the library has the following functions/methods:
 
Sequential Access: (pushes to the TX buffer, pops from the RX buffer)
get_byte(), get_word(), put_word(value)
 
Random Access:
get_byte(offset), get_word(access), put_word(offset, value)
 
Of course, if you are writing a userdata library, it may be just as easy to fully decode the serial protocol in ‘C’ and use callbacks to Lua to process the data. In my experience, this may actually be a better approach for “structured” protocols such as those used for industrial automation (Modbus) or SCADA (DNP3).
 
Tim