#php #node.js #sockets
Вопрос:
Я хочу считывать данные с последовательного порта USB, подключенного к порту RJ232, и печатать данные в моем проекте PHP Codeigniter. У меня есть программа для считывания данных с последовательного порта USB и печати на веб-странице php.. Я получил этот код в блоге https://designswow.blogspot.com/p/reading-data-from-serial-port-with_15.html
после настройки всех настроек я получил serialport.parsers.readline-это не ошибка функции в server.js файл
c:/nodejs/server.js
var BaudRate = 9600;
var ServerPort = 8080;
var DocumentPath = "c:/node";
var fs = require("fs");
var readline = require('readline');
var rl = readline.createInterface({
input : fs.createReadStream('server_config.txt'),
output: process.stdout,
terminal: false
})
rl.on('line',function(line){
console.log(line) // parse line
})
var express = require('express');
io = require('socket.io'), // include socket.io package
app = express(), // make an instance of express.js module
//server = app.listen(8080), // start a server on the port
server = app.listen(ServerPort), // start a server on the port
socketServer = io(server); // create a socket server using the express server
//initialize serial port initialization
var serialport = require('serialport'), // include the serialport package
SerialPort = serialport.SerialPort, // make a local instance of serial port package
portName = process.argv[2], // retrieve the port name from the command line argument
portConfig = {
//baudRate: 9600,
baudRate: BaudRate,
// call myPort.on('data') when a newline is received:
parser: serialport.parsers.readline('n')
};
// open the serial port
var myPort = new SerialPort(portName, portConfig);
//set up server and socketServer listener functions:
//app.use(express.static('d:/sp')); // serve files from the public folder
app.use(express.static(DocumentPath)); // serve files from the public folder
//app.use(express.static('c:/xampp/htdocs'));
app.get('/:name', serveFiles); // listener for all static file requests
socketServer.on('connection', openSocket); // listener for websocket data
function serveFiles(request, response) {
var fileName = request.params.name; // get the file name from the request
response.sendFile(fileName); // send the file
//res.sendFile('d:/sp/' fileName , { root : __dirname});
}
function openSocket(socket){
console.log('new user address: ' socket.handshake.address);
// send something to the web client with the data:
socket.emit('message', 'Server listening on address : ' socket.handshake.address);
// this function runs if there's input from the serialport:
myPort.on('data', function(data) {
socket.emit('message', data); // send the data to the client
});
}
Этот блог содержит еще три файла
c:/nodejs/node/client.js
c:/nodejs/index.html
D:/xampp/htdocs/data_window.php