# A buffer as payload from function node

**URL:** https://discourse.nodered.org/t/a-buffer-as-payload-from-function-node/94506
**Category:** General
**Tags:** function-node
**Created:** [10 January 2025 10:49 UTC](https://discourse.nodered.org/t/a-buffer-as-payload-from-function-node/94506 "2025-01-10T10:49:42Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![lbh00](https://avatars.discourse-cdn.com/v4/letter/l/8c91f0/32.png) [@lbh00](https://discourse.nodered.org/u/lbh00)
#### Post date: [10 January 2025 10:49 UTC](https://discourse.nodered.org/t/a-buffer-as-payload-from-function-node/94506/1 "2025-01-10T10:49:42Z")

</div>

Hello,

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

```auto
blReply =[217,181,0,75];
    for (i=0; i< blReply.length; i++){
        msg.payload = blReply[i];
        node.send(msg);
    }

```

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 ?

Regards  
lbh00

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [10 January 2025 10:55 UTC](https://discourse.nodered.org/t/a-buffer-as-payload-from-function-node/94506/2 "2025-01-10T10:55:13Z")

</div>

do you need to send one character at a time ? Why not just send the array as-is ? ie

```auto
msg.payload = [217,181,0,75];
node.send msg;

```

The serial port should send that as a binary payload.

---

<div class="post-metadata">

### Author: ![lbh00](https://avatars.discourse-cdn.com/v4/letter/l/8c91f0/32.png) [@lbh00](https://discourse.nodered.org/u/lbh00)
#### Post date: [10 January 2025 13:42 UTC](https://discourse.nodered.org/t/a-buffer-as-payload-from-function-node/94506/3 "2025-01-10T13:42:42Z")

</div>

Hello Dceejay,

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 ?

Regards  
lbh00

---

<div class="post-metadata">

### Author: ![lbh00](https://avatars.discourse-cdn.com/v4/letter/l/8c91f0/32.png) [@lbh00](https://discourse.nodered.org/u/lbh00)
#### Post date: [10 January 2025 14:41 UTC](https://discourse.nodered.org/t/a-buffer-as-payload-from-function-node/94506/4 "2025-01-10T14:41:44Z")

</div>

Hello,

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:

```auto
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:

```auto
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.

Regards  
lbh00

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [10 January 2025 16:07 UTC](https://discourse.nodered.org/t/a-buffer-as-payload-from-function-node/94506/5 "2025-01-10T16:07:29Z")

</div>

Aha - of course... yes - do exactly that.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [10 April 2025 16:08 UTC](https://discourse.nodered.org/t/a-buffer-as-payload-from-function-node/94506/6 "2025-04-10T16:08:15Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
