展示内容

lua模块大全 lua脚本解析modbus_串口

 

动态参数确定协议 

协议内容:

1.协议头:AB AB 
2.后32位为设备ID:262044017068AAAA
3.后2位使用背景(场景ID): 00( OEM ) ,02 (中天) 
4.后2位置代表有几个传感器:08
5.传感器参数
6.尾码:AB AB
参数说明:

01 03 01 01 03 00 
第一个和第二位:协议头判断
第三位:空气温度 
第四位 ~ 第五位 :截取位置 从第1个截取到第三个
第六位 :是否进行数据放大,01为 0.1倍  02 为 0.01倍

 

采集 脚本

-- @author qiyulin @date 2020.07.15 实现lua 采集物理网数据并展示
--初始化函数
function on_init()	
	-- 设置串口的超时时间:  timeout 接受总超时,单位:毫秒, timeeout_inter  字节间隔超时,单位:毫秒
	uart_set_timeout(1000,200)
	-- 判断是否有缓存的协议
	local len = read_flash_string(512)
	if len ~= nil and len > 0 
	then
		print(" read flush 采集参数")
	else
		-- 初始化默认采集协议
		-- AB AB 262044017068AAAA(设备id)  00( OEM背景) 04(4个值)   FE05(协议头) 01 01 02 01(空气温度)  FE05(协议头) 02030401(空气湿度)  FE05(协议头) 03050600(光照强度)  FE05(协议头) 04070800(二氧化碳) AB AB (尾码)
		local packet = {} 
		packet[0] = 0xAB
		packet[1] = 0xAB
		--设备id
		packet[2] = 0x32
		packet[3] = 0x36
		packet[4] = 0x32
		packet[5] = 0x30
		packet[6] = 0x34
		packet[7] = 0x34
		packet[8] = 0x30
		packet[9] = 0x31
		packet[10] = 0x37
		packet[11] = 0x30
		packet[12] = 0x36
		packet[13] = 0x38
		packet[14] = 0x41
		packet[15] = 0x41
		packet[16] = 0x41
		packet[17] = 0x41	
		-- 背景屏幕
		packet[18] = 0x02
		-- 传感器数量
		packet[19] = 0x04
		-- 空气温度
		packet[20] = 0x01
		packet[21] = 0x03
		packet[22] = 0x01
		packet[23] = 0x05
		packet[24] = 0x06
		packet[25] = 0x01
		-- 空气湿度
		packet[26] = 0x01
		packet[27] = 0x03
		packet[28] = 0x02
		packet[29] = 0x03
		packet[30] = 0x04
		packet[31] = 0x01
	    -- 光照强度
	    packet[32] = 0x01
		packet[33] = 0x03
		packet[34] = 0x03
		packet[35] = 0x13
		packet[36] = 0x15
		packet[37] = 0x00
		-- 二氧化碳
	    packet[38] = 0x01
		packet[39] = 0x03
		packet[40] = 0x04
		packet[41] = 0x0b
		packet[42] = 0x0c
		packet[43] = 0x00
		-- 结束码
		packet[44] = 0xAB
		packet[45] = 0xAB	
		-- count
		local leng = 0
		for i,v in ipairs(packet) do
			leng = leng+1
	    end
		
		-- 写入缓存	
	    write_flash_string(512,leng);	
		write_flash(0,packet);
	 	print("写入默认参数完成->"..leng)
	end
end

--设置全局变量uart_free_protocol,使用自由串口协议
uart_free_protocol=1

