# Numeric value and serial communication

**URL:** https://discourse.nodered.org/t/numeric-value-and-serial-communication/88326
**Category:** Dashboard
**Created:** [28 May 2024 18:25 UTC](https://discourse.nodered.org/t/numeric-value-and-serial-communication/88326 "2024-05-28T18:25:45Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Nic](https://avatars.discourse-cdn.com/v4/letter/n/45deac/32.png) [@Nic](https://discourse.nodered.org/u/Nic)
#### Post date: [28 May 2024 18:25 UTC](https://discourse.nodered.org/t/numeric-value-and-serial-communication/88326/1 "2024-05-28T18:25:45Z")

</div>

Hi all,  
I'm quite newbye. I communicate via serial port with Arduino board, using Serial.read() function I am able to send single byte and Arduino works as "actuator" making many functions. Now I need to pass at Arduino a value inserted in my PC interface developed usind ui-dashboard nodes. Could someone advise me which is the best way to implement an easy communcation in order to pass integer values o string between Arduino and node-red?  
Thanks in advance.  
n

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [28 May 2024 19:24 UTC](https://discourse.nodered.org/t/numeric-value-and-serial-communication/88326/2 "2024-05-28T19:24:36Z")

</div>

Hi @Nic,

Sorry I can't quite grasp the last comment

_integer values o string between Arduino and node-red?_

Do you want to send a string containing numbers, or send numbers represented as bytes?

- As a string, send as is "12334456"
- As a collection of bytes

Lets assume a 32bit Integer

```auto
const num = 123456;
const buffer = Buffer.alloc(4); /* 4 bytes in a 32bit integer */
buffer.writeInt32BE(num, 0); /* Big Endian */
//buffer.writeInt32LE(num, 0); /* Little Endian */

msg.payload = buffer
return msg;

```

As for reading back integers passed to you as a byte array:

```auto
const int32 = msg.payload.readInt32BE(0); // Big Endian

```

For the 'easy way' - I do recommend however, looking at this node by our own @Steve-Mcl

> **[node-red-contrib-buffer-parser](https://flows.nodered.org/node/node-red-contrib-buffer-parser)**
>
> Node-red nodes to convert values to and from buffer/array. Supports Big/Little Endian, BCD, byte swapping and much more

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [28 May 2024 21:00 UTC](https://discourse.nodered.org/t/numeric-value-and-serial-communication/88326/3 "2024-05-28T21:00:55Z")

</div>

I suspect the better solution is to always send and receive JSON. That way you can add meta data and perform different operations depending on the meta info.

---

<div class="post-metadata">

### Author: ![Nic](https://avatars.discourse-cdn.com/v4/letter/n/45deac/32.png) [@Nic](https://discourse.nodered.org/u/Nic)
#### Post date: [30 May 2024 13:16 UTC](https://discourse.nodered.org/t/numeric-value-and-serial-communication/88326/4 "2024-05-30T13:16:44Z")

</div>

Hi, thanks for your advises. Finally I set the fw on arduino side in order to have something like a "metadata" where I include strings and integers. Working with the "string node" on node-red side I can divide the data in every part I need.  
Thank you for the support.  
n

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [30 May 2024 14:03 UTC](https://discourse.nodered.org/t/numeric-value-and-serial-communication/88326/5 "2024-05-30T14:03:40Z")

</div>

> [@Nic](#):
>
> Working with the "string node" on node-red side I can divide the data in every part I need.

That is really not necessary & you are really re-inventing a perfectly good wheel that almost all of the JavaScript community already use - it is called **JavaScript Object Notation** (or JSON for short).

There are LOTS (and lots and lots) of reasons to go with a well established data interchange format (which JSON is) but the most important reasons are:

- It is highly extensible (future changes to your data will NOT break your project)
- It is literally built into JavaScript (`JSON.parse` / `JSON.stringify`) and built into Node-RED (the JSON node)
- It already exists and it works well
- Almost EVERY programmer knows exactly how to work with it (as opposed to some custom formatted string you invent)
- etc
- etc

---

<div class="post-metadata">

### Author: ![Nic](https://avatars.discourse-cdn.com/v4/letter/n/45deac/32.png) [@Nic](https://discourse.nodered.org/u/Nic)
#### Post date: [30 May 2024 14:28 UTC](https://discourse.nodered.org/t/numeric-value-and-serial-communication/88326/6 "2024-05-30T14:28:51Z")

</div>

Steve, you are right but what you don't know is that I'm not able to work with JavaScript 😉

---

<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: [29 June 2024 14:29 UTC](https://discourse.nodered.org/t/numeric-value-and-serial-communication/88326/7 "2024-06-29T14:29:12Z")

</div>

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