Serial Port Control

Hi everyone,

I'm having trouble using the node-red-node-serialport node at 0.8.3 on Node-RED 0.20.5

I try to connect a device (Multiverter) with a usb-serial adapter. With minicom I can control the device at /dev/ttyUSB0 with 115200-8N1 and Hardware- and Software Flow Control disabled (should be for the device).
I see in Node-RED the tag "connected" under the serial node and "disconnected" when I pull out the USB connector but when I try to send a command as payload it just times out.
I already got something out of the serial input node after I sent a command into the device via minicom.

As far as I analyzed the problem the only difference to minicom is Hardware and Software Flow Control which I can't find how to disable in Node-RED. Any advice over there?

I don't think it would be helpful but here's my flow:

[{"id":"144b9afa.7ca115","type":"tab","label":"Multiverter","disabled":false,"info":""},{"id":"af5f1367.a5fb8","type":"inject","z":"144b9afa.7ca115","name":"","topic":"","payload":"help","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":480,"wires":[["9255fcc4.f772e","66aaf885.659488"]]},{"id":"640ccb7b.19dfc4","type":"debug","z":"144b9afa.7ca115","name":"serial return","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":670,"y":460,"wires":[]},{"id":"9255fcc4.f772e","type":"serial request","z":"144b9afa.7ca115","name":"","serial":"92c4fb6c.c8bcd8","x":520,"y":320,"wires":[["640ccb7b.19dfc4"]]},{"id":"66aaf885.659488","type":"debug","z":"144b9afa.7ca115","name":"Sent","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":430,"y":420,"wires":[]},{"id":"92c4fb6c.c8bcd8","type":"serial-port","z":"","serialport":"/dev/ttyUSB0","serialbaud":"115200","databits":"8","parity":"none","stopbits":"1","waitfor":"","newline":"\\n","bin":"false","out":"char","addchar":"","responsetimeout":"3000"}]

Cheers!

what version of Nodejs are you using?

Node-RED does not enable flow control.

Node.js is on Version 10.16.3

you may need to add "\r" or "\n" to the end of any command you send

Tried that, does sadly not help.
I thought of that because in minicom I (obviously) have to hit return, but that doesn't help here.

[{"id":"144b9afa.7ca115","type":"tab","label":"Multiverter","disabled":false,"info":""},{"id":"af5f1367.a5fb8","type":"inject","z":"144b9afa.7ca115","name":"","topic":"","payload":"help \n","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":480,"wires":[["9255fcc4.f772e","66aaf885.659488"]]},{"id":"640ccb7b.19dfc4","type":"debug","z":"144b9afa.7ca115","name":"serial return","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":670,"y":460,"wires":[]},{"id":"9255fcc4.f772e","type":"serial request","z":"144b9afa.7ca115","name":"","serial":"92c4fb6c.c8bcd8","x":490,"y":320,"wires":[["640ccb7b.19dfc4"]]},{"id":"66aaf885.659488","type":"debug","z":"144b9afa.7ca115","name":"Sent","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":430,"y":420,"wires":[]},{"id":"cbe578b1.9c4c88","type":"inject","z":"144b9afa.7ca115","name":"","topic":"","payload":"help \r","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":420,"wires":[["9255fcc4.f772e","66aaf885.659488"]]},{"id":"92c4fb6c.c8bcd8","type":"serial-port","z":"","serialport":"/dev/ttyUSB0","serialbaud":"115200","databits":"8","parity":"none","stopbits":"1","waitfor":"","newline":"\\n","bin":"false","out":"char","addchar":"","responsetimeout":"3000"}]

Hi - well neither of those will work... for as start it is \r not /r.
second if you use an inject like that it will send the string /r (or \r) and not the return character. First you should try the serial config -

Yeah I realized the wrong slash after I exported it for here.

I tried it in the serial config though but it wont work either.

Now that's what I did this time.

[{"id":"4d794a3.d4b52b4","type":"serial request","z":"23060d0.15a20f4","name":"","serial":"92c4fb6c.c8bcd8","x":490,"y":320,"wires":[["d0ee70a.0313b9"]]},{"id":"92c4fb6c.c8bcd8","type":"serial-port","z":"","serialport":"/dev/ttyUSB0","serialbaud":"115200","databits":"8","parity":"none","stopbits":"1","waitfor":"","newline":"\\n","bin":"false","out":"char","addchar":"\\n","responsetimeout":"3000"}]

I just ran into this issue the other day. I couldn't get serial control to work for a particular device that needed a newline at the end of each command.

Setting the Serial Out Node to add the "\n" character to output messages did not work. I confirmed by connecting the output of the node to a serial port on my laptop and reading the output in real time. It simply appended the text to the String.

