# Help with rs232

**URL:** <https://discourse.nodered.org/t/help-with-rs232/28254>\
**Category:** General\
**Created:** [12 June 2020 19:59 UTC](https://discourse.nodered.org/t/help-with-rs232/28254 "2020-06-12T19:59:42Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![mike1976](https://avatars.discourse-cdn.com/v4/letter/m/a698b9/32.png) [@mike1976](https://discourse.nodered.org/u/mike1976)\
**Post date:** [12 June 2020 19:59 UTC](https://discourse.nodered.org/t/help-with-rs232/28254/1 "2020-06-12T19:59:43Z")

</div>

Hi,

can someone please help me with rs232 communication?

Have a scale that has an RS232 interface.  
Unfortunately I have no idea how I can accomplish the commands I send and receive values.

```auto
[{"id":"f0190195.7939f8","type":"serial in","z":"5b6e97cb.a67bf8","name":"","serial":"d606e68c.2e18b","x":140,"y":400,"wires":[["9a100448.11dc5"]]},{"id":"9a100448.11dc5","type":"debug","z":"5b6e97cb.a67bf8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":400,"y":400,"wires":[]},{"id":"b7af98bb.68b3d","type":"serial out","z":"5b6e97cb.a67bf8","name":"test","serial":"d606e68c.2e18b","x":610,"y":400,"wires":[]},{"id":"df93de13.b2485","type":"inject","z":"5b6e97cb.a67bf8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":540,"wires":[["74b1d48d.bc7c04"]]},{"id":"74b1d48d.bc7c04","type":"function","z":"5b6e97cb.a67bf8","name":"","func":"msg.payload = new Buffer\n([0x02+A+0x03]);\nreturn msg;\n\n","outputs":1,"noerr":0,"x":370,"y":560,"wires":[["8d036d47.a8cc2"]]},{"id":"8d036d47.a8cc2","type":"debug","z":"5b6e97cb.a67bf8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":620,"y":620,"wires":[]},{"id":"d606e68c.2e18b","type":"serial-port","z":"","serialport":"/dev/ttyAMA0","serialbaud":"2400","databits":"8","parity":"none","stopbits":"1","waitfor":"","newline":"","bin":"bin","out":"char","addchar":"false","responsetimeout":""}]

```

these are the commands:

```auto
5: Command mode, output on request

Request gross weight Command: HEX (02) + “A“ + “HEX (03), Answer: GW: 0023.45 (kg)

Request net weight Command HEX (02) + “B“ + “HEX (03): Answer: NW: 0013.45 (kg)

Request tare weight Command HEX (02) + “C“ + “HEX (03): Answer: TW: 0010.00 (kg)

Trigger zero position Command HEX (02) + “D“ + “HEX (03): Answer: D
Trigger tare function Command HEX (02) + “E“ + “HEX (03): Answer: E

```

Pleas Pleas.  
THX  
Mike

---

<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:** [12 June 2020 20:49 UTC](https://discourse.nodered.org/t/help-with-rs232/28254/2 "2020-06-12T20:49:50Z")

</div>

Hi that protocol has to be the simplest I've ever seen.

Try this to get you started...

Inject \> function \> serial request \> debug

In the function node do this...

```auto
msg.payload = String.fromCharCode(2) + 'A' + String.fromCharCode(3);
return msg;

```

See what you get back.

---

<div class="post-metadata">

**Author:** ![mike1976](https://avatars.discourse-cdn.com/v4/letter/m/a698b9/32.png) [@mike1976](https://discourse.nodered.org/u/mike1976)\
**Post date:** [13 June 2020 14:38 UTC](https://discourse.nodered.org/t/help-with-rs232/28254/3 "2020-06-13T14:38:10Z")

</div>

Thank you,

come back now:  
 ![string wert](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/5/35b60c2971824123ab06568d1a7b225b65a56160.png)

And how can I output the value as real from the string,  
for example -0065.0 or 00120.0.

Thank you so much

---

<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:** [13 June 2020 15:20 UTC](https://discourse.nodered.org/t/help-with-rs232/28254/4 "2020-06-13T15:20:19Z")

</div>

You can use the javascript split() method to split it into two parts at the ':' giving an array of two elements then use the function parseFloat to convert the second part to a number. So in a function node

```auto
msg.payload = parseFloat(msg.payload.split(":")[1])
return msg

```

---

<div class="post-metadata">

**Author:** ![mike1976](https://avatars.discourse-cdn.com/v4/letter/m/a698b9/32.png) [@mike1976](https://discourse.nodered.org/u/mike1976)\
**Post date:** [15 June 2020 11:19 UTC](https://discourse.nodered.org/t/help-with-rs232/28254/5 "2020-06-15T11:19:07Z")

</div>

Hi,

thank you again.  
That is exactly what I was looking for.

Would you also have an idea how I can read this sensor:

```auto
Example:PC sends command, query the current working mode:
AA B4 02 00 00 00 00 00 00 00 00 00 00 00 00 FF FF 00 AB
Sensor with ID A160 response:
AA C5 02 00 00 00 A1 60 03 AB
Show the sensorisin report active mode.
AA C5 02 00 01 00 A1 60 04 AB     
Show the sensorisin report query mode.
PC sends command, setthe sensor with ID A160 toreport query mode:
AA B4 02 01 01 00 00 00 00 00 00 00 00 00 00 A1 60 05 AB
Sensor with ID A160 response:
AA C5 02 01 01 00 A1 60 05 AB   
Show the sensorisset toreport query mode

```

this is an SD011 which I only want to query if necessary.  
and then wants to put it back to sleep mode.

> **[sds011\_protocol.pdf](https://learn.watterott.com/de/sensors/sds011/sds011_protocol.pdf)**
>
> 627.55 KB

sorry, rs232 is completely new territory for me. Thank you so much

---

<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:** [15 June 2020 12:02 UTC](https://discourse.nodered.org/t/help-with-rs232/28254/6 "2020-06-15T12:02:39Z")

</div>

Same as before however the protocol is more complex than STX A ETX

you will have to format an array of bytes (a buffer) to the format specified by the documentation

Best way to approach this is forget about the serial side and develop a flow that outputs a buffer containing `AA B4 02 00 00 00 00 00 00 00 00 00 00 00 00 FF FF 00 AB` (or in human terms `170, 180, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 0, 171`)

once you can reproduce that, then you can send it to the serial node.

Once you reliably get a response, i can show you how to easily extract the information you need.

---

<div class="post-metadata">

**Author:** ![mike1976](https://avatars.discourse-cdn.com/v4/letter/m/a698b9/32.png) [@mike1976](https://discourse.nodered.org/u/mike1976)\
**Post date:** [8 July 2020 21:23 UTC](https://discourse.nodered.org/t/help-with-rs232/28254/7 "2020-07-08T21:23:54Z")

</div>

Hello.

I would like to get the value of pm 10.0 from this string:

```auto
SPS30 sensor probing successful
FW: 2.2 HW: 7, SHDLC: 2.0
SPS30 Serial: 0805DD8776DA6E38
measurements started
error reading measurement
measured values:
	1.43 pm1.0
	3.41 pm2.5
	4.95 pm4.0
	5.25 pm10.0
	4.97 nc0.5
	9.06 nc1.0
	11.28 nc2.5
	11.72 nc4.5
	11.79 nc10.0
	0.68 typical particle size

measured values:
	1.43 pm1.0
	6.88 pm2.5
	11.22 pm4.0
	12.09 pm10.0
	0.00 nc0.5
	4.85 nc1.0
	11.06 nc2.5
	12.31 nc4.5
	12.48 nc10.0
	1.63 typical particle size

measured values:
	2.32 pm1.0
	2.45 pm2.5
	2.45 pm4.0
	2.45 pm1...

```

.

I am currently using this.

```auto
msg.payload = parseFloat(msg.payload.split("measured values:")[1])
return msg;

```

but that only gives me the value of the first line after "measured values:"  
how to get to pm10.0

thx

---

<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:** [8 July 2020 21:30 UTC](https://discourse.nodered.org/t/help-with-rs232/28254/8 "2020-07-08T21:30:31Z")

</div>

You're gonna have to do multiple splits and loop through the resulting arrays to get values separated.

Start by splitting on newline \n then loop through splitting on space character then check the 2nd element until you find pm10.0 - your value will be in the 1st element.

Alternatively, use a regular expression to extract it.

---

<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:** [9 July 2020 07:12 UTC](https://discourse.nodered.org/t/help-with-rs232/28254/9 "2020-07-09T07:12:46Z")

</div>

Where are you getting that from? If you have control over the format then change it to a json string, it will make life much easier. If you don't have control over it can you get it as a sequence of single line messages instead? If so then use a Change nodes to select just the pm10.0 lines and then it will be very easy.

---

<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:** [9 July 2020 08:38 UTC](https://discourse.nodered.org/t/help-with-rs232/28254/10 "2020-07-09T08:38:18Z")

</div>

@Colin I had assumed since the thread was a continuation by the OP that it was related to the sds011 protocol from an earlier post.

@mike1976 is this a different serial device? Do you have control over what it outputs? If so, Colin's suggestion is the best way to handle this.

---

<div class="post-metadata">

**Author:** ![mike1976](https://avatars.discourse-cdn.com/v4/letter/m/a698b9/32.png) [@mike1976](https://discourse.nodered.org/u/mike1976)\
**Post date:** [9 July 2020 17:40 UTC](https://discourse.nodered.org/t/help-with-rs232/28254/11 "2020-07-09T17:40:43Z")

</div>

sorry.

posted it in the wrong forum.  
It has nothing to do with rs232.

It is a SPS30 sensor from Sensirion.  
[https://www.sensirion.com/de/umweltsensoren/feinstaubsensoren-pm25/](https://www.sensirion.com/de/umweltsensoren/feinstaubsensoren-pm25/)

I read this from UART:

> **[Sensirion/embedded-uart-sps](https://github.com/Sensirion/embedded-uart-sps)**
>
> Embedded UART Driver for Sensirion Particulate Matter Sensors - Download the Zip Package from the Release Page - Sensirion/embedded-uart-sps

So, since I'm actually very awkward with handling scripts.  
I read the whole thing in node-red per exec:  
/home/pi/release/sps30-uart-3.1.0/./sps30\_example\_usage

and if the sensor goes into sleep mode after 1 minute, stop  
I read this again with:  
killall sps30\_example\_u  
and then I get the values in msg.paylod

yes, i can already imagine that this is certainly not the most beautiful variant, but i was glad when i managed to get at least the first values.

```auto
[{"id":"c21ca0ed.e51008","type":"inject","z":"2ce51737.375af","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":220,"y":120,"wires":[["d8d58063.f0d748","42071adb.36c6e4"]]},{"id":"d8d58063.f0d748","type":"exec","z":"2ce51737.375af","command":"/home/pi/release/sps30-uart-3.1.0/./sps30_example_usage","addpay":false,"append":"","useSpawn":"false","timer":"75","oldrc":false,"name":"","x":610,"y":140,"wires":[["ac8fd890.d4a3f8","1c76854.df4bdfb","36d9d39c.fe8c74"],[],[]]},{"id":"ac8fd890.d4a3f8","type":"debug","z":"2ce51737.375af","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":990,"y":80,"wires":[]},{"id":"65fab7e.29cfdc8","type":"debug","z":"2ce51737.375af","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":630,"y":400,"wires":[]},{"id":"1c76854.df4bdfb","type":"function","z":"2ce51737.375af","name":"","func":"msg.payload = parseFloat(msg.payload.split(\"measured values:\")[3])\nreturn msg;\n","outputs":1,"noerr":0,"x":270,"y":400,"wires":[["65fab7e.29cfdc8"]]},{"id":"36d9d39c.fe8c74","type":"function","z":"2ce51737.375af","name":"","func":"msg.payload = parseFloat(msg.payload.split(\"measured values:pm\")[1])\nreturn msg;\n","outputs":1,"noerr":0,"x":300,"y":460,"wires":[["9b372457.00c728"]]},{"id":"9b372457.00c728","type":"debug","z":"2ce51737.375af","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":550,"y":460,"wires":[]},{"id":"5ef1f0da.870b38","type":"inject","z":"2ce51737.375af","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":620,"wires":[[]]},{"id":"de31ff05.172898","type":"exec","z":"2ce51737.375af","command":"killall sps30_example_u","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":670,"y":640,"wires":[["ccf89be6.16045"],[],[]]},{"id":"ccf89be6.16045","type":"debug","z":"2ce51737.375af","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1000,"y":620,"wires":[]},{"id":"42071adb.36c6e4","type":"delay","z":"2ce51737.375af","name":"","pauseType":"delay","timeout":"76","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":450,"y":560,"wires":[["de31ff05.172898"]]}]

```

---

<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:** [7 September 2020 17:46 UTC](https://discourse.nodered.org/t/help-with-rs232/28254/12 "2020-09-07T17:46:28Z")

</div>

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