0
I am working on a project with serial data reading, using the Node Serialport library, but when printing the data on the console I have the data broken in several lines, example: send = "1000", I get in reading
Ceived = "1"
"00"
"0"
var SerialPort = require('serialport');
var Readline = SerialPort.parsers.Readline;
var parser = new Readline({delimiter:'\n\r'});
var serveStatic = require('serve-static');
const express = require('express');
const socketIo = require('socket.io');
const http = require('http');
var path = require('path');
const app = express();
const server = http.createServer(app);
const io = socketIo.listen(server);
server.listen(3000, () => {
console.log('Servidor Online na porta 3000');
});
const port = new SerialPort('COM3', {
baudRate: 115200,
dataBits: 8,
paridade: 'nenhum' ,
stopBits: 1,
flowControl: false ,
parser: new SerialPort.parsers.Readline('\n\r')
});
//define um diretorio commo public para acesso as propriedades
app.use(serveStatic(path.join(__dirname, 'public'), {
maxAge: '1d'
}));
// retorna index quano recebe requição
app.get('/', (req, res, next) => {
res.sendFile(__dirname + '/index.html');
});
port.open(function()
{
console.log('Porta aberta');
});
port.on('data', function (data)
{
ModbusTCP = data.toString('utf8');
ModbusTCP = ModbusTCP.replace(/(\r\n|\n|\r)/gm,"");
var infLeng = ModbusTCP.length;
console.log('tamanho do envio:',infLeng);
console.log('Valor Hexadeciaml: ' + ModbusTCP);
// cria conexao de dados entre o js e o index.html
io.emit('arduino:data', {
value: data.toString()
});
});
"0"
ps data is sent every 5 seconds by Arduin.
– Leonardo Silva
I tried to define a delimiter, but it didn’t work !!
– Leonardo Silva
I think I could solve it if I knew how to read the beginning of the message and the end.
– Leonardo Silva
I was able to solve the problem.
– Leonardo Silva