For example, if I sent a command that reads "POWR 1", and then a command that reads "VOLM 21" in the picocom terminal this came through as:

POWR 1\nVOLM 21\n

So instead I added this in a function node before the serial out node:

msg.payload = msg.payload+"\n"
return msg;

This gives me :

POWR 1
------------VOLM 21
(The hyphens are in place of white spaces because the forum will not display it properly otherwise)

So the serial node is not adding the character as I would have expected but the function node does. Because I am OCD I changed it to:

msg.payload = msg.payload+"\r"+"\n"
return msg;

Now I get:

POWR 1
VOLM 21

It's working fine this way. Might be worth a try.

2 Likes

You are my man. That's working for me fine.

I felt like that could be the problem for days because it didn't seem to send the command properly since it just timed down.

So really thank you, that's awesome!

1 Like

just for clarity - was it the "\r" that worked - or did it need both +"\r\n" ?
(as the former should have worked inside the serial node config... - so will need to be investigated)

Actually - yes it looks like a bug... now fixed in version 0.8.5 on npm.
in the config adding \r should now work... (without need for function node) - would be great if you can confirm. Thanks.

I used the \r to get it working!

Will confirm the 0.8.5 in a minute, when i got time for it!

So: I updated the serial node.

When I connect the inject node directly to the serial node where I added the \r (bypassing the function node) they stop timeouting like before.

[{"id":"144b9afa.7ca115","type":"tab","label":"Multiverter","disabled":false,"info":""},{"id":"af5f1367.a5fb8","type":"inject","z":"144b9afa.7ca115","name":"","topic":"","payload":"preset recall 3","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":480,"wires":[["e562c5d6.aef208"]]},{"id":"9255fcc4.f772e","type":"serial request","z":"144b9afa.7ca115","name":"","serial":"92c4fb6c.c8bcd8","x":490,"y":280,"wires":[["b8a16458.0f82a8"]]},{"id":"66aaf885.659488","type":"debug","z":"144b9afa.7ca115","name":"Sent","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":450,"y":340,"wires":[]},{"id":"cbe578b1.9c4c88","type":"inject","z":"144b9afa.7ca115","name":"","topic":"","payload":"help","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":440,"wires":[["e562c5d6.aef208"]]},{"id":"461da1a2.ae5b7","type":"inject","z":"144b9afa.7ca115","name":"","topic":"","payload":"preset recall 1","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":520,"wires":[["9255fcc4.f772e"]]},{"id":"b603dddf.b1375","type":"inject","z":"144b9afa.7ca115","name":"","topic":"","payload":"volume 15","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":560,"wires":[["9255fcc4.f772e"]]},{"id":"b8a16458.0f82a8","type":"debug","z":"144b9afa.7ca115","name":"return","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":870,"y":300,"wires":[]},{"id":"990b57a7.1b54a8","type":"udp in","z":"144b9afa.7ca115","name":"","iface":"","port":"34421","ipv":"udp4","multicast":"false","group":"","datatype":"utf8","x":160,"y":220,"wires":[["e562c5d6.aef208"]]},{"id":"e562c5d6.aef208","type":"function","z":"144b9afa.7ca115","name":"","func":"msg.payload = msg.payload+\"\\r\"\nreturn msg;","outputs":1,"noerr":0,"x":370,"y":180,"wires":[["9255fcc4.f772e","66aaf885.659488"]]},{"id":"92c4fb6c.c8bcd8","type":"serial-port","z":"","serialport":"/dev/ttyUSB0","serialbaud":"115200","databits":"8","parity":"none","stopbits":"1","waitfor":"","newline":"300","bin":"false","out":"time","addchar":"\\r","responsetimeout":"300"}]

That's my flow now.
While the upper two injects work flawless the lower two just timeout like before (but work if I connect them back to the function node of course).

1 Like

Thanks for testing.. ok ... 0.8.6... now there to try...
apologies for half a fix.

2 Likes

Yip that works! Thanks for helping out!

Thanks for bringing that to our attention.

Tried 4 days thinking it's about me beeing not firm with node-red right now but I'm glad that it wasn't me beeing dumb or so. :stuck_out_tongue:

Wrap things like that in triple back-ticks or use the "preformated text" icon in the editor toolbar.

image

Im trying the /r after i send a hex buffer and i get the following error..

TypeError [ERR_INVALID_ARG_TYPE]: The "list[1]" argument must be one of type Array, Buffer, or Uint8Array. Received type string

Anyone know what I am doing wrong here?

