我与POS(销售点)设备连接。我以十六进制代码发送信息,设备打印出收据。
我的问题是解析器(Readline
)无法正常工作。当我尝试使用parser.on("data", console.log)
时,它不会返回任何内容。
这是我的代码:
const SerialPort = require('serialport');// include the library
const WebSocketServer = require('ws').Server;
const SERVER_PORT = 7000; // port number for the webSocket server
const wss = new WebSocketServer({port: SERVER_PORT}); // the webSocket server
var connections = new Array; // list of connections to the server
const Readline = SerialPort.parsers.Readline;
wss.on('connection', handleConnection);
const myPort = new SerialPort("COM3", {
baudRate: 115200,
});
myPort.on('open', showPortOpen);
myPort.on('close', showPortClose);
myPort.on('error', showError);
const parser = myPort.pipe(new Readline('\r\n'))
console.log('parser setup');
parser.on('data', function(data) {
console.log('data received: ', data);
});
function handleConnection(client) {
console.log("New Connection"); // you have a new client
connections.push(client); // add this client to the connections array
client.on('message', sendToSerial); // when a client sends a message,
client.on('close', function() { // when a client closes its connection
console.log("connection closed"); // print it out
var position = connections.indexOf(client); // get the client's position in the array
connections.splice(position, 1); // and delete it from the array
});
}
function sendToSerial(data) {
console.log("sending to serial: " + data);
myPort.write(data, 'hex');
}
// This function broadcasts messages to all webSocket clients
function broadcast(data) {
console.log(data);
for (myConnection in connections) { // iterate over the array of connections
connections[myConnection].send(JSON.stringify(data)); // send the data to each connection
}
}
function showPortOpen() {
console.log('port open. Data rate: ' + myPort.baudRate);
}
function readSerialData(data) {
// if there are webSocket connections, send the serial data
// to all of them:
if (connections.length > 0) {
broadcast(data);
}
}
function showPortClose() {
console.log('port closed.');
}
function showError(error) {
console.log('Serial port error: ' + error);
}
我收到了消息,但它们被拆分了,我想将整个消息发送给客户。我试图定义解析器,然后将其传递给管道。我试图在SerialPort构造函数中设置解析器,更改了分隔符,但没有结果。 我认为我的错误与解析器有关。
这是我使用的结果
myPort.on('data', function(data) {
console.log('data received: ', data);
});
这个想法是在每个命令之后获取完整的消息并将其发送给客户端。
答案 0 :(得分:1)
使用SerialPort
中的内置readline api:
const SerialPort = require('serialport');// include the library
const WebSocketServer = require('ws').Server;
const SERVER_PORT = 7000; // port number for the webSocket server
const wss = new WebSocketServer({port: SERVER_PORT}); // the webSocket server
var connections = new Array; // list of connections to the server
const Readline = SerialPort.parsers.Readline;
wss.on('connection', handleConnection);
const myPort = new SerialPort('COM3', {
baudRate: 115200,
parser: SerialPort.parsers.readline('\r\n')
});
myPort.on('open', showPortOpen);
myPort.on('close', showPortClose);
myPort.on('error', showError);
myPort.on('data', data => readSerialData(data.toString());
// ...
function broadcast(data) {
console.log(data);
for (myConnection in connections) {
connections[myConnection].send(JSON.stringify(data));
}
}
// ...
function readSerialData(data) {
// if there are webSocket connections, send the serial data
// to all of them:
if (connections.length > 0) {
broadcast(data);
}
}
// ...
答案 1 :(得分:0)