0
Speak people, I would like to know how I can use the data received from the page to use outside the app.?
var SerialPort = require('serialport');
var Readline = SerialPort.parsers.Readline;
var methodOverride = require('method-override');
//faz requerimento dos modulos
var serveStatic = require('serve-static');
const express = require('express');
const socketIo = require('socket.io');
const http = require('http');
var path = require('path');
//cria servidor
const app = express();
const server = http.createServer(app);
const io = socketIo.listen(server);
//starta servidor na porta 3000
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'
}));
});
var bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({ extended: true });
app.route('/signup');
app.post( '/signup', urlencodedParser, function(req, res) {
var portCOM = req.body.portCOM;
var bauRATE = req.body.bauRATE;
res.json(
{
message: 'signup success',
portCOM : portCOM,
bauRATE : bauRATE,
}
);
});
app.get(function(req,res){
res.json({message: 'get request from signup'});
});
// configurações da comunicação serial
port = new SerialPort(portCOM, {
baudRate: 9600,
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}));
//EXIBE OS LOG NO TERMINAL, INFORMAÇOES VINDA DO ARDUINO
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);
});
Isn’t it enough to just call a function by passing req? Or maybe what you want to do is capture and treat these arguments before you arrive in the app.post with a middleware? Can you describe what you’re trying to do?
– Andre
I’m going through some parameters on the client side to connect to a serial port, so I need to take these parameters that are stored inside the app.post and treat them outside the app.post
– Leonardo Silva
when I try to use the portCOM variable outside the app.post I have the return portCOM is not defined.
– Leonardo Silva
I still don’t understand what the difficulty is. Don’t just send
portCOM
orres
as parameter to another function? You can edit your question by posting the full code, including the part that gives error?– Andre
posted the complete code, in the part that configure the connection to the port COM I can not use the variable.
– Leonardo Silva