-- 调用uart_send_data后收到的内容
function on_uart_recv_data(packet)
	-- 获取packet 的大小
	local leng = 0
	for k,v in pairs(packet) do
		print(k.."->"..v)
 		leng = leng+1	
	end
	print("receive leng->"..leng)
	--获取屏幕编号
	local sc=get_current_screen()
	print("current screen->"..sc)
	
	-- 判断wifi是否连接成功 ,有返回值则返回48 
	if leng==2
	then
		print("wifi connect")
		set_visiable(sc,3,0) 
	end
	
	-- 判断是否为 AB AB 协议
	if packet[0]==0xAB and packet[1]==0xAB		
	then
		
		-- 把云端发送过来的参数内容写到flash
		write_flash_string(512,leng);	
		write_flash(0,packet);
	 	print("写入云端参数完成,等待生效->"..leng)
		
 	-- 读取北斗定位内容 01 03 46 24 47 4E 52 4D 43 2C 	
	elseif packet[0]==0x01 and packet[1]==0x03 and packet[2]==0x46 and packet[3]==0x24 and packet[4]==0x47 and packet[5]==0x4E and packet[6]==0x52 and packet[7]==0x4D and packet[8]==0x43 and packet[9]==0x2C
	then
		print("有北斗定位")
		-- 判断获取是否成功
		if packet[19]==0x2C and packet[20]==0x41 and packet[21]==0x2C
		then
			print("正解析定位值")
			-- 获取纬度
			local lats = string.char(packet[22]).. string.char(packet[23])
			local late = string.char(packet[24]).. string.char(packet[25]).. string.char(packet[26]).. string.char(packet[27]).. string.char(packet[28]).. string.char(packet[29]).. string.char(packet[30]).. string.char(packet[31])
			local latxs = string.format("%0.4f",tonumber(late)/60)		
			local lat = tonumber(lats)+tonumber(latxs)
			-- 获取纬度(N,S)
			local ns = string.char(packet[33])
			--获取经度
			local lngs = string.char(packet[35]).. string.char(packet[36]).. string.char(packet[37])
			local lnge = string.char(packet[38]).. string.char(packet[39]).. string.char(packet[40]).. string.char(packet[41]).. string.char(packet[42]).. string.char(packet[43]).. string.char(packet[44]).. string.char(packet[45])
			local lngxs = string.format("%0.4f",tonumber(lnge)/60)
			local lng = tonumber(lngs)+tonumber(lngxs)
			-- 获取经度(E,W)
			local  ew = string.char(packet[47])
			-- 设置到组件
			set_text(sc,7,"北斗定位:"..lat.." ( "..ns.." ) ,"..lng.." ( "..ew.." ) ")
		end

	else
	
		-- 根据广播内容获取数据内容
		local leng = read_flash_string(512)
		local data = read_flash(0,leng)
		if data[0]==0xAB and data[1]==0xAB
		then
			-- 判断有几个参数
			local mynum = data[19]
			-- 判断参数内容
			for q=1,mynum do	
				local qindex=20+(q-1)*6				
				local myx1 = data[qindex]
				local myx2 = data[qindex+1]
				
				---获取数值单位
				local myunit = get_unit_label(mytype,true)
				-- 比较 packet 数据包
				if  packet[0]==myx1 and packet[1]==myx2
				then
					local mytype = data[qindex+2]
					local mystart = data[qindex+3]
					local myend = data[qindex+4]
					local myxs = data[qindex+5]
					print(qindex+5)
					local myno1 = 100+q
		 			print("参数: x1: "..myx1..",x2:"..myx2..",type->"..mytype..",start->"..mystart..",end->"..myend..",myxs->"..myxs)	
					local myunit = get_unit_label(mytype,false)
					local mynumstr = "0.0"
					local mynum=0
					-- 遍历变量
					local cha = myend - mystart
					if cha==0
					then
						mynum = packet[mystart] 
					elseif cha==1 
					then 
						mynum = packet[mystart]*256 + packet[myend] 
					elseif cha==2 
					then 
						mynum = packet[mystart]*256*256 + packet[mystart+1]*256 + packet[myend] 
					elseif cha==3 
					then 
						mynum = packet[mystart]*256*256*256 + packet[mystart+1]*256*256 + packet[mystart+2]*256 + packet[myend] 
					end
					--判断是否需要缩小				
					if myxs==0x01
					then
						mynumstr = string.format("%0.1f",mynum/10)	
	 				elseif myxs==0x02
					then
						mynumstr = string.format("%0.2f",mynum/100)	
					else 
						mynumstr = mynum
					end
					-- 对风向进行特殊处理
					if mytype == 0x08 
					then
						mynumstr = get_fx(mynum)
						set_text(sc,myno1,mynumstr)
					else
	 					set_text(sc,myno1,mynumstr..myunit)	
					end
				end
				
			end
			print("init item text ok!")
	
		end		

	end

end

--记录开机几秒了
init_system_second = 1

-- 根据风向值获取风向文字
function get_fx(mynum)
	if mynum ==0x00
	then
		return "东北偏北"
	elseif mynum==0x01
	then
		return "东北"
	elseif mynum==0x02
	then
		return "东北偏东"
	elseif mynum==0x03
	then
		return "正东"
	elseif mynum==0x04
	then
		return "东南偏东"	
	elseif mynum==0x05
	then
		return "东南"	
	elseif mynum==0x06
	then
		return "东南偏南"	
	elseif mynum==0x07
	then
		return "正南"		
	elseif mynum==0x08
	then
		return "西南偏南"	
	elseif mynum==0x09
	then
		return "西南"		
	elseif mynum==0x0a
	then
		return "西南偏西"		
	elseif mynum==0x0b	
	then
		return "正西"		
	elseif mynum==0x0c
	 then
		return "西北偏西"		
	elseif mynum==0x0d
	then
		return "西北"		
	elseif mynum==0x0e
	then
		return "西北偏北"			
	elseif mynum==0x0f
	then
		return "正北"		
	end
	
end

-- 根据类型获取unit或者label
function get_unit_label(mytype,isL)
if mytype==0x01 
then
	if isL 
	then 
		return "空气温度" 
	else
		return "℃"
	end
end

if mytype==0x02
then
	if isL 
	then 
		return "空气湿度" 
	else
		return "%"
	end
end

