我已经使用Node.js创建了一个网络服务器并进行了表达。我想通过单击一个按钮来读取一些数据到串行端口。写作有效,阅读无效。我没有任何错误。
这样我就可以看到与Putty一起发送的日期。但是read.js没有显示任何内容。
////index.js
const express = require('express');
const path = require('path');
const socket = require('socket.io');
const SerialPort = require('serialport');
const writeToPort = require('./write');
const readPort = require('./read');
const Readline = require('@serialport/parser-readline');
const app = express();
app.use(express.static(path.join(__dirname, 'public')));
const PORT = process.env.PORT || 80;
var server = app.listen(PORT, () => console.log('Server started on port:
'+PORT));
var io = socket(server);
io.on('connection', function(socket){
console.log('made socket connection');
// Port open
const wPort = new SerialPort('COM1');
const rPort = new SerialPort('COM2');
readPort.read(rPort);
// 'direction' event
socket.on('direction', function(data){
// Write to port
writeToPort.write(wPort, data);
});
});
////read.js
const SerialPort = require('serialport');
const Readline = require('@serialport/parser-readline');
exports.read = function(_port){
console.log('Reading...');
const port = _port;
const parser = port.pipe(new Readline({ delimiter: '\r\n' }));
parser.on('data', function(data){
console.log('Read: '+data);
});
}