# Send Array with serial out

**URL:** <https://discourse.nodered.org/t/send-array-with-serial-out/17558>\
**Category:** General\
**Created:** [5 November 2019 17:23 UTC](https://discourse.nodered.org/t/send-array-with-serial-out/17558 "2019-11-05T17:23:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![alpha9](https://avatars.discourse-cdn.com/v4/letter/a/7ba0ec/32.png) [@alpha9](https://discourse.nodered.org/u/alpha9)\
**Post date:** [5 November 2019 17:23 UTC](https://discourse.nodered.org/t/send-array-with-serial-out/17558/1 "2019-11-05T17:23:13Z")

</div>

Hello everyone  
I'm trying to send a buffer or array, created in a function-block, with the serial-out-block.  
The problem now is, that the array is sent within the message.payload information. So the array-information are sourounded with "[", array-elements are separeted with ",". But i need only the "raw-array-informations" in uint8.

 ![Serial-print](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/3/3b189d3a3c6d15f00cb700c4648152e6e8ff6b58.png)  
In the picture you can see the data transmitted by node-red and recorded with an analyzer. Note the "[", the "," and that the array-elements are transmitted in ascii.

Node-Details:  
Function:  
(only allowed two insert to pics, sorry)

Serial out:  
 ![Serial%20out](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/a/af935b90feafb26e464b993f6ecea8674f42da4b.png)

Node-export:

```auto
[{"id":"e02fb919.30ccb8","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"98cb9d7a.3815d","type":"serial-port","z":"","serialport":"/dev/ttyUSB0","serialbaud":"115200","databits":"8","parity":"none","stopbits":"1","waitfor":"","newline":"15","bin":"bin","out":"time","addchar":"","responsetimeout":"10000"},{"id":"1c06d1e8.bdb2c6","type":"serial out","z":"e02fb919.30ccb8","name":"","serial":"98cb9d7a.3815d","x":680,"y":280,"wires":[]},{"id":"946d00db.4cdfa8","type":"function","z":"e02fb919.30ccb8","name":"Telegramm","func":"var telegramm= {};\n\n/* raw array*/\nvar data = new Array([\n 0x55,\n 0x0f,\n 3,\n 255,\n 244,\n 57,\n 126,\n 159,\n 0,\n 0x20,\n 0x40,\n 0x00,\n 0x00,\n 0x00,\n 0x00,\n 0xff,\n]);\n\n/* calculate the Checksum */\nvar uint8 = new Uint8Array(1);\nfor(i = 0; i < 15; i++)\n{\n uint8[0] += data[0][i];\n}\nuint8[0] = (uint8[0] ^ 0xff ) + 1;\ndata[0][15] = uint8[0];\ntelegramm.payload = data;\n\nreturn telegramm;\n","outputs":1,"noerr":0,"x":480,"y":280,"wires":[["1c06d1e8.bdb2c6","c4fd038a.a2672"]]},{"id":"45696429.06fa04","type":"inject","z":"e02fb919.30ccb8","name":"","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":280,"wires":[["946d00db.4cdfa8"]]},{"id":"c4fd038a.a2672","type":"debug","z":"e02fb919.30ccb8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":690,"y":340,"wires":[]},{"id":"275a69d.97b2896","type":"serial in","z":"e02fb919.30ccb8","name":"","serial":"98cb9d7a.3815d","x":220,"y":120,"wires":[["f163fcee.7d88d8"]]},{"id":"f163fcee.7d88d8","type":"debug","z":"e02fb919.30ccb8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":440,"y":120,"wires":[]}]

```

Thank you for your support.  
Julian

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [6 November 2019 22:03 UTC](https://discourse.nodered.org/t/send-array-with-serial-out/17558/2 "2019-11-06T22:03:27Z")

</div>

So you are building an array of ints and trying to send them as raw bytes over a serial line?  
I think that will work IF you convert the array into a buffer, somthing like this (untested):

```auto
var telegramm= {};
/* raw array*/
var data = [
    0x55,
    0x0f,
    3,
    255,
... etc ...
];

telegramm.payload = Buffer.from(data);
return telegramm;

```

Two slight issues -- by create a new empty object `telegramm` you will lose any other fields from the incoming `msg` object (which may not be a problem in this flow). Also, the use of `new Array([...])` has been deprecated in the Javascript language (or at least it seems to be not preferred), so you can just define your data in between square brackets to create the initial data array.

---

<div class="post-metadata">

**Author:** ![alpha9](https://avatars.discourse-cdn.com/v4/letter/a/7ba0ec/32.png) [@alpha9](https://discourse.nodered.org/u/alpha9)\
**Post date:** [11 November 2019 09:56 UTC](https://discourse.nodered.org/t/send-array-with-serial-out/17558/3 "2019-11-11T09:56:46Z")

</div>

Thank you for your solution  
This is my first time with javascript, so its really helpful to learn the do's and don'ts in javascript.  
I implemented your code and it works just fine.  
A first version to continue developing the code was like this:

```auto
msg.payload = new Buffer([data[0][0], data[0][1], data[0][2], data[0][3], data[0][4], data[0][5], data[0][6], data[0][7], data[0][8], data[0][9], data[0][10], data[0][11], data[0][12], data[0][13], data[0][14], data[0][15]]);

```

But shure, this was a poor, no scalable solution.  
Julian
