modbusTT.lua 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819
  1. --- modbus模块功能
  2. -- @module modbus
  3. -- @author Dozingfiretruck
  4. -- @license MIT
  5. -- @copyright openLuat
  6. -- @release 2020.12.17
  7. module(...,package.seeall)
  8. require"utils"
  9. require"common"
  10. require"pm"
  11. require"pins"
  12. -- require "sdModuel"
  13. -- require "logModuel"
  14. --保持系统处于唤醒状态,此处只是为了测试需要,所以此模块没有地方调用pm.sleep("testUart")休眠,不会进入低功耗休眠状态
  15. --在开发“要求功耗低”的项目时,一定要想办法保证pm.wake("modbusrtu")后,在不需要串口时调用pm.sleep("testUart")
  16. local uart_id = 1
  17. -- local uart_baud = 9600
  18. local uart_baud = 115200
  19. -- pmd.ldoset(2,pmd.LDO_VMMC)
  20. local BAT_ADC_ID = 2
  21. adc.open(BAT_ADC_ID)
  22. local queryCondReg = nil --1000
  23. local queryCondRegValue = {} --
  24. local queryFlagReg = {}
  25. queryFlagReg["0064"] = "0000"
  26. queryFlagReg["0065"] = "0000"
  27. queryFlagReg["0066"] = "0000"
  28. queryFlagReg["0067"] = "0000"
  29. queryFlagReg["0068"] = "0000"
  30. queryFlagReg["0069"] = "0000"
  31. queryFlagReg["006a"] = "0000"
  32. queryFlagReg["006b"] = "0000"
  33. queryFlagReg["006c"] = "0000"
  34. queryFlagReg["006d"] = "0000"
  35. queryFlagReg["006e"] = "0000"
  36. queryFlagReg["006f"] = "0000"
  37. queryFlagReg["0070"] = "0000"
  38. local queryFlagResReg = {} -- 200
  39. queryFlagResReg["00c8"] = "0000"
  40. queryFlagResReg["00c9"] = "0000"
  41. queryFlagResReg["00ca"] = "0000"
  42. queryFlagResReg["00cb"] = "0000"
  43. queryFlagResReg["00cc"] = "0000"
  44. queryFlagResReg["00cd"] = "0000"
  45. queryFlagResReg["00ce"] = "0000"
  46. queryFlagResReg["00cf"] = "0000"
  47. queryFlagResReg["00d0"] = "0000"
  48. queryFlagResReg["00d1"] = "0000"
  49. queryFlagResReg["00d2"] = "0000"
  50. queryFlagResReg["00d3"] = "0000"
  51. queryFlagResReg["00d4"] = "0000" -- 1400
  52. local queryRespData = {} --
  53. local newPumbModelT = ""
  54. local newPumbNumberT = ""
  55. local oldPumbModelT = ""
  56. local oldPumbNumberT = ""
  57. local sensorNumberT = ""
  58. -- 获取table长度
  59. function table_leng(t)
  60. local leng=0
  61. for k, v in pairs(t) do
  62. leng=leng+1
  63. end
  64. return leng;
  65. end
  66. -- 将sting字节流转为字符串57 53 2D 54 2E 36 2D 30 35 31 00 34
  67. function getByte2AsciiStr( data )
  68. -- body
  69. local str = ""
  70. for i=1,#data, 2 do
  71. if string.char(data:byte(i)) == "\0" then
  72. -- body
  73. if string.char(data:byte(i+1)) == "\0" then
  74. -- body
  75. else
  76. str = str..string.char(data:byte(i+1))
  77. end
  78. break
  79. end
  80. str = str.. string.char(data:byte(i+1)) .. string.char(data:byte(i)) -- 011003E8000A14727731653332353437363938003700000000000000003774
  81. end
  82. return str
  83. end
  84. -- 将utf8str转16进制字符串,按宽度补0
  85. function getAscii2StringHexBe( str, width )
  86. -- body
  87. local hexStr = ""
  88. if width == 0 then
  89. -- body
  90. return hexStr
  91. end
  92. for i=1,#str, 2 do
  93. if i+1 > #str then
  94. -- body
  95. hexStr = hexStr .. string.format("%02x", string.byte("\0")) ..string.format("%02x", str:byte(i))
  96. break
  97. end
  98. hexStr = hexStr .. string.format("%02x", str:byte(i+1)) .. string.format("%02x", str:byte(i))
  99. end
  100. if (width - #str) > 0 then
  101. -- body
  102. local clen = 0
  103. if not IsOuNumber(#str) then
  104. clen = width - #str -1
  105. else
  106. clen = width - #str
  107. end
  108. for i=1,clen do
  109. hexStr = hexStr .. string.format("%02x", 0)
  110. end
  111. end
  112. return hexStr
  113. end
  114. -- 获取格式化日期时间
  115. function getDateTimeStr( )
  116. local tm = os.date("*t")
  117. return string.format("%04d%02d%02d%02d%02d%02d", tm.year, tm.month, tm.day, tm.hour, tm.min, tm.sec)
  118. end
  119. function IsOuNumber(num)
  120. local num1,num2=math.modf(num/2)--返回整数和小数部分
  121. if(num2==0)then
  122. return true
  123. else
  124. return false
  125. end
  126. end
  127. function ucs2deleteZero( ucs2Byte )
  128. -- body
  129. local ustr = ""
  130. for i=1,#ucs2Byte, 2 do
  131. local ascii1, ascii2 = ucs2Byte:byte(i),ucs2Byte:byte(i+1)
  132. if ascii1 == 0 and ascii2 ==0 then
  133. -- body
  134. break
  135. else
  136. ustr = ustr..common.ucs2beToUtf8(ucs2Byte:sub(i, i+1))
  137. end
  138. end
  139. return ustr
  140. end
  141. local function handlePumbHeart( startAddress, regNumber, data )
  142. local startAddDec = tonumber(startAddress, 16)
  143. local regNumberDec = tonumber(regNumber, 16)
  144. -- 01 10 0191 0001 02 0001 6bd1
  145. for i=1, 2*regNumberDec , 2 do
  146. local value = tonumber(data:sub(i,i+1):toHex(), 16)
  147. if startAddDec == 401 then
  148. -- body
  149. -- log.info("pumbStatus", startAddDec, value)
  150. if nvm.get("pumbStatus") ~= value then
  151. -- body
  152. nvm.set("pumbStatus", value)
  153. end
  154. end
  155. if startAddDec == 410 then
  156. -- body
  157. -- log.info("hydOil", startAddDec, value)
  158. if nvm.get("hydOil") ~= value then
  159. -- body
  160. nvm.set("hydOil", value)
  161. end
  162. end
  163. if startAddDec == 411 then
  164. -- body
  165. -- log.info("scaleValve", startAddDec, value)
  166. if nvm.get("scaleValve") ~= value then
  167. -- body
  168. nvm.set("scaleValve", value)
  169. end
  170. end
  171. if startAddDec == 412 then
  172. -- body
  173. -- log.info("dianci", startAddDec, value)
  174. if nvm.get("elecValve") ~= value then
  175. -- body
  176. nvm.set("elecValve", value)
  177. end
  178. end
  179. if startAddDec == 413 then
  180. -- body
  181. -- log.info("dianci", startAddDec, value)
  182. if nvm.get("filterValue") ~= value then
  183. -- body
  184. nvm.set("elecValve", value)
  185. end
  186. end
  187. if startAddDec > 419 and startAddDec < 430 then
  188. -- body
  189. -- log.info("dianci", startAddDec, value)
  190. newPumbModelT = newPumbModelT .. data:sub(i,i+1)
  191. if #newPumbModelT == 20 then
  192. -- body
  193. newPumbModelT = getByte2AsciiStr(newPumbModelT)
  194. nvm.set("newPumbModel", newPumbModelT)
  195. newPumbModelT = ""
  196. end
  197. end
  198. if startAddDec > 429 and startAddDec < 440 then
  199. -- body
  200. -- log.info("dianci", startAddDec, value)
  201. newPumbNumberT = newPumbNumberT .. data:sub(i,i+1)
  202. if #newPumbNumberT == 20 then
  203. -- body
  204. newPumbNumberT = getByte2AsciiStr(newPumbNumberT)
  205. nvm.set("newPumbNumber", newPumbNumberT)
  206. newPumbNumberT = ""
  207. end
  208. end
  209. if startAddDec > 439 and startAddDec < 450 then
  210. -- body
  211. -- log.info("dianci", startAddDec, value)
  212. oldPumbModelT = oldPumbModelT .. data:sub(i,i+1)
  213. if #oldPumbModelT == 20 then
  214. -- body
  215. oldPumbModelT = getByte2AsciiStr(oldPumbModelT)
  216. nvm.set("oldPumbModel", oldPumbModelT)
  217. oldPumbModelT = ""
  218. end
  219. end
  220. if startAddDec > 449 and startAddDec < 460 then
  221. -- body
  222. -- log.info("dianci", startAddDec, value)
  223. oldPumbNumberT = oldPumbNumberT .. data:sub(i,i+1)
  224. if #oldPumbNumberT == 20 then
  225. -- body
  226. oldPumbNumberT = getByte2AsciiStr(oldPumbNumberT)
  227. nvm.set("oldPumbNumber", oldPumbNumberT)
  228. oldPumbNumberT = ""
  229. end
  230. end
  231. if startAddDec > 459 and startAddDec < 470 then
  232. -- body
  233. -- log.info("dianci", startAddDec, value)
  234. sensorNumberT = sensorNumberT .. data:sub(i,i+1)
  235. if #sensorNumberT == 20 then
  236. -- body
  237. sensorNumberT = getByte2AsciiStr(sensorNumberT)
  238. nvm.set("sensorNumber", sensorNumberT)
  239. sensorNumberT = ""
  240. end
  241. end
  242. startAddDec = startAddDec +1
  243. end
  244. end
  245. local function mobBussErrSend (slaveAdd, functionCode, errcode )
  246. -- body
  247. local errRespData, errRespCrc
  248. errRespData = (slaveAdd..functionCode..errcode):fromHex()
  249. errRespCrc = pack.pack('<h', crypto.crc16("MODBUS",errRespData))
  250. logModuel.debug_log("send 485:"..(errRespData..errRespCrc):toHex() )
  251. uart.write(uart_id, errRespData..errRespCrc)
  252. end
  253. -- 处理接收到的数据
  254. local function proc( data )
  255. -- body 010600CF00017835
  256. orginDataHex =string.toHex(data)
  257. if not orginDataHex then
  258. -- body
  259. return false
  260. end
  261. if #data < 8 then
  262. -- body
  263. return false
  264. end
  265. if not crc16Compare(orginDataHex) then
  266. -- body
  267. logModuel.debug_log("data:"..orginDataHex..",CRC check failed!")
  268. return false
  269. end
  270. local slaveAdd = string.format("%02x",data:byte(1))
  271. local functionCode = string.format("%02x",data:byte(2))
  272. local startRegAddress = string.format("%02x",data:byte(3)) ..string.format("%02x",data:byte(4))
  273. local multiRespData = ""
  274. local respCrc = ""
  275. local errFunctionCode = ""
  276. if not slaveAdd or not functionCode then
  277. -- body
  278. logModuel.debug_log("data:"..orginDataHex..",没有从机码和功能码!")
  279. return false
  280. end
  281. if functionCode ~= "06" and functionCode ~= "10" and functionCode ~= "03" then
  282. -- body
  283. if not tonumber(functionCode) then
  284. -- body
  285. errFunctionCode = functionCode
  286. else
  287. errFunctionCode = tonumber(functionCode) + 80
  288. end
  289. multiRespData = (slaveAdd..errFunctionCode.."01"):fromHex()
  290. respCrc = pack.pack('<h', crypto.crc16("MODBUS",multiRespData))
  291. uart.write(uart_id, multiRespData..respCrc)
  292. logModuel.debug_log("data:"..orginDataHex..",非 03, 06, 10功能码,不允许操作!")
  293. return false
  294. end
  295. local regAddHex = tonumber(startRegAddress, 16)
  296. if functionCode == "06" then
  297. -- body
  298. local value = string.format("%02x",data:byte(5)).. string.format("%02x",data:byte(6))
  299. if not value then
  300. -- body
  301. mobBussErrSend(slaveAdd, "86", "03")
  302. return false
  303. end
  304. if regAddHex >= 100 and regAddHex < 200 then
  305. -- body
  306. queryFlagReg[startRegAddress] = "0001"
  307. setQueryFlagData(startRegAddress, data)
  308. end
  309. if regAddHex >= 200 and regAddHex < 300 then
  310. mobBussErrSend(slaveAdd, "86", "01")
  311. return false
  312. end
  313. if regAddHex >= 1400 then
  314. mobBussErrSend(slaveAdd, "86", "01")
  315. return false
  316. end
  317. multiRespData = (slaveAdd..functionCode..startRegAddress..value):fromHex()
  318. respCrc = pack.pack('<h', crypto.crc16("MODBUS",multiRespData))
  319. uart.write(uart_id, multiRespData..respCrc)
  320. end
  321. if functionCode == "10" then
  322. -- body
  323. if #data < 11 then
  324. -- body
  325. mobBussErrSend(slaveAdd, "90", "01")
  326. return false
  327. end
  328. local regNumber = string.format("%02x",data:byte(5)) .. string.format("%02x",data:byte(6))
  329. if regAddHex >= 1000 and regAddHex < 1400 then
  330. -- body 01 10 03e8 0001 02 0000 aaaa
  331. setQueryCondRegData(startRegAddress, data)
  332. end
  333. if regAddHex >= 100 and regAddHex < 200 then
  334. -- body
  335. if regAddHex > 212 then
  336. -- body
  337. mobBussErrSend(slaveAdd, "90", "01")
  338. return false
  339. end
  340. queryFlagReg[startRegAddress] = "0001"
  341. setQueryFlagData(startRegAddress, data)
  342. end
  343. if regAddHex >= 200 and regAddHex < 300 then
  344. -- body
  345. mobBussErrSend(slaveAdd, "90", "01")
  346. return false
  347. end
  348. if regAddHex >= 400 and regAddHex < 500 then --泵心跳相关
  349. -- queryFlagResReg[startRegAddress] = data
  350. local byteCount = string.format("%02x",data:byte(7))
  351. handlePumbHeart(startRegAddress, regNumber,data:sub(8, tonumber(byteCount, 16)+7))
  352. end
  353. if regAddHex >= 1400 then
  354. mobBussErrSend(slaveAdd, "90", "01")
  355. return false
  356. end
  357. multiRespData = (slaveAdd..functionCode..startRegAddress..regNumber):fromHex()
  358. respCrc = pack.pack('<h', crypto.crc16("MODBUS",multiRespData))
  359. logModuel.debug_log("send 485:"..(multiRespData..respCrc):toHex())
  360. uart.write(uart_id, multiRespData..respCrc)
  361. end
  362. if functionCode == "03" then
  363. -- body
  364. local regNumber = string.format("%02x",data:byte(5)) .. string.format("%02x",data:byte(6))
  365. local respValue = ""
  366. log.info("regAddHex:", regAddHex, "startAdd:",startRegAddress)
  367. if regAddHex >= 100 and regAddHex < 200 then
  368. -- body
  369. respValue = queryFlagReg[startRegAddress]
  370. if not respValue then
  371. -- body
  372. mobBussErrSend(slaveAdd, "83", "01")
  373. return false
  374. end
  375. byteCount = "02"
  376. multiRespData = (slaveAdd..functionCode..byteCount..respValue):fromHex()
  377. respCrc = pack.pack('<h', crypto.crc16("MODBUS",multiRespData))
  378. logModuel.debug_log("send 485:"..(multiRespData..respCrc):toHex())
  379. uart.write(uart_id, multiRespData..respCrc)
  380. return true
  381. end
  382. if regAddHex >= 200 and regAddHex < 300 then
  383. -- body
  384. respValue = queryFlagResReg[startRegAddress]
  385. if not respValue then
  386. -- body
  387. mobBussErrSend(slaveAdd, "83", "01")
  388. return false
  389. end
  390. byteCount = "02"
  391. multiRespData = (slaveAdd..functionCode..byteCount..respValue):fromHex()
  392. respCrc = pack.pack('<h', crypto.crc16("MODBUS",multiRespData))
  393. logModuel.debug_log("send 485:"..(multiRespData..respCrc):toHex())
  394. uart.write(uart_id, multiRespData..respCrc)
  395. return true
  396. end
  397. if regAddHex >= 1400 then
  398. -- body
  399. if tonumber(regNumber, 16) == 1 then
  400. -- body
  401. log.info("startRegAddress:",startRegAddress, "value:", queryRespData[startRegAddress])
  402. respValue = queryRespData[startRegAddress]
  403. if not respValue then
  404. -- body
  405. mobBussErrSend(slaveAdd, "83", "01")
  406. return false
  407. end
  408. else
  409. local regNumberDec,startRegAddressDec = tonumber(regNumber,16), tonumber(startRegAddress,16)
  410. if regNumberDec > table_leng(queryRespData) then
  411. -- body
  412. logModuel.debug_log("read regNumber > store queryRespData table length!")
  413. mobBussErrSend(slaveAdd, "83", "03")
  414. return false
  415. end
  416. for i=startRegAddressDec, (startRegAddressDec + regNumberDec-1) do
  417. local regHex = string.format("%04x", i)
  418. if queryRespData[regHex] then
  419. -- body
  420. respValue = respValue..queryRespData[regHex]
  421. else
  422. logModuel.debug_log("for in regNumber > store queryRespData table max regNumber!")
  423. mobBussErrSend(slaveAdd, "83", "03")
  424. return false
  425. end
  426. end
  427. end
  428. if not respValue then
  429. -- body
  430. logModuel.debug_log("modbus.proc error! maybe receive data unusual or sequence not correct! regAddHex=" .. regAddHex)
  431. return
  432. end
  433. byteCount = string.format("%02x", tonumber(regNumber, 16)*2)
  434. multiRespData = (slaveAdd..functionCode..byteCount..respValue):fromHex()
  435. respCrc = pack.pack('<h', crypto.crc16("MODBUS",multiRespData))
  436. logModuel.debug_log("send 485:"..(multiRespData..respCrc):toHex() )
  437. uart.write(uart_id, multiRespData..respCrc)
  438. return true
  439. end
  440. if regAddHex >= 600 and regAddHex < 700 then
  441. -- body
  442. local regNumberDec,startRegAddressDec = tonumber(regNumber,16), tonumber(startRegAddress,16)
  443. local networkInfo = getNetWorkInfoHex()
  444. local stap = (regAddHex-600)*4+1
  445. local endp = regNumberDec*4 + stap-1
  446. for i=stap,endp, 4 do
  447. respValue = respValue.. networkInfo:sub(i, i+3)
  448. end
  449. byteCount = string.format("%02x", tonumber(regNumber, 16)*2)
  450. multiRespData = (slaveAdd..functionCode..byteCount..respValue):fromHex()
  451. respCrc = pack.pack('<h', crypto.crc16("MODBUS",multiRespData))
  452. logModuel.debug_log("send 485:"..(multiRespData..respCrc):toHex())
  453. uart.write(uart_id, multiRespData..respCrc)
  454. return true
  455. end
  456. mobBussErrSend(slaveAdd, "83", "01")
  457. return false
  458. end
  459. end
  460. function getNetWorkInfoHex( )
  461. -- body
  462. netWorkDeviceData = ""
  463. local dateObj = os.date("*t")
  464. local temp = ""
  465. for i=600,619 do
  466. local regHex = string.format("%04x", i)
  467. if i == 600 then
  468. -- body
  469. temp = "0001"
  470. elseif i == 601 then
  471. -- body
  472. local csq = math.floor( (net.getRssi() / 31 * 100) )
  473. temp = string.format("%04x", csq)
  474. elseif i == 602 then
  475. temp = string.format("%04x", 0)
  476. elseif i == 610 then
  477. temp = string.format("%04x", dateObj.year)
  478. elseif i == 611 then
  479. temp = string.format("%04x", dateObj.month)
  480. elseif i == 612 then
  481. temp = string.format("%04x", dateObj.day)
  482. elseif i == 613 then
  483. temp = string.format("%04x", dateObj.hour)
  484. elseif i == 614 then
  485. temp = string.format("%04x", dateObj.min)
  486. elseif i == 615 then
  487. temp = string.format("%04x", dateObj.sec)
  488. else
  489. temp = "0000"
  490. end
  491. netWorkDeviceData = netWorkDeviceData .. temp
  492. end
  493. local dataVersion = getAscii2StringHexBe(nvm.get("localCntVersion"), 20)
  494. local updateTime = getAscii2StringHexBe(nvm.get("localCntUpdateTime"), 20)
  495. local imei = getAscii2StringHexBe(misc.getImei(), 20)
  496. return netWorkDeviceData .. dataVersion ..updateTime ..imei
  497. end
  498. -- 工具可以自定义
  499. -- 机位号
  500. -- 所属部件
  501. -- 工作位置
  502. -- 设置查询条件相关寄存器数据
  503. function setQueryCondRegData( startRegAddress, data )
  504. -- body
  505. if queryCondReg and tonumber(startRegAddress, 16) <= tonumber(queryCondReg, 16) then
  506. -- body
  507. initRegVariable()
  508. log.info("queryReg variable init!")
  509. end
  510. queryCondReg = startRegAddress
  511. local byteCount = string.format("%02x",data:byte(7))
  512. table.insert(queryCondRegValue, data:sub(8, tonumber(byteCount, 16)+7))
  513. log.info("queryCondRegValue first:", queryCondRegValue[1]:toHex() )
  514. end
  515. -- 设置查询标识寄存器数据
  516. function setQueryFlagData( startRegAddress, data )
  517. -- body
  518. -- queryFlagReg = string.format("%02x",data:byte(8)) .. string.format("%02x",data:byte(9))
  519. if startRegAddress then
  520. -- body
  521. sys.publish("pub_queryflag_reg", startRegAddress)
  522. end
  523. log.info("quryflag wrote done:", startRegAddress)
  524. end
  525. -- crc16校验
  526. function crc16Compare( wholeData )
  527. -- body
  528. -- local crc16get = string.format("%02x",wholeData:byte(-2))..string.format("%02x",wholeData:byte(-1))
  529. local crc16get = string.sub(wholeData, -4 )
  530. local dataExceptCrc =string.sub(wholeData, 1, -5 )
  531. log.info("dataExceptCrc :", dataExceptCrc)
  532. local caculateCrc = pack.pack('<h', crypto.crc16("MODBUS",dataExceptCrc:fromHex()))
  533. if crc16get:fromHex() ~= caculateCrc then
  534. -- body
  535. log.info("crc16 compare, crc16get =",crc16get,",caculateCrc=",string.toHex(caculateCrc) )
  536. return false
  537. end
  538. return true
  539. end
  540. -- 寄存器相关变量初始化
  541. function initRegVariable( )
  542. -- body
  543. queryCondReg = nil
  544. queryCondRegValue = {} --
  545. queryFlagReg["0064"] = "0000"
  546. queryFlagReg["0065"] = "0000"
  547. queryFlagReg["0066"] = "0000"
  548. queryFlagReg["0067"] = "0000"
  549. queryFlagReg["0068"] = "0000"
  550. queryFlagReg["0069"] = "0000"
  551. queryFlagReg["006a"] = "0000"
  552. queryFlagReg["006b"] = "0000"
  553. queryFlagReg["006c"] = "0000"
  554. queryFlagReg["006d"] = "0000"
  555. queryFlagReg["006e"] = "0000"
  556. queryFlagReg["006f"] = "0000"
  557. queryFlagReg["0070"] = "0000"
  558. queryFlagResReg["00c8"] = "0000"
  559. queryFlagResReg["00c9"] = "0000"
  560. queryFlagResReg["00ca"] = "0000"
  561. queryFlagResReg["00cb"] = "0000"
  562. queryFlagResReg["00cc"] = "0000"
  563. queryFlagResReg["00cd"] = "0000"
  564. queryFlagResReg["00ce"] = "0000"
  565. queryFlagResReg["00cf"] = "0000"
  566. queryFlagResReg["00d0"] = "0000"
  567. queryFlagResReg["00d1"] = "0000"
  568. queryFlagResReg["00d2"] = "0000"
  569. queryFlagResReg["00d3"] = "0000"
  570. queryFlagResReg["00d4"] = "0000" -- 1400
  571. queryRespData = {}
  572. end
  573. local function modbus_read()
  574. local cacheData = ""
  575. while true do
  576. local s = uart.read(uart_id,1)
  577. if s == "" then
  578. -- if not sys.waitUntil("UART_RECEIVE",35000/uart_baud) then
  579. if not sys.waitUntil("UART_RECEIVE",35000/uart_baud) then
  580. -- 3.5个字符的时间间隔,只是用在RTU模式下面,因为RTU模式没有开始符和结束符,
  581. -- 两个数据包之间只能靠时间间隔来区分,Modbus定义在不同的波特率下,间隔时间是不一样的,
  582. -- 所以就是3.5个字符的时间,波特率高,这个时间间隔就小,波特率低,这个时间间隔相应就大
  583. -- 4800 = 7.297ms
  584. -- 9600 = 3.646ms
  585. -- 19200 = 1.771ms
  586. -- 38400 = 0.885ms
  587. --uart接收数据,如果 35000/uart_baud 毫秒没有收到数据,则打印出来所有已收到的数据,清空数据缓冲区,等待下次数据接收
  588. --注意:
  589. --因为在整个GSM模块软件系统中,软件定时器的精确性无法保证,例如本demo配置的是100毫秒,在系统繁忙时,实际延时可能远远超过100毫秒,达到200毫秒、300毫秒、400毫秒等
  590. --设置的延时时间越短,误差越大
  591. if cacheData:len()>0 then
  592. local a,_ = string.toHex(cacheData)
  593. logModuel.debug_log("read 485:"..a)
  594. --用户逻辑处理代码
  595. --
  596. local resp = proc(cacheData)
  597. cacheData = ""
  598. end
  599. end
  600. else
  601. cacheData = cacheData..s
  602. end
  603. end
  604. end
  605. local function writeOk()
  606. log.info("testUart.writeOk")
  607. end
  608. --10 写多个寄存器
  609. local function writeMultiReg( slaveaddr, functionCode, startAddress, regNumber, values )
  610. -- body
  611. local byteCount = 2*regNumber
  612. local prefix = (string.format("%02x",slaveaddr)..string.format("%02x",functionCode)..string.format("%04x",startAddress)..string.format("%04x",regNumber)..string.format("%04x",byteCount)):fromHex()
  613. local valueHex = ""
  614. for i=1,math.ceil(values:len()/4) do
  615. local t = "0x"..string.sub(values, (i-1)*4+1, 4*i)
  616. valueHex = valueHex.. string.format("%04x", t)
  617. end
  618. local data = prefix..valueHex:fromHex()
  619. local modbus_crc_data= pack.pack('<h', crypto.crc16("MODBUS",data))
  620. local data_tx = data..modbus_crc_data
  621. log.info("uart2Task.write",data_tx)
  622. uart.write(uart_id,data_tx)
  623. end
  624. --06 写单个寄存器
  625. local function writeSingleReg( slaveaddr, functionCode, regAddress, value )
  626. -- body
  627. local data = (string.format("%02x",slaveaddr)..string.format("%02x",functionCode)..string.format("%04x",regAddress)..string.format("%04x",value)):fromHex()
  628. local modbus_crc_data= pack.pack('<h', crypto.crc16("MODBUS",data))
  629. local data_tx = data..modbus_crc_data
  630. log.info("uart2Task.write",data_tx)
  631. uart.write(uart_id,data_tx)
  632. end
  633. -- 查询扳手信息并写入结果寄存器
  634. local function query0064Res()
  635. -- body
  636. respRegAddress = "00c8"
  637. local conds = table.concat(queryCondRegValue)
  638. if #conds < 20 then
  639. -- body
  640. queryFlagReg["0064"] = "0000"
  641. return false
  642. end
  643. local number = getByte2AsciiStr(conds:sub(1, 20))--查询条件
  644. local result, count, desc = getBaseInfo("Wrench", {number=number} , "/sdcard0/common")
  645. queryFlagReg["0064"] = "0000"
  646. queryCondRegValue = {}--清空查询条件
  647. if not result then
  648. -- body
  649. queryFlagResReg[respRegAddress] = "0002"
  650. log.info("查询扳手信息失败, desc:", desc)
  651. logModuel.debug_log("查询扳手信息失败,count:"..count..",desc:"..desc..",querycond_number:"..number)
  652. return false
  653. end
  654. local singleRes = result[1]
  655. local minnj = pack.pack('>I', singleRes.mint)
  656. local maxnj = pack.pack('>I', singleRes.maxt)
  657. local fbl = pack.pack('>I', singleRes.angres)
  658. local resT = {
  659. string.format("%04x", singleRes.qualified),
  660. string.format("%04x", singleRes.type),
  661. string.format("%04x", singleRes.minp),
  662. string.format("%04x", singleRes.maxp),
  663. minnj:sub(3, 4):toHex() .. minnj:sub(1, 2):toHex(),
  664. maxnj:sub(3, 4):toHex() .. maxnj:sub(1, 2):toHex(),
  665. fbl:sub(3, 4):toHex() .. fbl:sub(1, 2):toHex(),
  666. getAscii2StringHexBe(singleRes.clt, 20),
  667. getAscii2StringHexBe(singleRes.model, 20),
  668. getAscii2StringHexBe(singleRes.fixed, 20)
  669. -- 线性方程系数和常数
  670. -- pack.pack(">f", singleRes.eq_a):toHex(),
  671. -- pack.pack(">f", singleRes.eq_b):toHex()
  672. }
  673. local res64Hex = table.concat(resT)
  674. local j = 1
  675. for i=1400,1439 do
  676. local regHex = string.format("%04x", i)
  677. queryRespData[regHex] = string.sub(res64Hex, j, j+3)
  678. j = j+4
  679. end
  680. log.info("queryRespData lenth:", table_leng(queryRespData))
  681. queryFlagResReg[respRegAddress] = "0001"
  682. end
  683. -- 查询法兰历史紧固数据并写入结果寄存器
  684. local function query0065Res()
  685. -- body
  686. respRegAddress = "00c9"
  687. local conds = table.concat(queryCondRegValue)
  688. if #conds < 90 then
  689. -- body
  690. queryFlagReg["0065"] = "0000"
  691. return false
  692. end
  693. local cond = {
  694. wnum = getByte2AsciiStr(conds:sub(1, 10)),
  695. fnum = getByte2AsciiStr(conds:sub(11, 30)),
  696. fmodel = getByte2AsciiStr(conds:sub(31, 50)),
  697. parts = ucs2deleteZero(conds:sub(51, 70)),
  698. work = ucs2deleteZero(conds:sub(71, 110))
  699. }
  700. log.info("wnum:", cond.wnum, "wnum_hex:", conds:sub(1, 10):toHex())
  701. log.info("record cond:",json.encode(cond))
  702. local result, count, desc = getBaseInfo("WorkRecord", cond , "/sdcard0/"..cond.wnum .."/".. cond.fnum)
  703. queryFlagReg["0065"] = "0000"
  704. queryCondRegValue = {}--清空查询条件
  705. if not result then
  706. -- body
  707. queryFlagResReg[respRegAddress] = "0002"
  708. logModuel.debug_log("法兰历史紧固记录查询失败,count:"..count..",desc:"..desc..",querycond:"..json.encode(cond))
  709. return false
  710. end
  711. -- WorkRecord
  712. local res = result[1]
  713. local regValueHex = ""
  714. local total = string.format("%04x", res.bnum)
  715. local lsnj = pack.pack('>I', res.torque)
  716. regValueHex = total.. lsnj:sub(3, 4):toHex() .. lsnj:sub(1, 2):toHex()
  717. -- queryRespData["057c"] = pack.pack('>I', 100)
  718. -- queryRespData["057d"] = pack.pack('>I', 500)
  719. local j = 1
  720. for i=1400,1402 do
  721. local regHex = string.format("%04x", i)
  722. queryRespData[regHex] = regValueHex:sub(j, j+3)
  723. j = j+4
  724. end
  725. queryFlagResReg[respRegAddress] = "0001"
  726. end
  727. -- 用户查询认证
  728. local function query0066Res()
  729. -- body
  730. respRegAddress = "00ca"
  731. -- "0001" 用户名错误 "0002" 密码错误
  732. -- "000a" 操作员 "000b" 管理员
  733. local conds = table.concat(queryCondRegValue)
  734. if #conds < 40 then
  735. -- body
  736. queryFlagReg["0066"] = "0000"
  737. return false
  738. end
  739. local username = getByte2AsciiStr(conds:sub(1, 20))--查询条件--用户名
  740. local password = getByte2AsciiStr(conds:sub(21, 40))--查询条件--密码
  741. local ures, count, desc = getBaseInfo("Users", {name=username} , "/sdcard0/common")
  742. queryFlagReg["0066"] = "0000"
  743. queryCondRegValue = {}--清空查询条件
  744. if not ures then
  745. -- body
  746. logModuel.debug_log("用户名查询失败,count:"..count..",desc:"..desc..",querycond:"..json.encode({name=username}))
  747. queryFlagResReg[respRegAddress] = "0001"
  748. return false
  749. end
  750. local result, rcount, rdesc = getBaseInfo("Users", {name=username, pwd=password} , "/sdcard0/common")
  751. if not result then
  752. -- body
  753. logModuel.debug_log("用户查询认证失败,密码不匹配,count:"..rcount..",desc:"..rdesc..",querycond:"..json.encode({name=username, pwd=password}))
  754. queryFlagResReg[respRegAddress] = "0002"
  755. return false
  756. end
  757. queryFlagResReg[respRegAddress] = string.format("%04x", result[1].perm)
  758. end
  759. -- 查询风场列表并写入结果寄存器
  760. local function query0067Res()
  761. -- body
  762. respRegAddress = "00cb"
  763. local result, count, desc = getBaseInfo_ununique("Wind", nil, "/sdcard0/common")
  764. queryFlagReg["0067"] = "0000"
  765. queryCondRegValue = {}--清空查询条件
  766. local lastWorkField = nvm.get("lastWorkField")
  767. if not result and lastWorkField == "" then
  768. -- body
  769. queryFlagResReg[respRegAddress] = "0002"
  770. logModuel.debug_log("查询风场列表失败,count:"..count..",desc:"..desc)
  771. return false
  772. end
  773. local fitWindTable, gpsLng, gpsLat, gpsTime = {}, nvm.get("gpsLng"), nvm.get("gpsLat"), nvm.get("gpsTime")
  774. local fieldDisplayRadius = nvm.get("fieldDisplayRadius") or 20
  775. print("aaaaaaaaaaaa")
  776. log.info("lastWorkField",lastWorkField)
  777. if not result then
  778. --无全风场列表显示上次作业风场
  779. -- body
  780. print("bbbbbbbbbbb")
  781. result = getBaseInfo("Wind", {number=lastWorkField}, "/sdcard0/common")
  782. else
  783. if gpsTime == 0 or gpsTime == "0" then
  784. --有风场列表,但GPS未定位成功过显示上次作业风场
  785. -- body
  786. print("cccccccccccccc")
  787. if lastWorkField ~= "" then
  788. -- body
  789. print("dddddddddddddd")
  790. result = getBaseInfo("Wind", {number=lastWorkField}, "/sdcard0/common")
  791. end
  792. else
  793. print("eeeeeeeeeeeeeee")
  794. for k,v in pairs(result) do
  795. --过滤风场列表中不在方圆20Km内的风场
  796. local dist = devTool.getDistance( v.lat, v.lon, gpsLat, gpsLng)
  797. log.info("dist:",dist)
  798. if dist < fieldDisplayRadius then
  799. -- body
  800. -- log.info("cicle",v.lat)
  801. table.insert(fitWindTable, v)
  802. end
  803. end
  804. if table_leng(fitWindTable) == 0 then
  805. --若过滤后无风场则显示上次作业风场
  806. -- body
  807. print("fffffffffffffffff")
  808. if lastWorkField ~= "" then
  809. -- body
  810. print("ggggggggggggggggggg")
  811. result = getBaseInfo("Wind", {number=lastWorkField}, "/sdcard0/common")
  812. end
  813. else
  814. print("hhhhhhhhhhhhhhhhhhhhhhhh")
  815. result = fitWindTable
  816. log.info("fittable:", json.encode(fitWindTable))
  817. end
  818. end
  819. end
  820. print("iiiiiiiiiiiiiiiiiii")
  821. if not result then
  822. -- body
  823. print("jjjjjjjjjjjjjjj")
  824. queryFlagResReg[respRegAddress] = "0002"
  825. logModuel.debug_log("查询风场列表失败过滤逻辑之后")
  826. return false
  827. end
  828. queryRespData["0578"] = string.format("%04x", table_leng(result))
  829. local regValueHex = ""
  830. for k,v in pairs(result) do
  831. local numberHex = getAscii2StringHexBe(v.number, 10)
  832. local ucs2Bytes = common.utf8ToUcs2be(v.name)
  833. local afterHandleNameByte = ""
  834. if #ucs2Bytes > 40 then
  835. -- body
  836. ucs2Bytes = ucs2Bytes:sub(1, 40)
  837. end
  838. local nameHex = ucs2Bytes:toHex()..getAscii2StringHexBe("\0", 40-#ucs2Bytes)
  839. log.info("number:",numberHex, "name:",nameHex)
  840. regValueHex = regValueHex..numberHex..nameHex
  841. end
  842. local j = 1
  843. for i=1410, 1409 + 25*table_leng(result) do
  844. local regHex = string.format("%04x", i)
  845. queryRespData[regHex] = regValueHex:sub(j, j+3)
  846. j = j+4
  847. end
  848. queryFlagResReg[respRegAddress] = "0001"
  849. end
  850. -- 查询风机型号列表并写入结果寄存器
  851. local function query0068Res()
  852. -- body
  853. respRegAddress = "00cc"
  854. local conds = table.concat(queryCondRegValue)
  855. if #conds < 10 then
  856. -- body
  857. queryFlagReg["0068"] = "0000"
  858. return false
  859. end
  860. local wnum = getByte2AsciiStr(conds:sub(1, 10)) --查询条件
  861. local result, count, desc = getBaseInfo_ununique("FanModel", {wnum=wnum} , "/sdcard0/"..wnum)
  862. queryFlagReg["0068"] = "0000"
  863. queryCondRegValue = {}--清空查询条件
  864. if not result then
  865. -- body
  866. queryFlagResReg[respRegAddress] = "0002"
  867. logModuel.debug_log("查询风机型号列表失败,count:"..count..",desc:"..desc..",querycond_wnum:"..wnum)
  868. return false
  869. end
  870. queryRespData["0578"] = string.format("%04x", table_leng(result))
  871. local regHexValue = ""
  872. for k, v in pairs(result) do
  873. regHexValue = regHexValue .. getAscii2StringHexBe(v.model, 20)
  874. end
  875. local j = 1
  876. for i=1410,1409 + table_leng(result) * 10 do
  877. local regHex = string.format("%04x", i)
  878. queryRespData[regHex] = string.sub(regHexValue, j, j+3)
  879. j = j+4
  880. end
  881. queryFlagResReg[respRegAddress] = "0001"
  882. end
  883. -- 查询风机机位号列表并写入结果寄存器
  884. local function query0069Res()
  885. -- body
  886. respRegAddress = "00cd"
  887. local conds = table.concat(queryCondRegValue)
  888. if #conds < 10 then
  889. -- body
  890. queryFlagReg["0069"] = "0000"
  891. return false
  892. end
  893. local wnum = getByte2AsciiStr(conds:sub(1, 10)) --查询条件
  894. -- local result, count, desc = getBaseInfo("Fan", {wnum=wnum} , "/sdcard0/"..wnum)
  895. local result, count, desc = getBaseInfo_ununique("Fan", {wnum=wnum} , "/sdcard0/"..wnum)
  896. queryFlagReg["0069"] = "0000"
  897. queryCondRegValue = {}--清空查询条件
  898. if not result then
  899. -- body
  900. queryFlagResReg[respRegAddress] = "0002"
  901. logModuel.debug_log("查询机位列表失败,count:"..count..",desc:"..desc..",querycond_number:"..wnum)
  902. return false
  903. end
  904. queryRespData["0578"] = string.format("%04x", table_leng(result))
  905. local regValueHex = ""
  906. for k,v in pairs(result) do
  907. local number = v.number
  908. if type(number) =="number" then
  909. -- body
  910. number = tostring(number)
  911. end
  912. local numberHex = getAscii2StringHexBe(number, 20)
  913. regValueHex = regValueHex..numberHex
  914. end
  915. local j = 1
  916. for i=1410, 1409 + 10*table_leng(result) do
  917. local regHex = string.format("%04x", i)
  918. queryRespData[regHex] = regValueHex:sub(j, j+3)
  919. j = j+4
  920. end
  921. queryFlagResReg[respRegAddress] = "0001"
  922. end
  923. -- 查询部件列表并写入结果寄存器
  924. local function query006aRes()
  925. -- body
  926. respRegAddress = "00ce"
  927. local result, count, desc = getBaseInfo_ununique("WorkingParts", nil, "/sdcard0/common")
  928. queryFlagReg["006a"] = "0000"
  929. queryCondRegValue = {}--清空查询条件
  930. if not result then
  931. -- body
  932. queryFlagResReg[respRegAddress] = "0002"
  933. logModuel.debug_log("查询部件列表失败,count:"..count..",desc:"..desc)
  934. return false
  935. end
  936. -- local result = {
  937. -- {id=123, name="测试部件1"},
  938. -- {id=124, name="测试部件2"},
  939. -- {id=125, name="测试部件3"}
  940. -- }
  941. queryRespData["0578"] = string.format("%04x", table_leng(result))
  942. local regValueHex = ""
  943. for k,v in pairs(result) do
  944. local ucs2Bytes = common.utf8ToUcs2be(v.name)
  945. local nameHex = ucs2Bytes:toHex()..getAscii2StringHexBe("\0", 20-#ucs2Bytes)
  946. regValueHex = regValueHex..nameHex
  947. end
  948. local j = 1
  949. for i=1410, 1409 + 10*table_leng(result) do
  950. local regHex = string.format("%04x", i)
  951. queryRespData[regHex] = regValueHex:sub(j, j+3)
  952. j = j+4
  953. end
  954. queryFlagResReg[respRegAddress] = "0001"
  955. end
  956. -- 查询工作位置并写入结果寄存器
  957. local function query006bRes()
  958. respRegAddress = "00cf"
  959. -- query body
  960. local conds = table.concat(queryCondRegValue)
  961. if #conds < 40 then
  962. -- body
  963. queryFlagReg["006b"] = "0000"
  964. return false
  965. end
  966. local fmdel = getByte2AsciiStr(conds:sub(1, 20))
  967. local ucs2Byte = conds:sub(21, 40)
  968. local Parts = ucs2deleteZero(ucs2Byte)
  969. local result, count, desc = getBaseInfo_ununique("WorkingPosition", { fmodel=fmdel, Parts=Parts }, "/sdcard0/common")
  970. queryFlagReg["006b"] = "0000"
  971. queryCondRegValue = {}--清空查询条件
  972. if not result then
  973. -- body
  974. queryFlagResReg[respRegAddress] = "0002"
  975. logModuel.debug_log("查询工作位置列表失败,count:"..count..",desc:"..desc..",querycond:"..json.encode({ fmodel=fmdel, Parts=Parts }))
  976. return false
  977. end
  978. queryRespData["0578"] = string.format("%04x", table_leng(result))
  979. local regValueHex = ""
  980. for k,v in pairs(result) do
  981. local ucs2Bytes = common.utf8ToUcs2be(v.name)
  982. local nameHex = ucs2Bytes:toHex()..getAscii2StringHexBe("\0", 40-#ucs2Bytes)
  983. regValueHex = regValueHex..nameHex
  984. end
  985. local j = 1
  986. for i=1410, 1409 + 20*table_leng(result) do
  987. local regHex = string.format("%04x", i)
  988. queryRespData[regHex] = regValueHex:sub(j, j+3)
  989. j = j+4
  990. end
  991. queryFlagResReg[respRegAddress] = "0001"
  992. end
  993. -- 查询自定义的3各文件中是否存在,不存在则新增一个
  994. local function queryCustomAndAdd( fanNumber, part, workPos , pt )
  995. -- body
  996. local fanExist = getBaseInfo("Fan", {wnmu=pt.WindFieldNumber, number=fanNumber,model=pt.Model}, "/sdcard0/"..pt.WindFieldNumber)
  997. if not fanExist then
  998. -- body
  999. saveBaseInfo("Fan",{id=0,wnmu=pt.WindFieldNumber, number=fanNumber,model=pt.Model}, "/sdcard0/"..pt.WindFieldNumber)
  1000. end
  1001. local partExist = getBaseInfo("WorkingParts", {name=part}, "/sdcard0/common")
  1002. if not partExist then
  1003. -- body
  1004. saveBaseInfo("WorkingParts",{id=0,name=part}, "/sdcard0/common")
  1005. end
  1006. local workPosExist = getBaseInfo("WorkingPosition", {Parts=part,fmodel=pt.Model, name=workPos}, "/sdcard0/common")
  1007. if not workPosExist then
  1008. -- body
  1009. saveBaseInfo("WorkingPosition",{id=0,Parts=part,fmodel=pt.Model, name=workPos}, "/sdcard0/common")
  1010. end
  1011. end
  1012. -- 上传紧固模式操作记录
  1013. local function query006cRes( )
  1014. respRegAddress = "00d0"
  1015. local conds = table.concat(queryCondRegValue)
  1016. if #conds < 180 then
  1017. -- body
  1018. queryFlagReg["006c"] = "0000"
  1019. return false
  1020. end
  1021. local msgid = devTool.guid()
  1022. local upTable = {
  1023. Username =getByte2AsciiStr(conds:sub(1, 20)), --用户名 string
  1024. WrenchNumber = getByte2AsciiStr(conds:sub(21, 40)), --扳手编号 string
  1025. WindFieldNumber = getByte2AsciiStr(conds:sub(41, 50)), --风场编号 string
  1026. TurbineNumber = getByte2AsciiStr(conds:sub(51, 70)), --机位号 string 自定义
  1027. Model = getByte2AsciiStr(conds:sub(71, 90)), --风机型号 string
  1028. -- PartNumber = common.ucs2beToUtf8(conds:sub(91, 110)), --部件号 Unicode 自定义
  1029. PartNumber = ucs2deleteZero(conds:sub(91, 110)), --部件号 Unicode 自定义
  1030. -- WorkPos = common.ucs2beToUtf8(conds:sub(111, 130)), --工作位置 Unicode 自定义
  1031. WorkPos = ucs2deleteZero(conds:sub(111, 150)), --工作位置 Unicode 自定义
  1032. BoltModel = getByte2AsciiStr(conds:sub(151, 160)), --螺栓型号 string
  1033. BoltTotal = tonumber(conds:sub(161, 162):toHex(), 16), --螺栓总数 u16
  1034. BoltNumber = tonumber(conds:sub(163, 164):toHex(), 16), --当前螺栓编号 u16
  1035. SetTorque = tonumber(conds:sub(167,168):toHex()..conds:sub(165,166):toHex(), 16), --设定扭矩Nm u32
  1036. FasteningTorque = tonumber(conds:sub(171,172):toHex()..conds:sub(169,170):toHex(), 16), --紧固扭矩Nm u32
  1037. SetStress = tonumber(conds:sub(173,174):toHex(), 16), --设定压力bar u16
  1038. FasteningStress = tonumber(conds:sub(175,176):toHex(), 16), --紧固压力bar u16
  1039. FasteningStatus = tonumber(conds:sub(177,178):toHex(), 16), --紧固状态 u16
  1040. Time = tonumber(conds:sub(179,180):toHex(), 16), --紧固时间 u16
  1041. netWorkSaveTime = os.time(),
  1042. msgid = msgid,
  1043. id = msgid
  1044. }
  1045. -- 查询自定义的3字段是否存在,不存在则新增一个
  1046. queryCustomAndAdd(upTable.TurbineNumber, upTable.PartNumber, upTable.WorkPos, {Model=upTable.Model, WindFieldNumber = upTable.WindFieldNumber})
  1047. -- 待上传 upTable 全部
  1048. -- if socket.isReady() then
  1049. -- -- body
  1050. -- sys.publish("LOCAL_PUB_MSG", json.encode({["topic"]="SHEGCL/IntelligenTool/UploadWorkRecordMA",["data"]=upTable}))
  1051. -- local uploadRes,msgid = sys.waitUntil("WAIT_UPDATA_RESP_SUCCESS")
  1052. -- else
  1053. saveBaseInfo("wait2upload", {["topic"]="SHEGCL/IntelligenTool/UploadWorkRecordMA",["data"]=upTable,["id"]=msgid}, "/sdcard0/common/temp")
  1054. -- end
  1055. -- 工作记录表 boltTotal, fastenNm 风场编号+机位号+风机型号+部件号+工作位置
  1056. local localRecord = {
  1057. id = upTable.WindFieldNumber..upTable.TurbineNumber..upTable.Model..upTable.PartNumber..upTable.WorkPos,
  1058. wnum = upTable.WindFieldNumber,
  1059. fnum = upTable.TurbineNumber,
  1060. fmodel = upTable.Model,
  1061. parts = upTable.PartNumber,
  1062. work = upTable.WorkPos,
  1063. bnum = upTable.BoltTotal,
  1064. torque = upTable.FasteningTorque
  1065. }
  1066. log.info("local record", json.encode(localRecord))
  1067. local result,count, desc = updateBaseInfo("WorkRecord", localRecord, "/sdcard0/"..localRecord.wnum .."/".. localRecord.fnum)
  1068. if not result or desc:sub(1, 29) == "this data not found where id=" then
  1069. -- body
  1070. result, desc = saveBaseInfo("WorkRecord", localRecord, "/sdcard0/"..localRecord.wnum .."/".. localRecord.fnum )
  1071. end
  1072. log.info("local result", result)
  1073. queryFlagReg["006c"] = "0000"
  1074. if not result then
  1075. -- body
  1076. logModuel.debug_log("上传紧固模式操作记录--写本地工作记录文件失败, desc:"..desc)
  1077. queryFlagResReg[respRegAddress] = "0002"
  1078. else
  1079. nvm.set("lastWorkField", localRecord.wnum)
  1080. queryFlagResReg[respRegAddress] = "0001" --结果置1表示操作成功
  1081. end
  1082. end
  1083. -- 上传维护模式操作记录
  1084. local function query006dRes()
  1085. respRegAddress = "00d1"
  1086. local conds = table.concat(queryCondRegValue)
  1087. if #conds < 162 then
  1088. -- body
  1089. queryFlagReg["006d"] = "0000"
  1090. return false
  1091. end
  1092. local msgid = devTool.guid()
  1093. local upTable = {
  1094. Username =getByte2AsciiStr(conds:sub(1, 20)), --用户名 string
  1095. WrenchNumber = getByte2AsciiStr(conds:sub(21, 40)), --扳手编号 string
  1096. WindFieldNumber = getByte2AsciiStr(conds:sub(41, 50)), --风场编号 string
  1097. TurbineNumber = getByte2AsciiStr(conds:sub(51, 70)), --机位号 string 自定义
  1098. Model = getByte2AsciiStr(conds:sub(71, 90)), --风机型号 string
  1099. -- PartNumber = common.ucs2beToUtf8(conds:sub(91, 110)), --部件号 Unicode 自定义
  1100. PartNumber = ucs2deleteZero(conds:sub(91, 110)), --部件号 Unicode 自定义
  1101. -- WorkPos = common.ucs2beToUtf8(conds:sub(111, 130)), --工作位置 Unicode 自定义
  1102. WorkPos = ucs2deleteZero(conds:sub(111, 150)), --工作位置 Unicode 自定义
  1103. BoltModel = getByte2AsciiStr(conds:sub(151, 160)), --螺栓型号 string
  1104. BoltTotal = tonumber(conds:sub(161, 162):toHex(), 16), --螺栓总数 u16
  1105. BoltNumber = tonumber(conds:sub(163, 164):toHex(), 16), --当前螺栓编号 u16
  1106. SetTorque = tonumber(conds:sub(167,168):toHex()..conds:sub(165,166):toHex(), 16), --设定扭矩Nm u32
  1107. FasteningTorque = tonumber(conds:sub(171,172):toHex()..conds:sub(169,170):toHex(), 16), --紧固扭矩Nm u32
  1108. SetStress = tonumber(conds:sub(173,174):toHex(), 16), --设定压力bar u16
  1109. FasteningStress = tonumber(conds:sub(175,176):toHex(), 16), --紧固压力bar u16
  1110. FasteningStatus = tonumber(conds:sub(177,178):toHex(), 16), --紧固状态 u16
  1111. Time = tonumber(conds:sub(179,180):toHex(), 16), --紧固时间 u16
  1112. WrenchAngle = tonumber(conds:sub(181,182):toHex(), 16), --扳手转动角度 u16
  1113. netWorkSaveTime = os.time(),
  1114. msgid = msgid,
  1115. id = msgid
  1116. }
  1117. -- 查询自定义的3字段是否存在,不存在则新增一个
  1118. queryCustomAndAdd(upTable.TurbineNumber, upTable.PartNumber, upTable.WorkPos, {Model=upTable.Model, WindFieldNumber = upTable.WindFieldNumber})
  1119. -- 待上传 upTable 全部
  1120. -- if socket.isReady() and (net.getRssi() > 14) then
  1121. -- -- body
  1122. -- sys.publish("LOCAL_PUB_MSG", json.encode({["topic"]="SHEGCL/IntelligenTool/UploadWorkRecordFA",["data"]=upTable}))
  1123. -- else
  1124. saveBaseInfo("wait2upload", {["topic"]="SHEGCL/IntelligenTool/UploadWorkRecordFA",["data"]=upTable,["id"]=msgid}, "/sdcard0/common/temp")
  1125. -- end
  1126. -- 工作记录表 boltTotal, fastenNm 风场编号+机位号+风机型号+部件号+工作位置
  1127. local localRecord = {
  1128. id = upTable.WindFieldNumber..upTable.TurbineNumber..upTable.Model..upTable.PartNumber..upTable.WorkPos,
  1129. wnum = upTable.WindFieldNumber,
  1130. fnum = upTable.TurbineNumber,
  1131. fmodel = upTable.Model,
  1132. parts = upTable.PartNumber,
  1133. work = upTable.WorkPos,
  1134. bnum = upTable.BoltTotal,
  1135. torque = upTable.FasteningTorque
  1136. }
  1137. log.info("local record", json.encode(localRecord))
  1138. local result,count, desc = updateBaseInfo("WorkRecord", localRecord, "/sdcard0/"..localRecord.wnum )
  1139. if not result and desc:sub(1, 29) == "this data not found where id=" then
  1140. -- body
  1141. log.info("oooooooooooooooo")
  1142. result, desc = saveBaseInfo("WorkRecord", localRecord, "/sdcard0/"..localRecord.wnum )
  1143. end
  1144. log.info("local result", result)
  1145. queryFlagReg["006d"] = "0000"
  1146. if not result then
  1147. -- body
  1148. queryFlagResReg[respRegAddress] = "0002"
  1149. logModuel.debug_log("上传维护模式操作记录--写本地工作记录文件失败, desc:"..desc)
  1150. else
  1151. nvm.set("lastWorkField", localRecord.wnum)
  1152. queryFlagResReg[respRegAddress] = "0001" --结果置1表示操作成功
  1153. end
  1154. end
  1155. -- 上传维护模式操作记录
  1156. local function query006eRes()
  1157. respRegAddress = "00d2"
  1158. local conds = table.concat(queryCondRegValue)
  1159. if #conds < 24 then
  1160. -- body
  1161. queryFlagReg["006e"] = "0000"
  1162. return false
  1163. end
  1164. local upTable = {
  1165. Username =getByte2AsciiStr(conds:sub(1, 20)), --用户名 string
  1166. SetStress = tonumber(conds:sub(21,22):toHex(), 16), --设定压力bar u16
  1167. FasteningStress = tonumber(conds:sub(23,24):toHex(), 16) --紧固压力bar u16
  1168. }
  1169. -- 待上传 upTable 全部
  1170. local result
  1171. if socket.isReady() then
  1172. -- body
  1173. sys.publish("LOCAL_PUB_MSG", json.encode({["topic"]="SHEGCL/IntelligenTool/UploadWorkRecordSimple",["data"]=upTable}))
  1174. result = true
  1175. else
  1176. result = saveBaseInfo(" ", {["topic"]="SHEGCL/IntelligenTool/UploadWorkRecordSimple",["data"]=upTable}, "/sdcard0/common")
  1177. end
  1178. queryFlagReg["006e"] = "0000"
  1179. if not result then
  1180. -- body
  1181. queryFlagResReg[respRegAddress] = "0002"
  1182. logModuel.debug_log("上传维护模式操作(仅次数)记录--写本地工作记录文件失败, desc:"..desc)
  1183. else
  1184. queryFlagResReg[respRegAddress] = "0001" --结果置1表示操作成功
  1185. end
  1186. end
  1187. -- 获取任务编号列表
  1188. local function query006fRes()
  1189. -- body
  1190. respRegAddress = "00d3"
  1191. local conds = table.concat(queryCondRegValue)
  1192. if #conds ~= 30 then
  1193. -- body
  1194. queryFlagReg["006f"] = "0000"
  1195. return false
  1196. end
  1197. local wind = getByte2AsciiStr(conds:sub(1, 10))--查询条件
  1198. local fmodel = getByte2AsciiStr(conds:sub(11, 30))--查询条件
  1199. local result, count, desc = getBaseInfo_ununique("WorkPlan", {wnum=wind,fmodel=fmodel}, "/sdcard0/common")
  1200. queryFlagReg["006f"] = "0000"
  1201. queryCondRegValue = {}--清空查询条件
  1202. if not result then
  1203. -- body
  1204. queryFlagResReg[respRegAddress] = "0002"
  1205. logModuel.debug_log("查询任务编号列表失败,count:"..count..",desc:"..desc)
  1206. return false
  1207. end
  1208. queryRespData["0578"] = string.format("%04x", table_leng(result))
  1209. local regValueHex = ""
  1210. for k,v in pairs(result) do
  1211. local pnum = getAscii2StringHexBe(v.pnum, 20)
  1212. local ucs2Bytes = common.utf8ToUcs2be(v.info)
  1213. local info = ucs2Bytes:toHex()..getAscii2StringHexBe("\0", 40-#ucs2Bytes)
  1214. log.info("pnum:", pnum, "info:", info)
  1215. regValueHex = regValueHex..pnum..info
  1216. end
  1217. local j = 1
  1218. for i=1410, 1409 + 30*table_leng(result) do
  1219. local regHex = string.format("%04x", i)
  1220. queryRespData[regHex] = regValueHex:sub(j, j+3)
  1221. j = j+4
  1222. end
  1223. queryFlagResReg[respRegAddress] = "0001"
  1224. end
  1225. -- 获取任务编号内容
  1226. local function query0070Res()
  1227. -- body
  1228. respRegAddress = "00d4"
  1229. queryFlagResReg[respRegAddress] = "0002"
  1230. local conds = table.concat(queryCondRegValue)
  1231. if #conds < 20 then
  1232. -- body
  1233. queryFlagReg["0070"] = "0000"
  1234. return false
  1235. end
  1236. local cond = getByte2AsciiStr(conds:sub(1,20))
  1237. local result, count, desc = getBaseInfo("WorkPlan", {pnum=cond}, "/sdcard0/common")
  1238. queryFlagReg["0070"] = "0000"
  1239. queryCondRegValue = {}--清空查询条件
  1240. if not result then
  1241. -- body
  1242. logModuel.debug_log("查询任务具体详情失败,count:"..count..",desc:"..desc,",cond_pnum="..cond)
  1243. return false
  1244. end
  1245. local singleRes = result[1]
  1246. local lt = pack.pack('>I', singleRes.lt)
  1247. local torque = pack.pack('>I', singleRes.torque)
  1248. local wname = common.utf8ToUcs2be(singleRes.wname)
  1249. local parts = common.utf8ToUcs2be(singleRes.parts)
  1250. local work = common.utf8ToUcs2be(singleRes.work)
  1251. local resT = {
  1252. getAscii2StringHexBe(singleRes.wnum, 10),
  1253. wname:toHex()..getAscii2StringHexBe("\0", 40-#wname),
  1254. getAscii2StringHexBe(singleRes.fnum, 20),
  1255. getAscii2StringHexBe(singleRes.fmodel, 20),
  1256. parts:toHex()..getAscii2StringHexBe("\0", 20-#parts),
  1257. work:toHex()..getAscii2StringHexBe("\0", 40-#work),
  1258. getAscii2StringHexBe(singleRes.bm, 20),
  1259. string.format("%04x", singleRes.bn),
  1260. string.format("%04x", singleRes.tm),
  1261. string.format("%04x", singleRes.bias),
  1262. lt:sub(3, 4):toHex() .. lt:sub(1, 2):toHex(),
  1263. torque:sub(3, 4):toHex() .. torque:sub(1, 2):toHex()
  1264. }
  1265. local resValueHex = table.concat(resT)
  1266. local j = 1
  1267. for i=1400,1491 do
  1268. local regHex = string.format("%04x", i)
  1269. queryRespData[regHex] = string.sub(resValueHex, j, j+3)
  1270. j = j+4
  1271. end
  1272. queryFlagResReg[respRegAddress] = "0001"
  1273. end
  1274. -- 获取扳手编号列表_模糊查询
  1275. local function query0071Res()
  1276. -- body
  1277. respRegAddress = "00d5"
  1278. local conds = table.concat(queryCondRegValue)
  1279. -- if #conds < 20 then
  1280. -- -- body
  1281. -- queryFlagReg["0064"] = "0000"
  1282. -- return false
  1283. -- end
  1284. local seachStr = getByte2AsciiStr(conds)--查询条件
  1285. local result, count, desc = getBaseInfo_ununique_dim("Wrench", {number = seachStr ,fixed = seachStr}, "/sdcard0/common")
  1286. queryFlagReg["0071"] = "0000"
  1287. queryCondRegValue = {}--清空查询条件
  1288. if not result then
  1289. -- body
  1290. queryFlagResReg[respRegAddress] = "0002"
  1291. logModuel.debug_log("查询扳手编号列表失败,count:"..count..",desc:"..desc)
  1292. return false
  1293. end
  1294. queryRespData["0578"] = string.format("%04x", table_leng(result))
  1295. local regValueHex = ""
  1296. for k,v in pairs(result) do
  1297. local number = getAscii2StringHexBe(v.number, 20)
  1298. log.info("number:", v.number)
  1299. regValueHex = regValueHex..number
  1300. end
  1301. local j = 1
  1302. for i=1410, 1409 + 10*table_leng(result) do
  1303. local regHex = string.format("%04x", i)
  1304. queryRespData[regHex] = regValueHex:sub(j, j+3)
  1305. j = j+4
  1306. end
  1307. queryFlagResReg[respRegAddress] = "0001"
  1308. end
  1309. -- 泵到网关心跳
  1310. function query0191Res()
  1311. -- body
  1312. if condition then
  1313. -- body
  1314. end
  1315. end
  1316. -- 网关到泵心跳
  1317. function query0258Res()
  1318. -- body
  1319. if condition then
  1320. -- body
  1321. end
  1322. end
  1323. local function queryByRegAdd(regAddress)
  1324. queryRespData = {}--清空之前的查询结果
  1325. if regAddress == "0064" then
  1326. -- body
  1327. query0064Res()
  1328. end
  1329. if regAddress == "0065" then
  1330. -- body
  1331. query0065Res()
  1332. end
  1333. if regAddress == "0066" then
  1334. -- body
  1335. query0066Res()
  1336. end
  1337. if regAddress == "0067" then
  1338. -- body
  1339. query0067Res()
  1340. end
  1341. if regAddress == "0068" then
  1342. -- body
  1343. query0068Res()
  1344. end
  1345. if regAddress == "0069" then
  1346. -- body
  1347. query0069Res()
  1348. end
  1349. if regAddress == "006a" then
  1350. -- body
  1351. query006aRes()
  1352. end
  1353. if regAddress == "006b" then
  1354. -- body
  1355. query006bRes()
  1356. end
  1357. if regAddress == "006c" then
  1358. -- body
  1359. query006cRes()
  1360. end
  1361. if regAddress == "006d" then
  1362. -- body
  1363. query006dRes()
  1364. end
  1365. if regAddress == "006e" then
  1366. -- body
  1367. query006eRes()
  1368. end
  1369. if regAddress == "006f" then
  1370. -- body
  1371. query006fRes()
  1372. end
  1373. if regAddress == "0070" then
  1374. -- body
  1375. query0070Res()
  1376. end
  1377. if regAddress == "0071" then
  1378. -- body
  1379. query0071Res()
  1380. end
  1381. end
  1382. local function queryByQueryflag()
  1383. while true do
  1384. local result, regAddress = sys.waitUntil("pub_queryflag_reg", 1000)
  1385. if result then
  1386. -- body
  1387. queryByRegAdd(regAddress)
  1388. end
  1389. end
  1390. end
  1391. pm.wake("modbusrtu")
  1392. --注册串口的数据发送通知函数
  1393. uart.on(uart_id,"receive",function() sys.publish("UART_RECEIVE") end)
  1394. uart.on(uart_id,"sent",writeOk)
  1395. --配置并且打开串口
  1396. -- uart.setup(uart_id,uart_baud,8,uart.PAR_NONE,uart.STOP_1)
  1397. uart.setup(uart_id,uart_baud,8,uart.PAR_NONE,uart.STOP_1,nil,1)
  1398. uart.set_rs485_oe(uart_id, pio.P0_19)
  1399. --启动串口数据接收任务
  1400. sys.taskInit(modbus_read)
  1401. sys.taskInit(queryByQueryflag)
  1402. function getHeartbeatContent( )
  1403. -- body
  1404. local gpsStr = nvm.get("gpsLng")..","..nvm.get("gpsLat")
  1405. -- local vbatt = readAdc() or "0"
  1406. local adcval,voltval = adc.read(BAT_ADC_ID)
  1407. local vbatt = math.ceil( 1.68 * voltval )
  1408. local ram = _G.collectgarbage("count")
  1409. local flash = rtos.get_fs_free_size(0,1)
  1410. -- local SDInfo = rtos.get_fs_free_size(1,1)
  1411. local Status = 0
  1412. if powerManage.setGpio11Fnc() == 1 then
  1413. -- body
  1414. Status = 1
  1415. end
  1416. local heartBeat = {
  1417. IMEI = misc.getImei(),
  1418. IMSI = sim.getImsi(),
  1419. CCID = sim.getIccid(),
  1420. CSQ = net.getRssi(),
  1421. GPS = gpsStr,
  1422. Status = Status,
  1423. lngType = nvm.get("lngType"),
  1424. latType = nvm.get("latType"),
  1425. LastGPSLocTime = nvm.get("gpsTime") ,
  1426. Power = vbatt,
  1427. RamInfo = ram..","..flash,
  1428. SoftVersion = _G.VERSION,
  1429. HeartTimeInterval = nvm.get("heartbeatInterval"),
  1430. CntVersion = nvm.get("localCntVersion"),
  1431. pumbStatus = nvm.get("pumbStatus"),
  1432. newPumbModel = nvm.get("newPumbModel"),
  1433. newPumbNumber = nvm.get("newPumbNumber"),
  1434. oldPumbModel = nvm.get("oldPumbModel"),
  1435. oldPumbNumber = nvm.get("oldPumbNumber"),
  1436. sensorNumber = nvm.get("sensorNumber"),
  1437. msgid = devTool.guid(),
  1438. time = os.time()
  1439. }
  1440. return heartBeat
  1441. end
  1442. local function checkPumbHeartbeatExtra( )
  1443. -- body
  1444. local pumbStatus = nvm.get("pumbStatus")
  1445. local hydOil, hydOilThr = nvm.get("hydOil"), nvm.get("hydOilThr")
  1446. local scaleValve, scaleValveThr = nvm.get("scaleValve"), nvm.get("scaleValveThr")
  1447. local elecValve, elecValveThr = nvm.get("elecValve"), nvm.get("elecValveThr")
  1448. local alarmType, alarmReason = "device", {}
  1449. if pumbStatus > 2 then
  1450. -- body
  1451. table.insert(alarmReason, "pumb_unusual_status")
  1452. end
  1453. if hydOil > hydOilThr then
  1454. -- body
  1455. table.insert(alarmReason, "low_oil")
  1456. end
  1457. if scaleValve > scaleValveThr then
  1458. -- body
  1459. table.insert(alarmReason, "low_scale_valve")
  1460. end
  1461. if elecValve > elecValveThr then
  1462. -- body
  1463. table.insert(alarmReason, "low_elec_valve")
  1464. end
  1465. if table_leng(alarmReason) == 0 then
  1466. -- body
  1467. return
  1468. end
  1469. local alarmData = {
  1470. Type = alarmType,
  1471. Reason = alarmReason,
  1472. Data = {pumbStatus=pumbStatus, hydOil= hydOil, scaleValve=scaleValve,elecValve=elecValve},
  1473. Time = os.time()
  1474. }
  1475. sys.publish("LOCAL_PUB_MSG", json.encode({["topic"]="SHEGCL/IntelligenTool/UploadAlarmInfo",["data"]=alarmData}))
  1476. end
  1477. function checkNetWorkHealth( msgType )
  1478. local alarmType, alarmReason = "gateway", {}
  1479. local adcval,voltval = adc.read(BAT_ADC_ID)
  1480. local vbatt = math.ceil( 1.68 * voltval )
  1481. local lowBatThd = nvm.get("lowBatThd") or 3300
  1482. if msgType then
  1483. -- body
  1484. table.insert(alarmReason, msgType)
  1485. else
  1486. if vbatt < lowBatThd then
  1487. -- body
  1488. table.insert(alarmReason, "network_bat_lower")
  1489. end
  1490. end
  1491. if table_leng(alarmReason) == 0 then
  1492. -- body
  1493. return
  1494. end
  1495. local alarmData = {
  1496. Type = alarmType,
  1497. Reason = alarmReason,
  1498. Data = {battery=vbatt},
  1499. Time = os.time()
  1500. }
  1501. sys.publish("LOCAL_PUB_MSG", json.encode({["topic"]="SHEGCL/IntelligenTool/UploadAlarmInfo",["data"]=alarmData}))
  1502. end
  1503. sdcardMouted = false
  1504. sys.taskInit(function( )
  1505. -- body
  1506. local result = sys.waitUntil("IO_SDCARD_MOUTED", 10000)
  1507. if result then
  1508. -- body
  1509. sdcardMouted = true
  1510. end
  1511. end)
  1512. sys.timerLoopStart(checkPumbHeartbeatExtra,60000)
  1513. local heartbeatInterval = nvm.get("heartbeatInterval") or 600
  1514. -- 定时心跳
  1515. sys.taskInit(function()
  1516. sys.wait(5000)
  1517. while true do
  1518. if socket.isReady() then --连上网再开始运行
  1519. -- sys.waitUntil("pub_gps_location_success_msg", 120*1000)
  1520. local heartBeatData = getHeartbeatContent()
  1521. log.info("pblish heartbeat msg!!!")
  1522. sys.publish("LOCAL_PUB_MSG", json.encode({["topic"]="SHEGCL/IntelligenTool/Heart",["data"]=heartBeatData}))
  1523. sys.wait(1000)
  1524. checkNetWorkHealth(nil)
  1525. sys.wait(heartbeatInterval *1000)
  1526. else --没连上网别忘了延时!不然会陷入while true死循环,导致模块无法运行其他代码
  1527. -- log.info("socket not ready")
  1528. sys.wait(5000) --等待5秒
  1529. end
  1530. end
  1531. end)