[{"id":"f042e293.4bd948","type":"tab","label":"Flow 5","disabled":false,"info":""},{"id":"be2f3e94.08e27","type":"serial out","z":"f042e293.4bd948","name":"","serial":"6316eb55.80e3a4","x":690,"y":280,"wires":[]},{"id":"2d54136f.d45514","type":"inject","z":"f042e293.4bd948","name":"Set to EOL","topic":"","payload":"[\"0x02\",\"0x01\",\"0x01\",\"0x41\",\"0x40\",\"0x00\",\"0x08\",\"0x41\",\"0x1E\",\"0x90\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x82\",\"0x03\",\"0x45\"]","payloadType":"bin","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":440,"y":280,"wires":[["52f778b2.1c696"]]},{"id":"b9937022.701d4","type":"inject","z":"f042e293.4bd948","name":"Set Normal","topic":"","payload":"[\"0x02\",\"0x01\",\"0x01\",\"0x41\",\"0x40\",\"0x00\",\"0x08\",\"0x41\",\"0x1E\",\"0x17\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x83\",\"0x03\",\"0xC3\"]","payloadType":"bin","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":430,"y":320,"wires":[["be2f3e94.08e27"]]},{"id":"43164026.d98908","type":"inject","z":"f042e293.4bd948","name":"MainBoard-Relay1Off","topic":"","payload":"[\"0x02\",\"0x01\",\"0x01\",\"0x41\",\"0x40\",\"0x00\",\"0x22\",\"0x20\",\"0x12\",\"0x00\",\"0x5A\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x03\",\"0x4A\"]","payloadType":"bin","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":400,"y":420,"wires":[["be2f3e94.08e27"]]},{"id":"9f0a0b28.d830b","type":"inject","z":"f042e293.4bd948","name":"MainBoard-Relay2On","topic":"","payload":"[\"0x02\",\"0x01\",\"0x01\",\"0x41\",\"0x40\",\"0x00\",\"0x22\",\"0x20\",\"0x12\",\"0x00\",\"0x5A\",\"0x00\",\"0x00\",\"0x00\",\"0x01\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x03\",\"0x4B\"]","payloadType":"bin","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":400,"y":460,"wires":[["be2f3e94.08e27"]]},{"id":"47cfc409.596604","type":"inject","z":"f042e293.4bd948","name":"MainBoard-Relay1On","topic":"","payload":"[\"0x02\",\"0x01\",\"0x01\",\"0x41\",\"0x40\",\"0x00\",\"0x22\",\"0x20\",\"0x12\",\"0x00\",\"0x5A\",\"0x00\",\"0x01\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x03\",\"0x4B\"]","payloadType":"bin","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":400,"y":380,"wires":[["52f778b2.1c696"]]},{"id":"13c53829.be0b88","type":"inject","z":"f042e293.4bd948","name":"fake mqtt Set to end of line mode","topic":"","payload":"0201 0141 4000 0841 1E90 0000 0000 8203 45","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":500,"wires":[["d12565c9.4d6cf8"]]},{"id":"d12565c9.4d6cf8","type":"function","z":"f042e293.4bd948","name":"Hex string to Hex buffer","func":"msg.oldPayload = msg.payload; //save it for later debugging\nvar noSpaces = msg.payload.replace(/ /g, '');// remove spaces\nmsg.payload = Buffer.from(noSpaces, \"hex\"); //convert to buffer\nreturn msg;","outputs":1,"noerr":0,"x":410,"y":520,"wires":[["be2f3e94.08e27"]]},{"id":"c8b308cb.efb1b","type":"inject","z":"f042e293.4bd948","name":"fake mqtt MainBoard-Relay1On","topic":"","payload":"0201 0141 4000 2220 1200 5A00 0100 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0003 4B","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":540,"wires":[["d12565c9.4d6cf8"]]},{"id":"52f778b2.1c696","type":"serial request","z":"f042e293.4bd948","name":"","serial":"6316eb55.80e3a4","x":690,"y":360,"wires":[["c03cb9b3.b9d49"]]},{"id":"c03cb9b3.b9d49","type":"debug","z":"f042e293.4bd948","name":"","active":true,"tosidebar":true,"console":true,"tostatus":false,"complete":"true","targetType":"full","x":870,"y":360,"wires":[]},{"id":"6316eb55.80e3a4","type":"serial-port","z":"","serialport":"/dev/ttyUSB0","serialbaud":"115200","databits":"8","parity":"none","stopbits":"1","waitfor":"","dtr":"none","rts":"none","cts":"none","dsr":"none","newline":"\\n","bin":"false","out":"char","addchar":"\\r","responsetimeout":"10000"}]