Function code Inside settings.js

#1

Sorry for my poor english…
I have a function created inside settings using promisses and i need return async msg. howto make work?

PortaPinPad = (msg,os) => {
return new Promise((resolve, reject) => {
SerialPort.list()
.then(function(ports) {
msg.portas = ports;
global.portas = ports;
var porta = “”; // ‘\\.\COM6’
var fabricante = “”;
var vendorid = “”
var productid = “”
if (os.platform() == “win32”) {
porta = ‘\\.\’ + ports[0].comName;
fabricante = ports[0].manufacturer
vendorid = ports[0].vendorId
productid =ports[0].productId
var portaNum = porta.replace(/[^0-9]+/, ‘’);
}
msg.PPC_FindPort= { portaNum : portaNum , porta : porta , fabricante:fabricante,vendorid:vendorid,productid:productid }
global.PPC_FindPort = { portaNum : portaNum , porta : porta , fabricante:fabricante,vendorid:vendorid,productid:productid }
console.log("\nPlatform:%s porta:%s %s", os.platform(), porta,portaNum)
//}
console.log(‘serial port success’);
resolve( msg );
}).catch(function(err) {
console.log(‘serial port error %s’,err);
msg.portas = [];
global.portas = [];
resolve( msg );
});
})
}

function FormataTx( buffer) {
var CRC16 = require(“crc16”);
if (buffer.substr(0,3) == “OPN”){
//console.dir(global.PPC_FindPort)
var fabricante = global.PPC_FindPort.fabricante
if (fabricante.indexOf(“INGENICO”) > -1) {
buffer = buffer.substr(0,3)
}else {
buffer += global.PPC_FindPort.portaNum
}
}
var bufferTX = buffer+"\x17"
var crc = CRC16(bufferTX)
var message = Buffer.from( “\x16”+bufferTX+" " );
message.writeUInt16BE(crc, bufferTX.length+1)
return message;
}

function TesteNode(msg) {
var os = require(‘os’);
msg.message = “OPN0020”;
var initializePromisePorta = PortaPinPad( msg,os );
initializePromisePorta.then( function (result) {
console.log("\n\n4906 %s %s" , result.PPC_FindPort.porta , result.message)
result.dadosTX = FormataTx(result.message);
console.dir( result ); // <<<<<<<<<< I wish return this msg >>>>>>>>>>>>>>>>>>>>>>
return result;
});

}

///////////////////////////////////
And I use this way…

var TesteNode = global.get(‘TesteNode’);
TesteNode(msg);
return msg;

0 Likes

#2

The documentation here shows how to send messages asynchronously from within a Function node.

0 Likes

#3

Thanks a lot. My problem was sync/async code. It’s works now

0 Likes