Serialport node reading data without line break

Asked

Viewed 162 times

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.

  • I tried to define a delimiter, but it didn’t work !!

  • I think I could solve it if I knew how to read the beginning of the message and the end.

  • I was able to solve the problem.

1 answer

0

I was able to solve the problem using Bytelength Parser, who needs a look at the documentation.

https://serialport.io/docs/en/api-parser-byte-length

follow example, code this mess I’m new to this, so I’m working on it still, but I believe it helps.

var SerialPort = require('serialport');
var Readline = SerialPort.parsers.Readline;
const Ready = require('@serialport/parser-ready')

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

const port = new SerialPort('COM3', {
	baudRate: 115200,
    dataBits: 8,
    paridade: 'nenhum' ,
    stopBits: 1,
    flowControl: false ,
    parser: new SerialPort.parsers.Readline('\r') 	
});

//define um limite para acionar um evento
const ByteLength = require('@serialport/parser-byte-length');
const parser = port.pipe(new ByteLength({length: 4}))

server.listen(3000, () => {
	console.log('Servidor Online na porta 3000');
});


//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');
	app.use(serveStatic(path.join(__dirname, 'public'), {
		maxAge: '1d'
		}));
});
	
	port.open(function()
	{
		console.log('Porta aberta');
	});
	var ModbusTCP = "";
	parser.on('data', function (data)
	{		
			ModbusTCP = data.toString();
			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()
			});
	});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.