首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >node-serialport组合JSON块

node-serialport组合JSON块
EN

Stack Overflow用户
提问于 2020-12-19 05:25:24
回答 1查看 24关注 0票数 1

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

我能够读取从串口接收的数据(控制器以多个块的形式发送大型JSON )

代码语言:javascript
复制
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')
      }
  })

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

代码语言:javascript
复制
    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
    }

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

解析器:

代码语言:javascript
复制
const parser = serial.pipe(new Readline({ delimiter: '\r\n', encoding: 'utf8' }))

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

结果:

代码语言:javascript
复制
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对象呢?

EN

回答 1

Stack Overflow用户

发布于 2020-12-19 21:54:26

好的,看起来我找到了一些变通方法,不确定它是否是最好的:

我使用的是库stream-json

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

代码语言:javascript
复制
const stream = serial.pipe(parser())

然后附加来自同一个库的汇编程序:

代码语言:javascript
复制
const assembler = Assembler.connectTo(stream)

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

这给了我完整的JavaScript对象:

代码语言: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}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65364106

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档