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:
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:
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 aserial in Block:
My Current Node-Red Solution that dont work:
I Inject this:
msg.payload = "RF8 S\r\n"
Function1 Block:
var buf = Buffer.from(msg.payload, "utf-8");
//msg.payload = msg.payload.toString('latin1');
msg.payload = buf;
return msg;