I have a flow in Node-red in which a function will have to send a binary message to the serial port node.
I set up the function node with code similar to this
When I receive the data on the serial port , the data received is actually a string (ASCII encoded)
the string received on the other end of the serial port is actually [217,181,0, 75].
I was expecting to receive the binary equivalent of the integers 217 181 0 and 75.
When I inject using the inject node the buffer [217,181,0, 75] the data recieved on the other end of the serial port is as expected the integers 217, 181, 0 and 75.
I Can't see what is wrong in the function node code, can you please help me out with this ?
Thanks for your reply.
Well sending 1 byte after the other was one way of debugging to understand what is going on.
I retried your proposal and I still get the data as an ASCII encoded message.
I connected a debug node to snoop on the data transiting from the function node to the serialport node I notice some differences:
the data sent from the function node the msg.payload is typed as array, while the msg.payload injected by the inject node is typed as buffer. What is the difference ?
I looked at the code of serialport node, and apparently if the typeof msg.payload is not a buffer, serial port will convert it to a string. a snip from the serialport node code:
encodePayload:
function (payload) {
if (!Buffer.isBuffer(payload)) {
if (typeof payload === "object") {
payload = JSON.stringify(payload);
}
else {
payload = payload.toString();
}
if (addchar !== "") { payload += addchar; }
}
else if (addchar !== "") {
payload = Buffer.concat([payload,Buffer.from(addchar)]);
}
return payload;
},
so what I did, is convert the array to a buffer inside my function as follows:
var payload = Buffer.from([217,181,0,75])
then send it to serialport node.
I hope this helps someone who is having this issue too.