# Problem with sending of commends to Serial Connection

**URL:** <https://discourse.nodered.org/t/problem-with-sending-of-commends-to-serial-connection/74858>\
**Category:** General\
**Created:** [7 February 2023 18:43 UTC](https://discourse.nodered.org/t/problem-with-sending-of-commends-to-serial-connection/74858 "2023-02-07T18:43:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![blanpa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/blanpa/32/75719_2.png) [@blanpa](https://discourse.nodered.org/u/blanpa)\
**Post date:** [7 February 2023 18:43 UTC](https://discourse.nodered.org/t/problem-with-sending-of-commends-to-serial-connection/74858/1 "2023-02-07T18:43:06Z")

</div>

Hello all,

I am relatively new to using Node-Red. I want to connect an external device via a USB RS232 interface to send and read commands. Connection and readout works fine, but sending commands causes me problems. For comparison with a Python script I can send data and get my desired information, that works. With Node-Red I can use the port equally for reading as with the Python Scrypt. But when I try to send the string I get no response. I am sure that the error is in the processing before sending the message.

I want to send the string "RF8 S\r\n" to my device to get the response.

What am I doing wrong? Can anyone help me?

# Here is my Python Script:

```auto

import time
import serial

# configure the serial connections (the parameters differs on the device you are connecting to)
ser = serial.Serial(
    port='/dev/ttyUSB1',
    baudrate=9600,
    timeout=1,
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS
)
ser.isOpen()
# Reading the data from the serial port. This will be running in an infinite loop.

while 1 :
        # get keyboard input
        test = "RF8 S\r\n"
        encoded = test.encode()
        print(f"encoded: {encoded}")
        ser.write(encoded)
        bytesToRead = ser.inWaiting()
        data = ser.read(bytesToRead)
        time.sleep(1)
        print(f"data: {data}")

```

# Output with Python Script:

```auto
encoded: b'RF8 S\r\n'
data: b'0112102\r\n'
encoded: b'RF8 S\r\n'
data: b'0112102\r\n'
encoded: b'RF8 S\r\n'
data: b'0112102\r\n'
encoded: b'RF8 S\r\n'
data: b'0112102\r\n'
encoded: b'RF8 S\r\n'
data: b'0112102\r\n'
encoded: b'RF8 S\r\n'

```

Output while just Sending with running Python Script and a **serial in** Block:

 ![Empfang](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/5/05304447244e45e8178bffaebf859328240b9ed9.png)

# My Current Node-Red Solution that dont work:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/4/040eaa097b590bb7cdd683cedd9f4dba1b98f0fd.png)

I Inject this:  
`msg.payload = "RF8 S\r\n"`

# Function1 Block:

```auto

var buf = Buffer.from(msg.payload, "utf-8");

//msg.payload = msg.payload.toString('latin1');

msg.payload = buf;

return msg;

```

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [7 February 2023 21:46 UTC](https://discourse.nodered.org/t/problem-with-sending-of-commends-to-serial-connection/74858/2 "2023-02-07T21:46:28Z")

</div>

> [@blanpa](#):
>
> What am I doing wrong? Can anyone help me?

Please provide the flow data here - so one can check what's happening.  
If you're unsure how to do this: Make sure your flow-in-question is currently shown, then open the Hamburger menu (top right corner), click `Export`, select `Current flow`, then press the `Copy to clipboard` button; close the dialog & then paste the data here. Please surround it with the `Preformatted Text` function in the button bar of this editor window; otherwise it's almost unreadable...

---

<div class="post-metadata">

**Author:** ![magicfire](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/magicfire/32/93420_2.png) [@magicfire](https://discourse.nodered.org/u/magicfire)\
**Post date:** [8 February 2023 05:15 UTC](https://discourse.nodered.org/t/problem-with-sending-of-commends-to-serial-connection/74858/3 "2023-02-08T05:15:55Z")

</div>

Try to modify your code in **function 1** node to something like this

```auto
var buf = Buffer.from([0x52, 0x46, 0x38, 0x20, 0x53 , 0x0D, 0x0A]);
msg.payload = buf;
return msg;

```

And I am not sure if the **serial request node** can work together with the **serial out node** , so why not delete one of them and try.

By the way, a corresponding post has been discussed in this forum already.  
[How to send this HEX command to my device using Node-red - General - Node-RED Forum (nodered.org)](https://discourse.nodered.org/t/how-to-send-this-hex-command-to-my-device-using-node-red/74419/32)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [8 February 2023 09:56 UTC](https://discourse.nodered.org/t/problem-with-sending-of-commends-to-serial-connection/74858/4 "2023-02-08T09:56:54Z")

</div>

Don't connect both a request node and output and input nodes at the same time. Either connect a request node, or a pair on Out/In

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [8 February 2023 10:58 UTC](https://discourse.nodered.org/t/problem-with-sending-of-commends-to-serial-connection/74858/5 "2023-02-08T10:58:52Z")

</div>

And, in fact, delete the ones you are not using.

---

<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:** [22 February 2023 10:59 UTC](https://discourse.nodered.org/t/problem-with-sending-of-commends-to-serial-connection/74858/6 "2023-02-22T10:59:07Z")

</div>

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