Readline解析器不能从串口正确读取

编程入门 行业动态 更新时间:2024-10-07 22:24:29

Readline解析器不能从<a href=https://www.elefans.com/category/jswz/34/1769224.html style=串口正确读取"/>

Readline解析器不能从串口正确读取

我与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构造函数中设置解析器,改变了定界符,但是没有结果,我想我的错误是解析器的问题。

这里你可以看到,没有返回 console.log

以下是我使用

myPort.on('data', function(data) {
    console.log('data received: ', data);
});

我的想法是在每条命令后获取整个信息并将其发送到客户端。

回答如下:

使用内置的readline api SerialPort:

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);
   }
}

// ...

更多推荐

Readline解析器不能从串口正确读取

本文发布于:2024-05-13 11:55:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1759500.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:串口   正确   Readline

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!