#raspberry-pi #node-serialport
#малина-пи #узел-serialport
Вопрос:
У меня есть 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",
"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: 'rn', 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?
Ответ №1:
Хорошо, похоже, я нашел какое-то обходное решение, не уверен, что оно лучшее:
Я использовал библиотеку 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","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}