uart1_msg.lua 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. --[[
  2. @module uart1_adc
  3. @summary UART1接收MCU发送的ADC数据(对齐新C结构体协议)
  4. @usage 处理粘包分包、协议解析、CRC校验
  5. ]]
  6. local uartid = 1
  7. local uart_send = {}
  8. local rdbuf = ""
  9. -- ===================== 【新版协议常量】 =====================
  10. local FRAME_HEAD = "\xFE\xFE"
  11. local FRAME_HEAD_LEN = 12 -- 帧头长度不变
  12. local CHECK_FIELD_LEN = 2 -- CRC 2字节
  13. local DEVICE_TYPE = 0x9102
  14. -- 【关键】ADC 数据长度:500点 uint16 = 1000字节
  15. local ADC_POINT_CNT = 500
  16. local ADC_DATA_LEN = ADC_POINT_CNT * 2 -- 1000 字节
  17. -- 【关键】完整 adc_data_up_t 结构体总长度(必须和C语言一致)
  18. local ADC_STRUCT_LEN = 2 + 4 + 4 + 4 + 2 + 2 + 2 + 1 + 2 + (ADC_POINT_CNT * 2)
  19. -- 计算结果 = 1023 字节
  20. -- ===================== 帧头解析(完全不变) =====================
  21. local function head_paser(data)
  22. local netHeader = {pro_ver=0,msg_id=0,msg_type1=0,msg_type2=0,msg_len=0}
  23. local nextpos = 1
  24. if #data < FRAME_HEAD_LEN then
  25. return false,nil
  26. end
  27. nextpos, netHeader.head = pack.unpack(data, "<H", nextpos)
  28. if netHeader.head ~= 0xFEFE then
  29. log.warn("uart1","帧头错误")
  30. return false,nil
  31. end
  32. nextpos, netHeader.pro_ver = pack.unpack(data, "b", nextpos)
  33. nextpos, netHeader.msg_id = pack.unpack(data, "<I", nextpos)
  34. nextpos, netHeader.msg_type1 = pack.unpack(data, "b", nextpos)
  35. nextpos, netHeader.msg_type2 = pack.unpack(data, "<H", nextpos)
  36. nextpos, netHeader.msg_len = pack.unpack(data, "<H", nextpos)
  37. return true, netHeader
  38. end
  39. -- ===================== 【核心】完整帧解析 =====================
  40. local function parse(data)
  41. if not data or #data == 0 then
  42. return false, data, nil
  43. end
  44. local head_pos = string.find(data, FRAME_HEAD, 1, true)
  45. if not head_pos then
  46. return false, data, nil
  47. end
  48. local remain_buf = string.sub(data, head_pos)
  49. if #remain_buf < FRAME_HEAD_LEN then
  50. return false, remain_buf, nil
  51. end
  52. local head_ok, netHeader = head_paser(remain_buf)
  53. if not head_ok then
  54. return true, string.sub(remain_buf, 2), nil
  55. end
  56. -- 【关键】msg_len = adc结构体长度 + 2字节CRC
  57. local frame_total_len = FRAME_HEAD_LEN + netHeader.msg_len
  58. -- 长度校验:必须等于 ADC结构体长度 + 2
  59. if netHeader.msg_len ~= (ADC_STRUCT_LEN + CHECK_FIELD_LEN) then
  60. log.error("uart1","长度错误,期望:", ADC_STRUCT_LEN + CHECK_FIELD_LEN, "实际:", netHeader.msg_len)
  61. return true, string.sub(remain_buf, 2), nil
  62. end
  63. if #remain_buf < frame_total_len then
  64. return false, remain_buf, nil
  65. end
  66. local full_frame = string.sub(remain_buf, 1, frame_total_len)
  67. local unproc_buf = string.sub(remain_buf, frame_total_len + 1)
  68. return true, unproc_buf, full_frame
  69. end
  70. -- ===================== 接收拼接(不变) =====================
  71. local function proc(data)
  72. if not data or #data == 0 then return end
  73. rdbuf = rdbuf .. data
  74. local result, unproc_buf, full_frame
  75. unproc_buf = rdbuf
  76. while true do
  77. result, unproc_buf, full_frame = parse(unproc_buf)
  78. if full_frame then
  79. sys.publish("UART1_ADC_RECIVE", full_frame)
  80. end
  81. if not result or not unproc_buf or unproc_buf == "" then
  82. break
  83. end
  84. end
  85. rdbuf = unproc_buf or ""
  86. end
  87. -- ===================== 串口回调(不变) =====================
  88. local function uart_cb(id, len)
  89. local data = ""
  90. repeat
  91. data = uart.read(id, 1050)
  92. if not data or #data == 0 then break end
  93. proc(data)
  94. --log.info("uart", "receive", id, #data)
  95. until data == ""
  96. end
  97. -- local rxbuff = zbuff.create(1050)
  98. -- local function uart_cb(id, len)
  99. -- local rx_len = uart.rx(id, rxbuff) -- 只读一次
  100. -- if rx_len > 0 then
  101. -- local data_str = rxbuff:toStr(1, rx_len)
  102. -- proc(data_str)
  103. -- log.info("uart", "receive", rx_len)
  104. -- end
  105. -- end
  106. -- 全局偏移量(不变)
  107. _G.Y_FULL_SCALE = 5
  108. _G.x_offset = 0
  109. _G.y_offset = 0
  110. -- ===================== 【核心:新版结构体数据解析】 =====================
  111. sys.subscribe("UART1_ADC_RECIVE", function(full_frame)
  112. if not full_frame or #full_frame == 0 then return end
  113. local head_ok, netHeader = head_paser(full_frame)
  114. if not head_ok then return end
  115. -- 1. 拆分:帧头 + 结构体数据(info) + CRC
  116. local total_frame_len = FRAME_HEAD_LEN + netHeader.msg_len
  117. local struct_data = string.sub(full_frame, FRAME_HEAD_LEN + 1, FRAME_HEAD_LEN + ADC_STRUCT_LEN)
  118. local recv_crc_str = string.sub(full_frame, total_frame_len - 1, total_frame_len)
  119. local crc_calc_data = string.sub(full_frame, 1, total_frame_len - CHECK_FIELD_LEN)
  120. -- 2. CRC 校验
  121. local _, recv_crc = pack.unpack(recv_crc_str, "<H", 1)
  122. local calc_crc = crypto.crc16("IBM", crc_calc_data)
  123. --log.info("uart1","CRC校验:接收=",string.format("%04X",recv_crc),"计算=",string.format("%04X",calc_crc))
  124. if recv_crc ~= calc_crc then
  125. log.error("uart1","CRC校验失败")
  126. return
  127. end
  128. --log.info("uart1","CRC校验成功")
  129. -- ===================== 【关键】解析 adc_data_up_t 结构体 =====================
  130. local pos = 1
  131. local adc_struct = {}
  132. _, adc_struct.device_type = pack.unpack(struct_data, "<H", pos) pos = pos + 2
  133. _, adc_struct.device_sn = pack.unpack(struct_data, "<I", pos) pos = pos + 4
  134. _, adc_struct.reserve = pack.unpack(struct_data, "<I", pos) pos = pos + 4
  135. _, adc_struct.frequency = pack.unpack(struct_data, "<I", pos) pos = pos + 4
  136. _, adc_struct.max_vol = pack.unpack(struct_data, "<H", pos) pos = pos + 2
  137. _, adc_struct.min_vol = pack.unpack(struct_data, "<H", pos) pos = pos + 2
  138. _, adc_struct.x_offset = pack.unpack(struct_data, "<H", pos) pos = pos + 2
  139. _, adc_struct.compress_type = pack.unpack(struct_data, "b", pos) pos = pos + 1
  140. _, adc_struct.data_len = pack.unpack(struct_data, "<H", pos) pos = pos + 2
  141. -- 提取真正的 500 点 ADC 数据
  142. local adc_raw_data = string.sub(struct_data, pos, pos + ADC_DATA_LEN - 1)
  143. log.info("uart1","触发位置:", adc_struct.x_offset, "ADC点数:", adc_struct.data_len)
  144. -- 打印频率、电压最大值和电压最小值
  145. -- log.info("uart1", "频率: " .. adc_struct.frequency .. " Hz")
  146. -- log.info("uart1", "电压最大值: " .. adc_struct.max_vol .. " mV")
  147. -- log.info("uart1", "电压最小值: " .. adc_struct.min_vol .. " mV")
  148. -- 发布事件,通知UI更新
  149. sys.publish("ADC_DATA_UPDATE", {
  150. frequency = adc_struct.frequency,
  151. max_vol = adc_struct.max_vol,
  152. min_vol = adc_struct.min_vol,
  153. x_offset = adc_struct.x_offset
  154. })
  155. -- ===================== 500点波形转换 =====================
  156. local wave_data = {}
  157. local raw_wave = {}
  158. pos = 1
  159. for i = 1, ADC_POINT_CNT do
  160. local adc_val = 0
  161. if pos <= #adc_raw_data then
  162. _, adc_val = pack.unpack(adc_raw_data, "<H", pos)
  163. pos = pos + 2
  164. end
  165. -- 电压计算(和你原来一样)
  166. local V_adc = adc_val * 3.3 / 4095.0
  167. -- if V_adc < 0.2 then V_adc = 0.2 end
  168. -- local V_real = (V_adc - 0.2) * 10.0
  169. local V_real = V_adc * 5.0
  170. local display_val = V_real / _G.Y_FULL_SCALE * 100.0
  171. display_val = math.floor(display_val + 0.5)
  172. display_val = math.max(0, display_val)
  173. raw_wave[i] = display_val
  174. end
  175. -- 偏移处理
  176. for i = 1, ADC_POINT_CNT do
  177. local val = raw_wave[i]
  178. val = val + _G.y_offset
  179. val = math.max(0, val)
  180. table.insert(wave_data, val)
  181. end
  182. -- 发布 500 点波形
  183. sys.publish("ADC_WAVE_DATA", wave_data)
  184. --log.info("uart1","500点波形解析完成")
  185. end)
  186. -- ===================== 串口初始化(不变) =====================
  187. uart.setup(uartid, 921600,8,1,uart.NONE,uart.LSB)
  188. uart.on(uartid, "receive", uart_cb)
  189. local function uart_send_cb(id)
  190. log.info("uart", id , "发送完成")
  191. end
  192. uart.on(uartid, "sent", uart_send_cb)
  193. local msg_id = 0
  194. -- ADC配置参数下发
  195. function uart_send.send_adc_config(config)
  196. if not config then
  197. log.error("uart1", "发送ADC配置失败:参数为空")
  198. return
  199. end
  200. log.info("uart1", "发送ADC配置数据:")
  201. log.info("uart1", "垂直档位: " .. config.vertical_gear .. " mV")
  202. log.info("uart1", "时间档位: " .. config.time_gear .. " us")
  203. log.info("uart1", "水平偏移: " .. config.x_offset)
  204. log.info("uart1", "垂直偏移: " .. config.y_offset)
  205. log.info("uart1", "触发阈值: " .. config.trigger_threshold .. " mV")
  206. log.info("uart1", "触发类型: " .. config.trigger_type)
  207. log.info("uart1", "触发脉宽: " .. config.trigger_width .. " us")
  208. local ADC_CONFIG_SIZE = 25
  209. local MSG_LEN = ADC_CONFIG_SIZE + CHECK_FIELD_LEN
  210. local frame_header = pack.pack("<H", 0xFEFE)
  211. local proto_ver = pack.pack("b", 0x03)
  212. local msg_id_pack = pack.pack("<I", msg_id)
  213. local first_type = pack.pack("b", 0x01)
  214. local second_type = pack.pack("<H", 0x1001)
  215. local msg_len_pack = pack.pack("<H", MSG_LEN)
  216. local device_type = pack.pack("<H", DEVICE_TYPE)
  217. local device_sn = pack.pack("<I", 0x00000001)
  218. local reserve = pack.pack("<I", 0)
  219. local vertical_gear = pack.pack("<H", config.vertical_gear)
  220. local time_gear = pack.pack("<I", config.time_gear)
  221. local x_offset = pack.pack("b", config.x_offset)
  222. local y_offset = pack.pack("b", config.y_offset)
  223. local trigger_threshold = pack.pack("<H", config.trigger_threshold)
  224. local trigger_type = pack.pack("b", config.trigger_type)
  225. local trigger_width = pack.pack("<I", config.trigger_width)
  226. local frame_data = frame_header .. proto_ver .. msg_id_pack .. first_type .. second_type .. msg_len_pack ..
  227. device_type .. device_sn .. reserve ..
  228. vertical_gear .. time_gear .. x_offset .. y_offset ..
  229. trigger_threshold .. trigger_type .. trigger_width
  230. local crc = crypto.crc16("IBM", frame_data)
  231. local crc_pack = pack.pack("<H", crc)
  232. local full_frame = frame_data .. crc_pack
  233. local hex_str = string.toHex(full_frame, " ")
  234. log.info("uart1", "完整帧 Hex: ", hex_str)
  235. log.info("uart1", "完整帧长度: ", #full_frame)
  236. uart.write(uartid, full_frame, #full_frame)
  237. log.info("uart1", "发送ADC配置指令成功,消息ID:", msg_id)
  238. msg_id = (msg_id + 1) & 0xFFFFFFFF
  239. end
  240. -- ADC动态操作下发
  241. function uart_send.send_adc_operate(config)
  242. if not config then
  243. log.error("uart1", "发送ADC动态操作失败:参数为空")
  244. return
  245. end
  246. log.info("uart1", "发送ADC动态操作数据:")
  247. log.info("uart1", "触发方式: " .. config.trigger_method)
  248. log.info("uart1", "运行控制: " .. config.run_control)
  249. log.info("uart1", "复位: " .. config.reset)
  250. local ADC_CONFIG_SIZE = 13
  251. local MSG_LEN = ADC_CONFIG_SIZE + CHECK_FIELD_LEN
  252. local frame_header = pack.pack("<H", 0xFEFE)
  253. local proto_ver = pack.pack("b", 0x03)
  254. local msg_id_pack = pack.pack("<I", msg_id)
  255. local first_type = pack.pack("b", 0x01)
  256. local second_type = pack.pack("<H", 0x1002)
  257. local msg_len_pack = pack.pack("<H", MSG_LEN)
  258. local device_type = pack.pack("<H", DEVICE_TYPE)
  259. local device_sn = pack.pack("<I", 0x00000001)
  260. local reserve = pack.pack("<I", 0)
  261. local reset = pack.pack("b", config.reset)
  262. local run_control = pack.pack("b", config.run_control)
  263. local trigger_method = pack.pack("b", config.trigger_method)
  264. local frame_data = frame_header .. proto_ver .. msg_id_pack .. first_type .. second_type .. msg_len_pack ..
  265. device_type .. device_sn .. reserve ..
  266. reset .. run_control .. trigger_method
  267. local crc = crypto.crc16("IBM", frame_data)
  268. local crc_pack = pack.pack("<H", crc)
  269. local full_frame = frame_data .. crc_pack
  270. local hex_str = string.toHex(full_frame, " ")
  271. log.info("uart1", "完整帧 Hex: ", hex_str)
  272. log.info("uart1", "完整帧长度: ", #full_frame)
  273. uart.write(uartid, full_frame, #full_frame)
  274. log.info("uart1", "发送ADC配置指令成功,消息ID:", msg_id)
  275. msg_id = (msg_id + 1) & 0xFFFFFFFF
  276. end
  277. -- DAC配置参数下发
  278. function uart_send.send_dac_config(config)
  279. if not config then
  280. log.error("uart1", "发送DAC配置失败:参数为空")
  281. return
  282. end
  283. log.info("uart1", "发送DAC配置数据:")
  284. log.info("uart1", "运行控制: " .. config.run_control)
  285. log.info("uart1", "波形类型: " .. config.wave_type)
  286. log.info("uart1", "最大电压: " .. config.max_vol)
  287. log.info("uart1", "最小电压: " .. config.min_vol)
  288. log.info("uart1", "频率: " .. config.frequency)
  289. log.info("uart1", "占空比: " .. config.duty_cycle)
  290. local DAC_CONFIG_SIZE = 21
  291. local MSG_LEN = DAC_CONFIG_SIZE + CHECK_FIELD_LEN
  292. local frame_header = pack.pack("<H", 0xFEFE)
  293. local proto_ver = pack.pack("b", 0x03)
  294. local msg_id_pack = pack.pack("<I", msg_id)
  295. local first_type = pack.pack("b", 0x01)
  296. local second_type = pack.pack("<H", 0x1003)
  297. local msg_len_pack = pack.pack("<H", MSG_LEN)
  298. local device_type = pack.pack("<H", DEVICE_TYPE)
  299. local device_sn = pack.pack("<I", 0x00000001)
  300. local reserve = pack.pack("<I", 0)
  301. local run_control = pack.pack("b", config.run_control)
  302. local wave_type = pack.pack("b", config.wave_type)
  303. local max_vol = pack.pack("<H", config.max_vol)
  304. local min_vol = pack.pack("<H", config.min_vol)
  305. local frequency = pack.pack("<I", config.frequency)
  306. local duty_cycle = pack.pack("<b", config.duty_cycle)
  307. local frame_data = frame_header .. proto_ver .. msg_id_pack .. first_type .. second_type .. msg_len_pack ..
  308. device_type .. device_sn .. reserve ..
  309. run_control .. wave_type .. max_vol .. min_vol .. frequency .. duty_cycle
  310. local crc = crypto.crc16("IBM", frame_data)
  311. local crc_pack = pack.pack("<H", crc)
  312. local full_frame = frame_data .. crc_pack
  313. local hex_str = string.toHex(full_frame, " ")
  314. log.info("uart1", "完整帧 Hex: ", hex_str)
  315. log.info("uart1", "完整帧长度: ", #full_frame)
  316. uart.write(uartid, full_frame, #full_frame)
  317. log.info("uart1", "发送DAC配置指令成功,消息ID:", msg_id)
  318. msg_id = (msg_id + 1) & 0xFFFFFFFF
  319. end
  320. -- 上电初始化发送函数(发送20个 55 AA 组合)
  321. local function send_init_data()
  322. -- 构建20个 55 AA 的数据
  323. local init_data = ""
  324. for i = 1, 20 do
  325. init_data = init_data .. "\x55\xAA"
  326. end
  327. -- 发送数据
  328. uart.write(uartid, init_data, #init_data)
  329. log.info("uart1", "上电初始化数据发送完成,长度:", #init_data)
  330. -- 打印发送的数据(十六进制)
  331. local hex_str = string.toHex(init_data, " ")
  332. log.info("uart1", "初始化数据 Hex:", hex_str)
  333. end
  334. -- 一次性定时器:上电后1秒发送初始化数据
  335. sys.timerStart(send_init_data, 1000)
  336. return uart_send