节点串行端口组合 JSON 块

问题描述

我有一个 RaspBerry PI 通过串口连接到 ESP32 控制器

我能够读取从串行接收到的数据(控制器将大型 JSON 作为多个块发送)

serial = new SerialPort(
    usb,{
      baudrate: 115200,dataBits: 8,parity: 'none',stopBits: 1,},err => {
  
      if (!!err) {
        isOpened = false
        const error = 'Error opening serial port: ' + u.stringify(err)
        handleError(error)
      }else{
        isOpened = true
        log.info(sig,'Serial Port opened')
      }
  })

如果没有解析器,数据将如下所示:

    2020/12/18 21:03:48.956 info:  [SERI write] message sent

    2020/12/18 21:03:49.020 debug:  [SERI handleData] Data: {
        "type": "answer","orig_message": {
            "type": "message","command":  "identify"
        },"timestamp":    1608239027,"heap-size":    284132,"controller":   "MM1","firmware": "0.1-ex","io":   [{
                "name": "I1","type": "input"
            },{
                "nam
    2020/12/18 21:03:49.037 debug:  [SERI handleData] Data: e": "I2",{
                "name": "I3",{
                "name": "I4",{
                "name": "I5",{
                "name": "I6",{
                "name": "I7","type": "inp
    2020/12/18 21:03:49.059 debug:  [SERI handleData] Data: ut"
            },{
                "name": "TO.0","type": "output"
            },{
                "name": "TO.1",{
                "name": "A0","type": "analog"
            },{
                "name": "A1",{
                "name": "A2",{
                "
    2020/12/18 21:03:49.071 debug:  [SERI handleData] Data: name":  "A3",{
                "name": "A4",{
                "name": "A5","type": "analog"
            }],"error":    0
    }

添加解析器后(尝试了多个不同的解析器),数据将显示为分隔行:

解析器:

const parser = serial.pipe(new Readline({ delimiter: '\r\n',encoding: 'utf8' }))

  parser.on('data',data => {
    handleData(data)
  })

结果:

2020/12/18 20:28:03.137 info:  [SERI write] message sent
2020/12/18 20:28:03.208 debug:  [SERI handleData] Data: {
2020/12/18 20:28:03.211 debug:  [SERI handleData] Data:     "type": "answer",2020/12/18 20:28:03.212 debug:  [SERI handleData] Data:     "orig_message": {
2020/12/18 20:28:03.214 debug:  [SERI handleData] Data:         "type": "message",2020/12/18 20:28:03.215 debug:  [SERI handleData] Data:         "command":  "identify"
2020/12/18 20:28:03.217 debug:  [SERI handleData] Data:     },2020/12/18 20:28:03.218 debug:  [SERI handleData] Data:     "timestamp":    1608236881,2020/12/18 20:28:03.220 debug:  [SERI handleData] Data:     "heap-size":    284132,2020/12/18 20:28:03.221 debug:  [SERI handleData] Data:     "controller":   "MM1",2020/12/18 20:28:03.222 debug:  [SERI handleData] Data:     "firmware": "0.1-ex",2020/12/18 20:28:03.224 debug:  [SERI handleData] Data:     "io":   [{
2020/12/18 20:28:03.225 debug:  [SERI handleData] Data:             "name": "I1",2020/12/18 20:28:03.227 debug:  [SERI handleData] Data:             "type": "input"
2020/12/18 20:28:03.228 debug:  [SERI handleData] Data:         },{
2020/12/18 20:28:03.235 debug:  [SERI handleData] Data:             "name": "I2",2020/12/18 20:28:03.237 debug:  [SERI handleData] Data:             "type": "input"
2020/12/18 20:28:03.238 debug:  [SERI handleData] Data:         },{
2020/12/18 20:28:03.239 debug:  [SERI handleData] Data:             "name": "I3",2020/12/18 20:28:03.240 debug:  [SERI handleData] Data:             "type": "input"
2020/12/18 20:28:03.241 debug:  [SERI handleData] Data:         },{
2020/12/18 20:28:03.242 debug:  [SERI handleData] Data:             "name": "I4",2020/12/18 20:28:03.243 debug:  [SERI handleData] Data:             "type": "input"
2020/12/18 20:28:03.244 debug:  [SERI handleData] Data:         },{
2020/12/18 20:28:03.245 debug:  [SERI handleData] Data:             "name": "I5",2020/12/18 20:28:03.246 debug:  [SERI handleData] Data:             "type": "input"
2020/12/18 20:28:03.247 debug:  [SERI handleData] Data:         },{
2020/12/18 20:28:03.248 debug:  [SERI handleData] Data:             "name": "I6",2020/12/18 20:28:03.249 debug:  [SERI handleData] Data:             "type": "input"
2020/12/18 20:28:03.250 debug:  [SERI handleData] Data:         },{
2020/12/18 20:28:03.251 debug:  [SERI handleData] Data:             "name": "I7",2020/12/18 20:28:03.257 debug:  [SERI handleData] Data:             "type": "input"
2020/12/18 20:28:03.259 debug:  [SERI handleData] Data:         },{
2020/12/18 20:28:03.260 debug:  [SERI handleData] Data:             "name": "TO.0",2020/12/18 20:28:03.261 debug:  [SERI handleData] Data:             "type": "output"
2020/12/18 20:28:03.262 debug:  [SERI handleData] Data:         },{
2020/12/18 20:28:03.263 debug:  [SERI handleData] Data:             "name": "TO.1",2020/12/18 20:28:03.264 debug:  [SERI handleData] Data:             "type": "output"
2020/12/18 20:28:03.264 debug:  [SERI handleData] Data:         },{
2020/12/18 20:28:03.265 debug:  [SERI handleData] Data:             "name": "A0",2020/12/18 20:28:03.266 debug:  [SERI handleData] Data:             "type": "analog"
2020/12/18 20:28:03.267 debug:  [SERI handleData] Data:         },{
2020/12/18 20:28:03.268 debug:  [SERI handleData] Data:             "name": "A1",2020/12/18 20:28:03.269 debug:  [SERI handleData] Data:             "type": "analog"
2020/12/18 20:28:03.270 debug:  [SERI handleData] Data:         },{
2020/12/18 20:28:03.271 debug:  [SERI handleData] Data:             "name": "A2",2020/12/18 20:28:03.272 debug:  [SERI handleData] Data:             "type": "analog"
2020/12/18 20:28:03.273 debug:  [SERI handleData] Data:         },{
2020/12/18 20:28:03.274 debug:  [SERI handleData] Data:             "name": "A3",2020/12/18 20:28:03.275 debug:  [SERI handleData] Data:             "type": "analog"
2020/12/18 20:28:03.276 debug:  [SERI handleData] Data:         },{
2020/12/18 20:28:03.277 debug:  [SERI handleData] Data:             "name": "A4",2020/12/18 20:28:03.278 debug:  [SERI handleData] Data:             "type": "analog"
2020/12/18 20:28:03.279 debug:  [SERI handleData] Data:         },{
2020/12/18 20:28:03.280 debug:  [SERI handleData] Data:             "name": "A5",2020/12/18 20:28:03.280 debug:  [SERI handleData] Data:             "type": "analog"
2020/12/18 20:28:03.281 debug:  [SERI handleData] Data:         }],2020/12/18 20:28:03.282 debug:  [SERI handleData] Data:     "error":    0
2020/12/18 20:28:03.283 debug:  [SERI handleData] Data: }

那么如何将这些块组合成一个完整的 JSON 对象?

解决方法

好的,似乎我找到了一些解决方法,不确定它是否是最好的:

我使用了图书馆 stream-json

我向串行流添加了一个解析器:

const stream = serial.pipe(parser())

然后从同一个库中附加汇编器:

const assembler = Assembler.connectTo(stream)

  assembler.on('done',asm => {
    handleData(asm.current)
  })

这给了我完整的 JavaScript 对象:

Data: {"type":"answer","orig_message":{"type":"message","command":"identify"},"timestamp":1608299108,"heap-size":284132,"controller":"MM1","firmware":"0.1-ex","io":[{"name":"I1","type":"input"},{"name":"I2",{"name":"I3",{"name":"I4",{"name":"I5",{"name":"I6",{"name":"I7",{"name":"TO.0","type":"output"},{"name":"TO.1",{"name":"A0","type":"analog"},{"name":"A1",{"name":"A2",{"name":"A3",{"name":"A4",{"name":"A5","type":"analog"}],"error":0}