initSer(4,230400,8,0,1) -- RCP Chan Sample Prec Min Max Units VirtualTP1 = addChannel("TP1", 25, 0, 0, 100, "%") setTickRate(25) PacketCounter = 0 function onTick() DataPacket = {} Header = false -- Initialize Header flag to false ByteReceived = readCSer(4,1) -- Read a byte while ByteReceived ~= nil do -- Look for header if no timeout if (ByteReceived == 0xFE) then -- First byte of header is 0xFE DataPacket[1] = ByteReceived ByteReceived = readCSer(4,1) -- Read the next byte if ByteReceived == 0xAA then -- Second byte of header is 0xAA DataPacket[2] = ByteReceived Header = true end end if (Header == true) then -- Found the header Index = 3 ByteReceived = readCSer(4,2) -- Aux serial, timeout = 2ms while ByteReceived ~= nil do -- Read all the data bytes DataPacket[Index] = ByteReceived ByteReceived = readCSer(4,2) Index = Index + 1 if Index >= 64 then break end -- Too many bytes, something's wrong end if(Index==16) then -- If proper # bytes, update channels setChannel(VirtualTP1, DataPacket[5]) print(".") end -- updating virtual channels end -- processing this packet if(Header == false) then -- Else continue looking for header ByteReceived = readCSer(4,1) end end -- Outer while loop end -- onTick function