How do I use serialport node to write to a RS485

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 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?

just give it a go - see what happens.

inject (set some data in the payload) :point_right: serial request (setup the correct com settings) :point_right: 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?)

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

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()

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) :point_right: serial request (setup the correct com settings) :point_right: debug node (see if you get a reply)

They have buffers (which are byte arays)

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

example...
image

↑ Not a bit of JavaScript in sight

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

[{"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"}]

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

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

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

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

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