问题描述
我第一次使用Serialport,并尝试使用以下方法与作为高度调节台控制器的UART设备建立读写连接.该应用程序是使用电子的桌面应用程序.
I am working with serialport for the first time and trying to establish a read and write connection with an UART device which is a controller of a height adjustment desk by using the below method. The application is a desktop application using electron.
const SerialPort = require('serialport')
const Readline = require('@serialport/parser-readline')
const parser = new Readline()
const port = new SerialPort("COM4", {
baudRate: 9600
})
这是我使用的代码,port.read()始终返回空值.
This is the code I have used and port.read() always returns null value.
对于写操作,我使用了如下代码:
For the write operation I have used the code like below:
var buf = new Buffer([ 0XF1, 0XF1, 0X01, 0X00, 0X01, 0X7E]);
port.write(buf, function(err,n) {
if (err) {
return console.log('Error on write: ', err.message)
}
console.log(n)
console.log('message written')
})
缓冲区值是用于向上移动办公桌的缓冲区值,但是没有任何操作发生,并且它不返回错误或在回调中返回未定义的值.
The buffer values are the ones for moving the desk up but no operation takes place and it returns no error or returns undefined value in callback.
有关设备和设置的更多详细信息:使用RJ45转USB连接器连接到桌子的控制盒.
More details on the device and setup:Using an RJ45 to USB connector to connect with the control box of the table.
The definition of the SCI is as below:
(Baud Rate):9600
(Data Mode):8
(Stop Bit):1
(Parity Type):None
Path: COM3
手机是指我的系统.
基本写操作缓冲区值:
上移= 0XF1 0XF1 0X01 0X00 0X01 0X7E
Move Up=0XF1 0XF1 0X01 0X00 0X01 0X7E
下移= 0XF1 0XF1 0X02 0X00 0X02 0X7E
Move Down=0XF1 0XF1 0X02 0X00 0X02 0X7E
停止动作= 0XF1 0XF1 0X0c 0X00 0X0c 0X7E
Stop Action=0XF1 0XF1 0X0c 0X00 0X0c 0X7E
阅读功能示例:
当前高度(1000mm-0x03E8)0XF2 0XF2 0X01 0X02 0X03 0XE8 0XEE 0X7E
Current height(1000mm-0x03E8)0XF2 0XF2 0X01 0X02 0X03 0XE8 0XEE 0X7E
(数据"中有两个字节,因此数据长度"为0x02;校验和" = 0x01 + 0x02 + 0x03 + 0xE8 = 0xEE)
(there is two bytes in ‘Data’,so ‘Data Length’ is 0x02;‘Checksum’= 0x01 + 0x02 +0x03 +0xE8 = 0xEE)
期望读取功能可以提供当前的高度信息,而写入功能则可以控制设备.
Expecting the read functionality to give the current height info and write functionality to be able to control the device.
版本,操作系统和硬件:
SerialPort @ ^ 8.0.7
SerialPort@ ^8.0.7
Node.js v10.16.0
Node.js v10.16.0
Windows
硬件和芯片组?COM4
Hardware and chipset?COM4
FTDIBUS \ VID_0403 + PID_6001 + AB0JIYQTA \ 0000
FTDIBUS\VID_0403+PID_6001+AB0JIYQTA\0000
FTDI
推荐答案
创建读取行解析器表明消息以\ n终止,但是您的写操作不包含消息,因此可以解释为什么命令无法到达设备.
Creating a read-line parser suggests that messages are terminated by \n, but your write does not include one, so that could explain why commands are not getting to the device.
对于读取,如果要使用读取行解析器,则需要将serialPort传递给它,然后侦听解析器中的数据,例如:
For the reads, if you want to use the read-line parser, you need to pipe the serialPort to it, then listen for data from the parser, e.g.:
serialPort.pipe(parser);
parser.on('data', (data) => {
console.log(data)
})
注意:数据将是缓冲区
您可以跳过解析器并直接调用read.我想你尝试过了.医生说:
You could skip the parser and call read directly. I assume you tried that. Docs say:
https://serialport.io/docs/api-stream#serialport-read
因此,可能是办公桌仅在收到消息后才输出数据.
So, it could be that the desk only outputs data after a message is received.
这篇关于读/写数据返回null-无法执行任何操作-节点串行端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!