~Chris
I used ezLuaide to make the ezLCD+102 wrap and unwrap messages.
Instructions to the ezLCD always come in the format "m###\r" or "t ###
### 'some_string_of_text'"\r where '\r' is a return.
Button reports from the ezLCD always come in the format "x##\r" or "r##
\r". I imagine you could create and decode similar messages in the
modbus format.
Where I say: ez.Rs232Tx("x" .. ID .. "\r")
Your program can: ez.Rs232Tx(Start .. Address .. Function .. Data ..
CRC .. End)
You need to write a function to compute the right CRC value which is a
little confusing but there should be plenty of help on the web.
Use something similar to:
MacroBuffer = {}
NumberOfMacros = 0
table.insert(MacroBuffer, Byte)
NumberOfMacros = NumberOfMacros + 1
In your rs232 RX function to grab each byte of the incoming message
and then add it to a buffer. Then write a function to parse each
message for the information you want.
Below is some of the code I use. If anyone sees anything wrong with
it please tell me.
CommandType = 0 --0 = none, 1 = macro, 2 = text
MacroBuffer = {}
NumberOfMacros = 0
TextStringBuffer = {}
NumberOfTextStrings = 0
function RS232RXHandler(Byte)
if CommandType == 2 then
table.insert(TextStringBuffer, Byte)
if string.char(Byte) == "\r" then
NumberOfTextStrings = NumberOfTextStrings + 1
CommandType = 0
end
elseif CommandType == 1 then
table.insert(MacroBuffer, Byte)
if string.char(Byte) == "\r" then
NumberOfMacros = NumberOfMacros + 1
CommandType = 0
end
else --if CommandType == 0 then
if string.char(Byte) == "m" then
CommandType = 1
table.insert(MacroBuffer, Byte)
elseif string.char(Byte) == "t" then
CommandType = 2
table.insert(TextStringBuffer, Byte)
elseif string.char(Byte) == "\r" then
ez.Rs232Tx(">\r")
else
ez.Rs232Tx("!\r")
end
end
end
function GetCharFromMacroBuffer()
local Temp = MacroBuffer[1]
table.remove(MacroBuffer, 1)
return Temp
end
function GetNextMacroNumber()
local Temp = 0
local NextChar
if NumberOfMacros > 0 then
NextChar = GetCharFromMacroBuffer()
while string.char(NextChar) ~= "m" do
NextChar = GetCharFromMacroBuffer()
end
NextChar = GetCharFromMacroBuffer()
Temp = NextChar - 48
NextChar = GetCharFromMacroBuffer()
while string.char(NextChar) ~= "\r" do
Temp = Temp * 10
Temp = Temp + (NextChar - 48)
NextChar = GetCharFromMacroBuffer()
end
NumberOfMacros = NumberOfMacros - 1
return Temp
else
return -1
end
end
function ButtonHandler(ID, Event)
ez.Button(ID, Event)
if Event == 2 then
ez.Rs232Tx("x" .. ID .. "\r")
elseif Event == 1 then
ez.Rs232Tx("r" .. ID .. "\r")
end
end
ez.Rs232Open("RS232RXHandler", 115200)
ez.SetButtonEvent("ButtonHandler")