Raspberry pi 节点serialport组合JSON块

Raspberry pi 节点serialport组合JSON块,raspberry-pi,node-serialport,Raspberry Pi,Node Serialport,我有一个树莓PI通过串行端口连接到ESP32控制器 我能够读取从串口接收到的数据(控制器将大JSON作为多个块发送) 如果没有解析器,数据将如下所示: 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",

我有一个树莓PI通过串行端口连接到ESP32控制器

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

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

    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",
                "type": "input"
            }, {
                "name": "I3",
                "type": "input"
            }, {
                "name": "I4",
                "type": "input"
            }, {
                "name": "I5",
                "type": "input"
            }, {
                "name": "I6",
                "type": "input"
            }, {
                "name": "I7",
                "type": "inp
    2020/12/18 21:03:49.059 debug:  [SERI handleData] Data: ut"
            }, {
                "name": "TO.0",
                "type": "output"
            }, {
                "name": "TO.1",
                "type": "output"
            }, {
                "name": "A0",
                "type": "analog"
            }, {
                "name": "A1",
                "type": "analog"
            }, {
                "name": "A2",
                "type": "analog"
            }, {
                "
    2020/12/18 21:03:49.071 debug:  [SERI handleData] Data: name":  "A3",
                "type": "analog"
            }, {
                "name": "A4",
                "type": "analog"
            }, {
                "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对象呢?

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

我用过图书馆

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

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","type":"input"},{"name":"I3","type":"input"},{"name":"I4","type":"input"},{"name":"I5","type":"input"},{"name":"I6","type":"input"},{"name":"I7","type":"input"},{"name":"TO.0","type":"output"},{"name":"TO.1","type":"output"},{"name":"A0","type":"analog"},{"name":"A1","type":"analog"},{"name":"A2","type":"analog"},{"name":"A3","type":"analog"},{"name":"A4","type":"analog"},{"name":"A5","type":"analog"}],"error":0}
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","type":"input"},{"name":"I3","type":"input"},{"name":"I4","type":"input"},{"name":"I5","type":"input"},{"name":"I6","type":"input"},{"name":"I7","type":"input"},{"name":"TO.0","type":"output"},{"name":"TO.1","type":"output"},{"name":"A0","type":"analog"},{"name":"A1","type":"analog"},{"name":"A2","type":"analog"},{"name":"A3","type":"analog"},{"name":"A4","type":"analog"},{"name":"A5","type":"analog"}],"error":0}