# Sending HEX payload to serial connection with Serial Port Node

**URL:** <https://discourse.nodered.org/t/sending-hex-payload-to-serial-connection-with-serial-port-node/47684>\
**Category:** General\
**Created:** [25 June 2021 21:31 UTC](https://discourse.nodered.org/t/sending-hex-payload-to-serial-connection-with-serial-port-node/47684 "2021-06-25T21:31:49Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![juanh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/juanh/32/43958_2.png) [@juanh](https://discourse.nodered.org/u/juanh)\
**Post date:** [25 June 2021 21:31 UTC](https://discourse.nodered.org/t/sending-hex-payload-to-serial-connection-with-serial-port-node/47684/1 "2021-06-25T21:31:50Z")

</div>

I'm hoping someone can provide some guidance, and nudge me in the right direction. I have a Pylontech battery bank, as part of my solar power system.

I would like to use Node-RED to periodically poll the battery bank for operational parameters, via the RS232 Console port.

The instructions I have instruct the following:

> _First, the console has to be activated. To do this, switch the terminal program to 1200, 8, N, 1._
> 
> _Then send the following hex string to the battery:_
> 
> _Data: 7E 32 30 30 31 34 36 38 32 43 30 30 34 38 35 32 30 46 43 43 33 0D_
> 
> _Now switch to 115200,8, N, 1 .._
> 
> _Data: Send 0D 0A._
> 
> _The pylon\> prompt appears, which enables commands to be entered._
> 
> _First command e.g._
> 
> _help \ n (\ n = line feed character 0A)_
> 
> _shows some help_
> 
> _pwr \ n_

```auto
@

Power Volt Curr Tempr Tlow Thigh Vlow Vhigh Base.St Volt.St Curr.St Temp.St Coulomb Time BVSt BTSt

1 49884 4310 28000 25000 26000 3322 3327 Batch Normal Normal Normal 31% 2017 - 07 - 09 14:56: 37 Normal Normal

2 49832 4472 28000 25000 25000 3321 3323 Batch Normal Normal Normal 31% 2017 - 07 - 09 14:56: 36 Normal Normal

3 49842 4318 28000 25000 25000 3322 3324 Batch Normal Normal Normal 30% 2017 - 07 - 09 14:56: 36 Normal Normal

4 49858 4111 27000 23000 23000 3322 3325 Batch Normal Normal Normal 30% 2017 - 07 - 09 14:56: 36 Normal Normal

5 - ------ Absent - ------

6 - ------ Absent - ------

7 - ------ Absent - ------

8 - ------ Absent - ------

Command completed successfully

$$

```

> _The numbers are given as fixed point numbers and usually have to be divided by 1000._

I'm hoping to use the [node-red-node-serialport](https://flows.nodered.org/node/node-red-node-serialport) pallette for this, but I'm having a bit of difficulty figuring out how to do this. Any help is greatly appreciated.

---

<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:** [26 June 2021 07:39 UTC](https://discourse.nodered.org/t/sending-hex-payload-to-serial-connection-with-serial-port-node/47684/2 "2021-06-26T07:39:32Z")

</div>

Unfortunately I do not believe that you can change the baud rate at run time with the serial nodes. I think you may have to do something like using a Python script to interface with the device and pass the result to node-red. Perhaps the supplier provides a python script that you could use.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [26 June 2021 09:12 UTC](https://discourse.nodered.org/t/sending-hex-payload-to-serial-connection-with-serial-port-node/47684/3 "2021-06-26T09:12:19Z")

</div>

Or can you do the first part outside of Node-RED and then once into 115200 mode - just stay in that mode ? (and then use Node-RED)

---

<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:** [26 June 2021 10:22 UTC](https://discourse.nodered.org/t/sending-hex-payload-to-serial-connection-with-serial-port-node/47684/4 "2021-06-26T10:22:30Z")

</div>

Though that would mean stopping node red in order to release the port if the device needed re-initialising. Then restarting node-red once it was initialised.

---

<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:** [26 June 2021 10:25 UTC](https://discourse.nodered.org/t/sending-hex-payload-to-serial-connection-with-serial-port-node/47684/5 "2021-06-26T10:25:46Z")

</div>

If BAUD 1200 is fast enough for you and the `pwr\n` command works/responds, then stick with that (i.e. dont change speed)

Something like this might work...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/4/8427cf9f193709fef31770f2883dffd9aa49def2.png)

```auto
[{"id":"741a12ab7e85b435","type":"inject","z":"62f0c6d6.6621a8","name":"0D 0A - get command prompt","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[\"0x0D\", \"0x0A\"]","payloadType":"bin","x":1020,"y":460,"wires":[["155d22f0618d2958"]]},{"id":"1f5815eb4435c35c","type":"debug","z":"62f0c6d6.6621a8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1550,"y":460,"wires":[]},{"id":"8ddea5c7ce58bd41","type":"inject","z":"62f0c6d6.6621a8","name":"pwr - get power data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"pwr","payloadType":"str","x":990,"y":520,"wires":[["8173a6cfcbf7edca"]]},{"id":"155d22f0618d2958","type":"serial request","z":"62f0c6d6.6621a8","name":"","serial":"e6c39153.e8eab8","x":1370,"y":460,"wires":[["1f5815eb4435c35c"]]},{"id":"8173a6cfcbf7edca","type":"function","z":"62f0c6d6.6621a8","name":"add \\n","func":"msg.payload += \"\\n\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1190,"y":520,"wires":[["155d22f0618d2958"]]},{"id":"e6c39153.e8eab8","type":"serial-port","serialport":"COM3","serialbaud":"1200","databits":"8","parity":"none","stopbits":"1","waitfor":"","newline":"","bin":"false","out":"char","addchar":"","responsetimeout":"10000"}]

```

Obviously you will need to change com port accordingly.

---

<div class="post-metadata">

**Author:** ![juanh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/juanh/32/43958_2.png) [@juanh](https://discourse.nodered.org/u/juanh)\
**Post date:** [26 June 2021 11:54 UTC](https://discourse.nodered.org/t/sending-hex-payload-to-serial-connection-with-serial-port-node/47684/6 "2021-06-26T11:54:16Z")

</div>

Thanks for all the responses!

Initially I was just looking for an example of how to send the HEX payload, and didn't even yet realise the issue with changing the BAUD rate after the flow starts.

I'm trying to test if the PWR command will work with a rate of 1200 as @Steve-Mcl suggested, using a modified version of his flow, but getting the following:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/a/1a04b5e64fdfe9f5c9306e1631043ddf6b9f8484.png)

Here is how my flow is looking:

```auto
[{"id":"580e0e34.5a512","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"741a12ab7e85b435","type":"inject","z":"580e0e34.5a512","name":"0D 0A - get command prompt","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":"10","topic":"","payload":"[\"0x0D\", \"0x0A\"]","payloadType":"bin","x":410,"y":180,"wires":[["155d22f0618d2958"]]},{"id":"1f5815eb4435c35c","type":"debug","z":"580e0e34.5a512","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":910,"y":180,"wires":[]},{"id":"8ddea5c7ce58bd41","type":"inject","z":"580e0e34.5a512","name":"pwr - get power data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":"15","topic":"","payload":"pwr","payloadType":"str","x":380,"y":240,"wires":[["8173a6cfcbf7edca"]]},{"id":"155d22f0618d2958","type":"serial request","z":"580e0e34.5a512","name":"Serial Interface","serial":"e6c39153.e8eab8","x":720,"y":180,"wires":[["1f5815eb4435c35c"]]},{"id":"8173a6cfcbf7edca","type":"function","z":"580e0e34.5a512","name":"add \\n","func":"msg.payload += \"\\n\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":550,"y":240,"wires":[["155d22f0618d2958"]]},{"id":"c79aaf44.ecb93","type":"inject","z":"580e0e34.5a512","name":"Enable Console","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":"5","topic":"","payload":"[\"0x7E\",\"0x32\",\"0x30\",\"0x30\",\"0x31\",\"0x34\",\"0x36\",\"0x38\",\"0x32\",\"0x43\",\"0x30\",\"0x30\",\"0x34\",\"0x38\",\"0x35\",\"0x32\",\"0x30\",\"0x46\",\"0x43\",\"0x43\",\"0x33\",\"0x0D\"]","payloadType":"bin","x":360,"y":120,"wires":[["155d22f0618d2958"]]},{"id":"e6c39153.e8eab8","type":"serial-port","serialport":"/dev/ttyUSB0","serialbaud":"1200","databits":"8","parity":"none","stopbits":"1","waitfor":"","dtr":"none","rts":"none","cts":"none","dsr":"none","newline":"","bin":"false","out":"char","addchar":"","responsetimeout":"10000"}]

```

Is the _undefined_ payloads as a result of me doing something wrong, or due to the 1200 rate not working?

If the above isn't working, @dceejay 's solution of first activating the console externally might be the way to go, but I'm wondering if it will reset "activated console" everytime the serial port disconnects.

---

<div class="post-metadata">

**Author:** ![juanh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/juanh/32/43958_2.png) [@juanh](https://discourse.nodered.org/u/juanh)\
**Post date:** [26 June 2021 14:32 UTC](https://discourse.nodered.org/t/sending-hex-payload-to-serial-connection-with-serial-port-node/47684/7 "2021-06-26T14:32:55Z")

</div>

I tried connecting directly to the serial port using putty, and a few other serial programs and they all failed, so I suspect the cable I have is incompatible. Will try with a different cable/converter in the near future, and pick this part of the project up again after that. Thanks again for everyone's advice!

---

<div class="post-metadata">

**Author:** ![alexander77](https://avatars.discourse-cdn.com/v4/letter/a/3ec8ea/32.png) [@alexander77](https://discourse.nodered.org/u/alexander77)\
**Post date:** [26 June 2021 15:32 UTC](https://discourse.nodered.org/t/sending-hex-payload-to-serial-connection-with-serial-port-node/47684/8 "2021-06-26T15:32:28Z")

</div>

> [@juanh](#):
>
> Data: 7E 32 30 30 31 34 36 38 32 43 30 30 34 38 35 32 30 46 43 43 33 0D

I asked my ascii table and got that in true ascii:  
~20014682C0048520FCC3\n  
No need to send that as hex characters. Simpy send it as plain text.

---

<div class="post-metadata">

**Author:** ![juanh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/juanh/32/43958_2.png) [@juanh](https://discourse.nodered.org/u/juanh)\
**Post date:** [30 June 2021 13:57 UTC](https://discourse.nodered.org/t/sending-hex-payload-to-serial-connection-with-serial-port-node/47684/9 "2021-06-30T13:57:55Z")

</div>

Just an update. Managed to get the above to work by including the _msg.baudrate =_ property, which allowed me to change between baud rates on the fly, regardless of what baud rate was set on the serial node.

---

<div class="post-metadata">

**Author:** ![alexander77](https://avatars.discourse-cdn.com/v4/letter/a/3ec8ea/32.png) [@alexander77](https://discourse.nodered.org/u/alexander77)\
**Post date:** [1 July 2021 16:50 UTC](https://discourse.nodered.org/t/sending-hex-payload-to-serial-connection-with-serial-port-node/47684/10 "2021-07-01T16:50:00Z")

</div>

Thats great news! What did you send as property, simply 4800, 9600, 19200 aso?

---

<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:** [30 August 2021 16:50 UTC](https://discourse.nodered.org/t/sending-hex-payload-to-serial-connection-with-serial-port-node/47684/11 "2021-08-30T16:50:27Z")

</div>

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