从 NodeJs Serialport 读取数据 [英] Reading data from NodeJs Serialport
问题描述
我希望能够读取发送的 ascii 命令接收到的数据.
I would like to be able to read data received by the ascii command sent.
下面是向我的锁控制器发送命令的代码
Below is the code that sends command to my lock controller
var express = require('express');
var router = express.Router();
var SerialPort = require('serialport');
/* GET home page */
router.get('/', function(request, response){
SerialPort.list(function (err, ports) {
ports.forEach(function(port) {
console.log(port.comName);
console.log(port.pnpId);
console.log(port.manufacturer);
});
});
var port = new SerialPort("COM5", {
baudRate: 38400
});
port.on('open', function() {
// NodeJS v4 and earlier
port.write(new Buffer('status1', 'ascii'), function(err) {
if (err) {
return console.log('Error on write: ', err.message);
}
console.log('message written');
});
});
// open errors will be emitted as an error event
port.on('error', function(err) {
console.log('Error: ', err.message);
});
});
// Important
module.exports = router;
在文档中,它提到了使用解析器来尝试读取数据,https://github.com/EmergingTechnologyAdvisors/node-serialport#serialportparsers--object 但我不知道如何实现它,我想在命令 status1
之后执行写的.
In the doc, it mentions the use of parsers to try and read data, https://github.com/EmergingTechnologyAdvisors/node-serialport#serialportparsers--object but I am not sure how to implement it, and I would want to execute after the command status1
has been written.
基本上记录成功写入控制台的命令的响应
Essentially logs the response of the command succesfully written to the console
推荐答案
有一些特殊性.
您可以在应用程序启动时打开端口,并在每个请求的端口关闭或打开端口时重新连接.它定义了如何处理数据流.如果您向端口发送请求,则 answer 可以包含先前请求(多个)的数据.您可以忽略此问题(如果答案很短且请求间隔足够大)或使用分配 id 发送请求并使用此 id 搜索答案.
There are some peculiarities.
You can open port on application start and reconnect on port close or open port on each request. It defines how work with data flow. If you send request to port then answer can contain data of previous requests (more than one). You can ignore this problem (if answer is short and request interval is enough large) or send request with assign id and search answer with this id.
SerialPort.list(function (err, ports) {
ports.forEach(function(port) {
console.log(port.comName, port.pnpId, port.manufacturer); // or console.log(port)
});
});
router.get('/', function(req, res){
function sendData(code, msg) {
res.statusCode = 500;
res.write(msg);
console.log(msg);
}
var port = new SerialPort("COM5", {
baudRate: 38400
});
port.on('open', function() {
port.write(Buffer.from('status1', 'ascii'), function(err) {
if (err)
return sendData(500, err.message);
console.log('message written');
});
});
var buffer = '';
port.on('data', function(chunk) {
buffer += chunk;
var answers = buffer.split(/\r?\n/); \\ Split data by new line character or smth-else
buffer = answers.pop(); \\ Store unfinished data
if (answer.length > 0)
sendData(200, answer[0]);
});
port.on('error', function(err) {
sendData(500, err.message);
});
});
module.exports = router;
这篇关于从 NodeJs Serialport 读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!