# How do I use serialport node to write to a RS485

**URL:** <https://discourse.nodered.org/t/how-do-i-use-serialport-node-to-write-to-a-rs485/63484>\
**Category:** General\
**Created:** [5 June 2022 13:40 UTC](https://discourse.nodered.org/t/how-do-i-use-serialport-node-to-write-to-a-rs485/63484 "2022-06-05T13:40:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![SP-Mushrooms](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sp-mushrooms/32/61710_2.png) [@SP-Mushrooms](https://discourse.nodered.org/u/SP-Mushrooms)\
**Post date:** [5 June 2022 13:40 UTC](https://discourse.nodered.org/t/how-do-i-use-serialport-node-to-write-to-a-rs485/63484/1 "2022-06-05T13:40:07Z")

</div>

Sorry for this qn but I cant find a solution for my problem

my problemis how to I write to a RS485 device using simple serial communication. I am trying to google search and look through the forum but I keep on finding post on the modbus node that I already tried that node for communication but doesn't work as the device send data in ttl? [https://docs.google.com/document/d/1mkupPSn9Fc5ugG-JKFVZB9q\_2JomUZusl-wHYM55wJ4/edit?usp=sharing](https://docs.google.com/document/d/1mkupPSn9Fc5ugG-JKFVZB9q_2JomUZusl-wHYM55wJ4/edit?usp=sharing) Here is my document for my device I am using.

I also tried node-red-contrib-serialport-rs485 0.0.6 but I can't install it due to compadability errors.

I am looking at node-red-node-serialport to see if it can work for simple serial comunication does anyone got any sample code that I can go of on?

---

<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:** [5 June 2022 14:06 UTC](https://discourse.nodered.org/t/how-do-i-use-serialport-node-to-write-to-a-rs485/63484/2 "2022-06-05T14:06:25Z")

</div>

> [@SP-Mushrooms](#):
>
> I am looking at node-red-node-serialport to see if it can work for simple serial comunication does anyone got any sample code that I can go of on?

just give it a go - see what happens.

inject (set some data in the payload) 👉 serial request (setup the correct com settings) 👉 debug node (see if you get a reply)

  

Other than that, we really cannot give good advise without further details like ...

- Can you communicate to device using something like putty? (to ensure the wiring is correct)
- What OS?
- What are you trying to write (sample data)? Is it a known protocol?
- Does the end device respond to commands or does it just "spit out data"?
- What is the serial port settings (COM1? /dev/ttyUSB1? what speed/start/stop bits etc?)

---

<div class="post-metadata">

**Author:** ![SP-Mushrooms](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sp-mushrooms/32/61710_2.png) [@SP-Mushrooms](https://discourse.nodered.org/u/SP-Mushrooms)\
**Post date:** [6 June 2022 12:45 UTC](https://discourse.nodered.org/t/how-do-i-use-serialport-node-to-write-to-a-rs485/63484/3 "2022-06-06T12:45:29Z")

</div>

Hello thanks for replying I am trying to format send data to a serial device I manage to find a python sample code for that device but since I am new to javascript I don't know how to convert the python to javascript. As javascript apparently don't have byte?

python code

```auto
import time
import serial
import struct
import binascii

ser=serial.Serial('/dev/ttyUSB5',baudrate=9600,parity=serial.PARITY_NONE,stopbits=serial.STOPBITS_ONE)

print(ser.isOpen())

string2 = '68 03 01 00 22 aa 04 9e '

data1 = bytearray.fromhex(string2)
for data in data1:
        hex_byte = ("{0:02x}".format(data))
        print (hex_byte)
        ser.write(bytearray.fromhex(hex_byte))

ser.close()

```

---

<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:** [6 June 2022 13:10 UTC](https://discourse.nodered.org/t/how-do-i-use-serialport-node-to-write-to-a-rs485/63484/4 "2022-06-06T13:10:53Z")

</div>

If you want to send `68 03 01 00 22 aa 04 9e` to the serial port then use either a SERIAL Request or SERIAL Out node.

Use an INJECT to send a buffer with `68 03 01 00 22 aa 04 9e`

As i said before...  
inject (set some data in the payload) 👉 serial request (setup the correct com settings) 👉 debug node (see if you get a reply)

> [@SP-Mushrooms](#):
>
> As javascript apparently don't have byte

They have buffers (which are byte arays)

> [@SP-Mushrooms](#):
>
> I am new to javascript I don't know how to convert the python to javascript

There is no JavaScript required for this - it is a "no code"

example...  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/e/9e7b0c08ed65e18968ecc29891696cb49ab6a0ba.png)

↑ Not a bit of JavaScript in sight

Demo flow (use `CTRL-I` to import it)...

```auto
[{"id":"9d6f8adbbaf48ea1","type":"inject","z":"b1e41431717e4e05","name":"[\"0x68\",\"0x03\",\"0x01\",\"0x00\",\"0x22\",\"0xaa\",\"0x04\",\"0x9e\"]","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[\"0x68\",\"0x03\",\"0x01\",\"0x00\",\"0x22\",\"0xaa\",\"0x04\",\"0x9e\"]","payloadType":"bin","x":290,"y":700,"wires":[["108f6d38a4465865"]]},{"id":"6306a144b714f63a","type":"serial in","z":"b1e41431717e4e05","name":"","serial":"85ed1317.7df08","x":150,"y":820,"wires":[["b539eb2d1e88055f"]]},{"id":"108f6d38a4465865","type":"serial out","z":"b1e41431717e4e05","name":"","serial":"85ed1317.7df08","x":150,"y":760,"wires":[]},{"id":"b539eb2d1e88055f","type":"debug","z":"b1e41431717e4e05","name":"from serial port","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":420,"y":820,"wires":[]},{"id":"4291d5a4222128a3","type":"inject","z":"b1e41431717e4e05","name":"[\"0x68\",\"0x03\",\"0x01\",\"0x00\",\"0x22\",\"0xaa\",\"0x04\",\"0x9e\"]","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[\"0x68\",\"0x03\",\"0x01\",\"0x00\",\"0x22\",\"0xaa\",\"0x04\",\"0x9e\"]","payloadType":"bin","x":290,"y":440,"wires":[["815d91315a9adf9f"]]},{"id":"815d91315a9adf9f","type":"serial request","z":"b1e41431717e4e05","name":"","serial":"85ed1317.7df08","x":150,"y":500,"wires":[["77c7937de8058de1"]]},{"id":"77c7937de8058de1","type":"debug","z":"b1e41431717e4e05","name":"from serial port","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":420,"y":500,"wires":[]},{"id":"de62fc3ed3c6c8be","type":"comment","z":"b1e41431717e4e05","name":"Using the Request / response pattern","info":"","x":230,"y":400,"wires":[]},{"id":"aa2abd9269d72d90","type":"comment","z":"b1e41431717e4e05","name":"Using the In / Out pattern","info":"","x":190,"y":660,"wires":[]},{"id":"85ed1317.7df08","type":"serial-port","serialport":"/dev/ttyUSB5","serialbaud":"9600","databits":"8","parity":"none","stopbits":"1","waitfor":"","dtr":"none","rts":"none","cts":"none","dsr":"none","newline":"200","bin":"bin","out":"interbyte","addchar":"","responsetimeout":"2000"}]

```

---

<div class="post-metadata">

**Author:** ![SP-Mushrooms](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sp-mushrooms/32/61710_2.png) [@SP-Mushrooms](https://discourse.nodered.org/u/SP-Mushrooms)\
**Post date:** [6 June 2022 14:05 UTC](https://discourse.nodered.org/t/how-do-i-use-serialport-node-to-write-to-a-rs485/63484/5 "2022-06-06T14:05:37Z")

</div>

Is there any way to speed up the process as the string I have will be longer and string will have more as I try to control more things with the serial port/device that is attached to the port

---

<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:** [6 June 2022 17:20 UTC](https://discourse.nodered.org/t/how-do-i-use-serialport-node-to-write-to-a-rs485/63484/6 "2022-06-06T17:20:25Z")

</div>

Your question is unclear.

You can send as much data as you need to (within the capabilities of the serial port settings).

You could adjust the "wait for 200ms silence" to a lower value. You could implement a known (or custom) protocol and use the END MARK instead of waiting for silence. You could set the Baud Rate higher.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/e/cea9555c63a2fd30b0e0e284e85dffc3191ae0a2.png)

---

<div class="post-metadata">

**Author:** ![SP-Mushrooms](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sp-mushrooms/32/61710_2.png) [@SP-Mushrooms](https://discourse.nodered.org/u/SP-Mushrooms)\
**Post date:** [7 June 2022 01:48 UTC](https://discourse.nodered.org/t/how-do-i-use-serialport-node-to-write-to-a-rs485/63484/7 "2022-06-07T01:48:50Z")

</div>

Sorry for my qn not being clear your answer worked but I wanted to ask for example my string was even longer like

`string2 = '68 03 01 00 22 aa 04 9e 04 4a c2 01 4a 3c 4a c2 01 4a 3c 4a 3c 4a 3c 4a 3c 4a c2 01 4a c2 01 4a 3c 4a 3c 4a c2 01 4a 3c 4a 3c 4a 3c 4a 3c 4a 3c 4a c2 01 4a 3c 4a 3c 4a 3c 4a 3c 4a 3c 49 c2 01 49 c2 01 49 c2 01 4a c2 01 49 c2 01 4a c2 01 4a c2 01 49 c2 01 4a c2 3 01 00 22 aa 04 9e 04 4a c2 01 4a 3c 4a c2 01 4a 3c 4a 3c 4a 3c 4a 3c 4a c2 01 4a c2 01 4a 3c 4a 3c 4a c2 01 4a 3c 4a 3c 4a 3c 4a 3c 4a 3c 4a c2 01 4a 3c 4a 3c 4a 3c 4a 3c 4a 3c 49 c2 01 49 c2 01 49 c2 01 4a c4 4a c2 01 4a 3c 4a c2 01 4a 3c 4a 3c 4a 3c 4a 3c 4a c2 01 4a c2 01 4a 3c 4a 3c 4a c2 01 4a 3c 4a 3c 4a 3c 4a 3c 4a 3c 4a c2 01 4a 3c 4a 3c 4a 3c 4a 3c 4a 3c 49 c2 01 49 c2 01 49 c2 01 4a ca c2 01 4a 3c 4a 3c 4a 3c 4a 3c 4a c2 01 4a c2 01 4a 3c 4a 3c 4a c2 01 4a 3c 4a 3c 4a 3c 4a 3c 4a 3c 4a c2 01 4a 3c 4a 3c 4a 3c 4a 3c 4a 3c 49 c2 01 49 c2 01 49 c2 01 4a c2 01 49 c2 01 4a c2 01 4a c2 01 49 c2 01 4a c2 3 01 00 22 aa 04 9e 04 4a c2 01 4a 3c 4a c2 01 4a 3c 4a 3c 4a 3c 4a 3c 4a c2 01 4a c2 01 4a 3c 4a 3c 4a c2 01 4a 3c 4a 3c 4a 3c 4a 3c 4a 3c 4a c2 01 4a 3c 4a 3c 4a 3c 4a 3c 4a 3c 49 c2`

How to do but I realise I can easily do that by doing another code like my python code below

```auto
y = "String put here"

spaces_string = y.replace(' ', '", " 0x')

print(spaces_string)
#68", " 0x03", " 0x45", " 0x16 (output)
#example need to add a | ["0x | at the frount and| "] | at the back

```

---

<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:** [21 June 2022 01:49 UTC](https://discourse.nodered.org/t/how-do-i-use-serialport-node-to-write-to-a-rs485/63484/8 "2022-06-21T01:49:40Z")

</div>

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