if mytype==0x03
then
	if isL 
	then 
		return "光照强度" 
	else
		return "Lux"
	end
end	

if mytype==0x04
then
	if isL 
	then 
		return "二氧化碳" 
	else
		return "ppm"
	end
end	

if mytype==0x05
then
	if isL 
	then 
		return "PH值" 
	else
		return ""
	end
end

if mytype==0x06
then
	if isL 
	then 
		return "电导率" 
	else
		return "us/cm"
	end
end	

if mytype==0x07
then
	if isL 
	then 
		return "风速" 
	else
		return "m/s"
	end
end	

if mytype==0x08
then
	if isL 
	then 
		return "风向" 
	else
		return ""
	end
end
			
if mytype==0x08
then
	if isL 
	then 
		return "风向" 
	else
		return ""
	end
end	

if mytype==0x09
then
	if isL 
	then 
		return "气压" 
	else
		return "hpa"
	end
end	

if mytype==0x0a
then
	if isL 
	then 
		return "雨量" 
	else
		return "mm/h"
	end
end	

if mytype==0x0b
then
	if isL 
	then 
		return "紫外线强度" 
	else
		return "w/㎡"
	end
end	

if mytype==0x0c
then
	if isL 
	then 
		return "光合强度" 
	else
		return "w/㎡"
	end
end	

if mytype==0x0d
then
	if isL 
	then 
		return "露点温度" 
	else
		return "℃"
	end
end	

if mytype==0x0e
then
	if isL 
	then 
		return "土壤温度" 
	else
		return "℃"
	end
end	

if mytype==0x0f
then
	if isL 
	then 
		return "土壤湿度" 
	else
		return "%"
	end
end	

if mytype==0x10
then
	if isL 
	then 
		return "土壤温度2" 
	else
		return "℃"
	end
end	

if mytype==0x11
then
	if isL 
	then 
		return "土壤温度3" 
	else
		return "℃"
	end
end	

if mytype==0x12
then
	if isL 
	then 
		return "土壤温度4" 
	else
		return "℃"
	end
end	

if mytype==0x13
then
	if isL 
	then 
		return "土壤湿度2" 
	else
		return "%"
	end
end	

if mytype==0x14
then
	if isL 
	then 
		return "土壤湿度3" 
	else
		return "%"
	end
end	

if mytype==0x15
then
	if isL 
	then 
		return "土壤湿度4" 
	else
		return "%"
	end
end	

if mytype==0x16
then
	if isL 
	then 
		return "电导率2" 
	else
		return "us/cm"
	end
end	

if mytype==0x17
then
	if isL 
	then 
		return "电导率3" 
	else
		return "us/cm"
	end
end	

if mytype==0x18
then
	if isL 
	then 
		return "电导率4" 
	else
		return "us/cm"
	end
end	

end

--定时回调函数,系统每隔1秒钟自动调用。
function on_systick()
	print(init_system_second)
	if init_system_second ==5 or init_system_second==59
	then
		
		--获取屏幕编号
		local sc=get_current_screen()
				
		--获取缓存协议
		local leng = read_flash_string(512)
		local packet = read_flash(0,leng)
		-- 判断是否为控制协议
		if packet[0]==0xAB and packet[1]==0xAB
		then
			--切换背景
			local mybg = packet[18]
				print("切换背景->"..mybg)	
			if sc==mybg
			then
			--是一个背景不需要切换
			else
				change_screen(mybg)
			end
			
			-- 设置二维码内容
			local mydid = get_text(mybg,4)
			if mydid == "AA" 
			then
				local did = ""
				for i= 2, 17 do
				   did = did..string.char(packet[i])
				end
				print("set qrcode ok")
				set_text(mybg,4,did)
			end
			-- 判断有几个参数
			local mynum = packet[19]
			for z= 0, 12 do
					 local myk3 = 301+z
				 local myk2 = 201+z
				 local myk1 = 101+z	
				 if z< mynum
				 then
				 	set_visiable(mybg,myk1,1)
	 				set_visiable(mybg,myk2,1)
					set_visiable(mybg,myk3,1) 
				 else
 					 set_visiable(mybg,myk1,0)
		 			 set_visiable(mybg,myk2,0)
					 set_visiable(mybg,myk3,0)  
				 end
			end	
 			print("init item ok!")	
			-- 判断参数内容
			for q=1,mynum do	
				local qindex=22+(q-1)*6				
				local mytype = packet[qindex]				
				---判断内容
				local mylabel = get_unit_label(mytype,true)				
				print("type->"..mytype..",label->"..mylabel)
		
				local myno2 = 200+q;
				set_text(mybg,myno2,mylabel)
			end
			print("init item text ok!")
			
		end	
		
		-- 重置记时
		init_system_second=6
	end 

	-- 记时
	
	init_system_second=init_system_second+1	

end

网盘地址:https://pan.baidu.com/s/1xg6RTwkk3CHbZ9QEapySxA

提取码:zjjd