大彩 DACAI lua脚本实现 modbus 485 从机数据采集(支持云端下发脚本控制),含有北斗设备定位

编程入门 行业动态 更新时间:2024-10-26 04:22:35

大彩 DACAI lua<a href=https://www.elefans.com/category/jswz/34/1771291.html style=脚本实现 modbus 485 从机数据采集(支持云端下发脚本控制),含有北斗设备定位"/>

大彩 DACAI lua脚本实现 modbus 485 从机数据采集(支持云端下发脚本控制),含有北斗设备定位

展示内容

 

动态参数确定协议 

协议内容: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 thenprint(" 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] = 0xABpacket[1] = 0xAB--设备idpacket[2] = 0x32packet[3] = 0x36packet[4] = 0x32packet[5] = 0x30packet[6] = 0x34packet[7] = 0x34packet[8] = 0x30packet[9] = 0x31packet[10] = 0x37packet[11] = 0x30packet[12] = 0x36packet[13] = 0x38packet[14] = 0x41packet[15] = 0x41packet[16] = 0x41packet[17] = 0x41	-- 背景屏幕packet[18] = 0x02-- 传感器数量packet[19] = 0x04-- 空气温度packet[20] = 0x01packet[21] = 0x03packet[22] = 0x01packet[23] = 0x05packet[24] = 0x06packet[25] = 0x01-- 空气湿度packet[26] = 0x01packet[27] = 0x03packet[28] = 0x02packet[29] = 0x03packet[30] = 0x04packet[31] = 0x01-- 光照强度packet[32] = 0x01packet[33] = 0x03packet[34] = 0x03packet[35] = 0x13packet[36] = 0x15packet[37] = 0x00-- 二氧化碳packet[38] = 0x01packet[39] = 0x03packet[40] = 0x04packet[41] = 0x0bpacket[42] = 0x0cpacket[43] = 0x00-- 结束码packet[44] = 0xABpacket[45] = 0xAB	-- countlocal leng = 0for i,v in ipairs(packet) doleng = leng+1end-- 写入缓存	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 = 0for k,v in pairs(packet) doprint(k.."->"..v)leng = leng+1	endprint("receive leng->"..leng)--获取屏幕编号local sc=get_current_screen()print("current screen->"..sc)-- 判断wifi是否连接成功 ,有返回值则返回48 if leng==2thenprint("wifi connect")set_visiable(sc,3,0) end-- 判断是否为 AB AB 协议if packet[0]==0xAB and packet[1]==0xAB		then-- 把云端发送过来的参数内容写到flashwrite_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]==0x2Cthenprint("有北斗定位")-- 判断获取是否成功if packet[19]==0x2C and packet[20]==0x41 and packet[21]==0x2Cthenprint("正解析定位值")-- 获取纬度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.." ) ")endelse-- 根据广播内容获取数据内容local leng = read_flash_string(512)local data = read_flash(0,leng)if data[0]==0xAB and data[1]==0xABthen-- 判断有几个参数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]==myx2thenlocal 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+qprint("参数: 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 - mystartif cha==0thenmynum = 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==0x01thenmynumstr = string.format("%0.1f",mynum/10)	elseif myxs==0x02thenmynumstr = string.format("%0.2f",mynum/100)	else mynumstr = mynumend-- 对风向进行特殊处理if mytype == 0x08 thenmynumstr = get_fx(mynum)set_text(sc,myno1,mynumstr)elseset_text(sc,myno1,mynumstr..myunit)	endendendprint("init item text ok!")end		endend--记录开机几秒了
init_system_second = 1-- 根据风向值获取风向文字
function get_fx(mynum)if mynum ==0x00thenreturn "东北偏北"elseif mynum==0x01thenreturn "东北"elseif mynum==0x02thenreturn "东北偏东"elseif mynum==0x03thenreturn "正东"elseif mynum==0x04thenreturn "东南偏东"	elseif mynum==0x05thenreturn "东南"	elseif mynum==0x06thenreturn "东南偏南"	elseif mynum==0x07thenreturn "正南"		elseif mynum==0x08thenreturn "西南偏南"	elseif mynum==0x09thenreturn "西南"		elseif mynum==0x0athenreturn "西南偏西"		elseif mynum==0x0b	thenreturn "正西"		elseif mynum==0x0cthenreturn "西北偏西"		elseif mynum==0x0dthenreturn "西北"		elseif mynum==0x0ethenreturn "西北偏北"			elseif mynum==0x0fthenreturn "正北"		endend-- 根据类型获取unit或者label
function get_unit_label(mytype,isL)
if mytype==0x01 
thenif isL then return "空气温度" elsereturn "℃"end
endif mytype==0x02
thenif isL then return "空气湿度" elsereturn "%"end
endif mytype==0x03
thenif isL then return "光照强度" elsereturn "Lux"end
end	if mytype==0x04
thenif isL then return "二氧化碳" elsereturn "ppm"end
end	if mytype==0x05
thenif isL then return "PH值" elsereturn ""end
endif mytype==0x06
thenif isL then return "电导率" elsereturn "us/cm"end
end	if mytype==0x07
thenif isL then return "风速" elsereturn "m/s"end
end	if mytype==0x08
thenif isL then return "风向" elsereturn ""end
endif mytype==0x08
thenif isL then return "风向" elsereturn ""end
end	if mytype==0x09
thenif isL then return "气压" elsereturn "hpa"end
end	if mytype==0x0a
thenif isL then return "雨量" elsereturn "mm/h"end
end	if mytype==0x0b
thenif isL then return "紫外线强度" elsereturn "w/㎡"end
end	if mytype==0x0c
thenif isL then return "光合强度" elsereturn "w/㎡"end
end	if mytype==0x0d
thenif isL then return "露点温度" elsereturn "℃"end
end	if mytype==0x0e
thenif isL then return "土壤温度" elsereturn "℃"end
end	if mytype==0x0f
thenif isL then return "土壤湿度" elsereturn "%"end
end	if mytype==0x10
thenif isL then return "土壤温度2" elsereturn "℃"end
end	if mytype==0x11
thenif isL then return "土壤温度3" elsereturn "℃"end
end	if mytype==0x12
thenif isL then return "土壤温度4" elsereturn "℃"end
end	if mytype==0x13
thenif isL then return "土壤湿度2" elsereturn "%"end
end	if mytype==0x14
thenif isL then return "土壤湿度3" elsereturn "%"end
end	if mytype==0x15
thenif isL then return "土壤湿度4" elsereturn "%"end
end	if mytype==0x16
thenif isL then return "电导率2" elsereturn "us/cm"end
end	if mytype==0x17
thenif isL then return "电导率3" elsereturn "us/cm"end
end	if mytype==0x18
thenif isL then return "电导率4" elsereturn "us/cm"end
end	end--定时回调函数,系统每隔1秒钟自动调用。
function on_systick()print(init_system_second)if init_system_second ==5 or init_system_second==59then--获取屏幕编号local sc=get_current_screen()--获取缓存协议local leng = read_flash_string(512)local packet = read_flash(0,leng)-- 判断是否为控制协议if packet[0]==0xAB and packet[1]==0xABthen--切换背景local mybg = packet[18]print("切换背景->"..mybg)	if sc==mybgthen--是一个背景不需要切换elsechange_screen(mybg)end-- 设置二维码内容local mydid = get_text(mybg,4)if mydid == "AA" thenlocal did = ""for i= 2, 17 dodid = did..string.char(packet[i])endprint("set qrcode ok")set_text(mybg,4,did)end-- 判断有几个参数local mynum = packet[19]for z= 0, 12 dolocal myk3 = 301+zlocal myk2 = 201+zlocal myk1 = 101+z	if z< mynumthenset_visiable(mybg,myk1,1)set_visiable(mybg,myk2,1)set_visiable(mybg,myk3,1) elseset_visiable(mybg,myk1,0)set_visiable(mybg,myk2,0)set_visiable(mybg,myk3,0)  endend	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)endprint("init item text ok!")end	-- 重置记时init_system_second=6end -- 记时init_system_second=init_system_second+1	end

网盘地址:

提取码:zjjd

感谢您的支持,如对您有所帮助,请您打赏,谢谢啦~

更多推荐

大彩 DACAI lua脚本实现 modbus 485 从机数据采集(支持云端下发脚本控制),含有北斗设备定位

本文发布于:2024-02-10 20:58:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1677244.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   云端   北斗   数据采集   设备

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!