Вот так:
uart.setup(2, 9600, 8, uart.PARITY_NONE, uart.STOPBITS_1, {tx = 17, rx = 16})
uart.start(2)
local d25 = 0
local d10 = 0
local gotRAW = {}
local startUART = false
local function ptrANSW()
local crc = 0
for i = 3, 8 do crc = crc + gotRAW[i] end
crc = bit.band(crc, 0xFF)
-- print(string.format('\t\tcrc = %02X',crc))
if crc ~= gotRAW[9] then print('Bad CRC'); gotRAW = {}; startUART = false return end
d25 = ((gotRAW[4]*256 ) + gotRAW[3])/10
d10 = ((gotRAW[6]*256 ) + gotRAW[5])/10
print('\n\t\t\tPM2.5 = '..d25..' μg/m3, d10 = '.. PM10..' μg/m3')
gotRAW = {}
startUART = false
-- dofile('analizeMTRF.lua')
end
uart.on(2,"data",1,
function(data)
local bt = string.byte(data, 1)
-- print(string.format("bt = %02X", bt))
if startUART == false and bt ~= 0xAA then return
elseif startUART == false then startUART = true end
gotRAW[#gotRAW+1] = bt
if gotRAW[2] and gotRAW[2] ~= 0xC0 then gotRAW = {}; startUART = false end
if #gotRAW == 10 then ptrANSW() end
end, 0)
Добавить комментарий