# R-Pi 4 serial port truncates messages

**URL:** <https://discourse.nodered.org/t/r-pi-4-serial-port-truncates-messages/49692>\
**Category:** General\
**Created:** [13 August 2021 16:18 UTC](https://discourse.nodered.org/t/r-pi-4-serial-port-truncates-messages/49692 "2021-08-13T16:18:17Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![dsplitz](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dsplitz/32/49940_2.png) [@dsplitz](https://discourse.nodered.org/u/dsplitz)\
**Post date:** [13 August 2021 16:18 UTC](https://discourse.nodered.org/t/r-pi-4-serial-port-truncates-messages/49692/1 "2021-08-13T16:18:17Z")

</div>

I can't be sure if this is a node-red or R-PI issue. It's a low runner so I can work around it but thought someone may want to investigate who knows how to get under the hood.

I've seen this issue on two separate R-PI 4 modules mounted on the R-PI IO boards. The flow below can be used to quickly find the issue. You need to add a wire to loop back the serial port on the IO board.  
In my application, I used a o-scope to verify the message size on the wire was correct, this seems to be a receive side issue.

What you'll see is an expected msg length of 32 bytes. The flow will stop when a msg of less than 16 bytes is received.  
'''  
[flows(1).json](https://discourse.nodered.org/uploads/short-url/qXwwGLW3ZaWYtooziCoJlpPFt2k.json) (2.1 KB)  
'''

---

<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 August 2021 17:40 UTC](https://discourse.nodered.org/t/r-pi-4-serial-port-truncates-messages/49692/2 "2021-08-13T17:40:24Z")

</div>

What happens if you increase the port timeout significantly?

For the future, please see this post for how to post flows - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

**[Edit]** Also does it truncate or miss characters?

---

<div class="post-metadata">

**Author:** ![dsplitz](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dsplitz/32/49940_2.png) [@dsplitz](https://discourse.nodered.org/u/dsplitz)\
**Post date:** [13 August 2021 17:51 UTC](https://discourse.nodered.org/t/r-pi-4-serial-port-truncates-messages/49692/3 "2021-08-13T17:51:48Z")

</div>

> [@Colin](#):
>
> truncate

The message is truncated. Oddly, in my application I expect 23 bytes and it seems to always truncate to 16 bytes.

I've tried changing baud rate, timeout, response timeout, modem control signals, message pacing and nothing has an effect. I've also verified the signal quality which is very good.

```auto
[{"id":"62a0e5a5.fc59bc","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"a5e97b88.29fc1","type":"inject","z":"62a0e5a5.fc59bc","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":270,"y":480,"wires":[["c96883ab.928c68"]]},{"id":"e5a01549.1808c","type":"function","z":"62a0e5a5.fc59bc","name":"","func":"\nif (msg.payload.length > 16)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":600,"wires":[["14a3a644.d5496a"]]},{"id":"360db49b.d92ca4","type":"debug","z":"62a0e5a5.fc59bc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":420,"wires":[]},{"id":"24f2195f.278fde","type":"debug","z":"62a0e5a5.fc59bc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":430,"y":660,"wires":[]},{"id":"c96883ab.928c68","type":"function","z":"62a0e5a5.fc59bc","name":"","func":"\nmsg.payload = [0.1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6]\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":480,"wires":[["360db49b.d92ca4","97e1f8f6.418b5"]]},{"id":"97e1f8f6.418b5","type":"serial out","z":"62a0e5a5.fc59bc","name":"","serial":"bc2ed729.523838","x":910,"y":480,"wires":[]},{"id":"c4505f2a.356db","type":"serial in","z":"62a0e5a5.fc59bc","name":"","serial":"bc2ed729.523838","x":270,"y":600,"wires":[["e5a01549.1808c","24f2195f.278fde"]]},{"id":"14a3a644.d5496a","type":"delay","z":"62a0e5a5.fc59bc","name":"","pauseType":"delay","timeout":"10","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":670,"y":540,"wires":[["97e1f8f6.418b5"]]},{"id":"bc2ed729.523838","type":"serial-port","serialport":"/dev/ttyAMA1","serialbaud":"115200","databits":"8","parity":"none","stopbits":"1","waitfor":"","dtr":"none","rts":"none","cts":"none","dsr":"none","newline":"5","bin":"bin","out":"interbyte","addchar":"","responsetimeout":"0"}]

```

Thanks,  
Dave

---

<div class="post-metadata">

**Author:** ![edje11](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edje11/32/572_2.png) [@edje11](https://discourse.nodered.org/u/edje11)\
**Post date:** [13 August 2021 19:04 UTC](https://discourse.nodered.org/t/r-pi-4-serial-port-truncates-messages/49692/4 "2021-08-13T19:04:39Z")

</div>

> [@dsplitz](#):
>
> I've seen this issue on two separate R-PI 4 modules mounted on the R-PI IO boards. The flow below can be used to quickly find the issue. You need to add a wire to loop back the serial port on the IO board

What's the IO board, do you have specs of it?  
Where do you put the loopback wire on? Uart2 (AMA1) is on pins 27 & 28 of the Pi header.  
Did you edit the /boot/config.txt to enable the uart2?

---

<div class="post-metadata">

**Author:** ![dsplitz](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dsplitz/32/49940_2.png) [@dsplitz](https://discourse.nodered.org/u/dsplitz)\
**Post date:** [13 August 2021 19:37 UTC](https://discourse.nodered.org/t/r-pi-4-serial-port-truncates-messages/49692/5 "2021-08-13T19:37:24Z")

</div>

Sure, here's the link for the IO board:

> **[Buy a Compute Module 4 IO Board – Raspberry Pi](https://www.raspberrypi.org/products/compute-module-4-io-board/)**
>
> Exposing every interface from Raspberry Pi Compute Module 4, the Compute Module 4 IO Board provides a development platform and reference base-board design for our most powerful Compute Module yet.

Yes, for the test flow I use a jumper between pins 27/28 of J8 and yes I did enable the uart in the config file.  
I running from the on-board memory not a USB stick.

Obviously in my application I'm not doing a loopback. I am polling a separate processor that's just running some embedded code and see the same issue.

---

<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 August 2021 20:37 UTC](https://discourse.nodered.org/t/r-pi-4-serial-port-truncates-messages/49692/6 "2021-08-13T20:37:57Z")

</div>

Clutching at straws maybe, but could you stop node-red and start it again in a terminal and post the log from startup here please. Including some comms action.

---

<div class="post-metadata">

**Author:** ![edje11](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edje11/32/572_2.png) [@edje11](https://discourse.nodered.org/u/edje11)\
**Post date:** [13 August 2021 20:46 UTC](https://discourse.nodered.org/t/r-pi-4-serial-port-truncates-messages/49692/7 "2021-08-13T20:46:16Z")

</div>

Tested it here on a "normal" Pi4 with a jumper on pin 27 & 28, added dtoverlay=uart2 in /boot/config.txt.  
And it works fine here with your flow (removed the delay node).  
Array with 26 elements is send and every time there 55 bytes received. This because you send an array and set the receiver as binary buffer output.

 ![Capture](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/3/b329ffd1215ef0980af0cfb138a3466e16b06fb0.png)

To send binary data try this in your function node:

```auto
msg.payload = Buffer.from("\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x20\x21\x22\x23\x24");
return msg;

```

30 bytes are send and 30 bytes are received.

Example:

```auto
[{"id":"44e345f9fecfeb6f","type":"inject","z":"581aefe8afaa4f7f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":480,"y":260,"wires":[["7a21b882602e5055"]]},{"id":"82f92f3c7094b58d","type":"function","z":"581aefe8afaa4f7f","name":"","func":"\nif (msg.payload.length > 16)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":650,"y":380,"wires":[[]]},{"id":"615714af560fb444","type":"debug","z":"581aefe8afaa4f7f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":840,"y":200,"wires":[]},{"id":"e74fb363437e23bd","type":"debug","z":"581aefe8afaa4f7f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":640,"y":440,"wires":[]},{"id":"7a21b882602e5055","type":"function","z":"581aefe8afaa4f7f","name":"","func":"msg.payload = Buffer.from(\"\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x09\\x0A\\x0B\\x0C\\x0D\\x0E\\x0F\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x20\\x21\\x22\\x23\\x24\");\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":650,"y":260,"wires":[["615714af560fb444","53e8bbe77866d8bd"]]},{"id":"53e8bbe77866d8bd","type":"serial out","z":"581aefe8afaa4f7f","name":"","serial":"bc2ed729.523838","x":1120,"y":260,"wires":[]},{"id":"1ddd9268c097ba56","type":"serial in","z":"581aefe8afaa4f7f","name":"","serial":"bc2ed729.523838","x":480,"y":380,"wires":[["82f92f3c7094b58d","e74fb363437e23bd"]]},{"id":"bc2ed729.523838","type":"serial-port","serialport":"/dev/ttyAMA1","serialbaud":"115200","databits":"8","parity":"none","stopbits":"1","waitfor":"","dtr":"none","rts":"none","cts":"none","dsr":"none","newline":"5","bin":"bin","out":"interbyte","addchar":"","responsetimeout":"0"}]

```

---

<div class="post-metadata">

**Author:** ![dsplitz](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dsplitz/32/49940_2.png) [@dsplitz](https://discourse.nodered.org/u/dsplitz)\
**Post date:** [13 August 2021 22:54 UTC](https://discourse.nodered.org/t/r-pi-4-serial-port-truncates-messages/49692/8 "2021-08-13T22:54:08Z")

</div>

I'm running a test over the weekend so yes I will do your test on Monday.  
The error is random and may take as much as 30 minutes or more to happen.

I'm sorry, what do you mean by " Including some comms action."

---

<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:** [14 August 2021 06:22 UTC](https://discourse.nodered.org/t/r-pi-4-serial-port-truncates-messages/49692/9 "2021-08-14T06:22:39Z")

</div>

> [@dsplitz](#):
>
> what do you mean by " Including some comms action."

I meant up to the point the error occurs. I got the impression that it was a solid failure, but apparently not. In that case are you certain that the full message is sent for the message that fails?  
Another question. Does the rest of the message appear as the next message, or possibly on the front of the next message?  
You said you had tried various timeout settings, have you tried a very long timeout? One second for example?

---

<div class="post-metadata">

**Author:** ![edje11](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edje11/32/572_2.png) [@edje11](https://discourse.nodered.org/u/edje11)\
**Post date:** [14 August 2021 10:49 UTC](https://discourse.nodered.org/t/r-pi-4-serial-port-truncates-messages/49692/10 "2021-08-14T10:49:51Z")

</div>

Keep in mind that pin 27&28 is also used by i2c.  
Because your Module 4 IO board has an real-time clock it's likely that the i2c bus is used and can interfere with the UART.

---

<div class="post-metadata">

**Author:** ![dsplitz](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dsplitz/32/49940_2.png) [@dsplitz](https://discourse.nodered.org/u/dsplitz)\
**Post date:** [16 August 2021 13:17 UTC](https://discourse.nodered.org/t/r-pi-4-serial-port-truncates-messages/49692/11 "2021-08-16T13:17:48Z")

</div>

Is this the image you wanted?  
Since I use a start byte, any left over bytes would be discarded. I'll see if i can modify the test to catch any left over bytes. I will also try a string instead of just bytes.  
I did try a very long timeout to see if the erro was due to the timeout expiring.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/3/93947adfaf0b7295ae0b2065c60ccc3e9a98e488.png)

---

<div class="post-metadata">

**Author:** ![dsplitz](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dsplitz/32/49940_2.png) [@dsplitz](https://discourse.nodered.org/u/dsplitz)\
**Post date:** [16 August 2021 13:36 UTC](https://discourse.nodered.org/t/r-pi-4-serial-port-truncates-messages/49692/12 "2021-08-16T13:36:38Z")

</div>

It looks to me like the RTC is on SPI-0 which is not on this pin set.

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

---

<div class="post-metadata">

**Author:** ![dsplitz](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dsplitz/32/49940_2.png) [@dsplitz](https://discourse.nodered.org/u/dsplitz)\
**Post date:** [16 August 2021 13:51 UTC](https://discourse.nodered.org/t/r-pi-4-serial-port-truncates-messages/49692/13 "2021-08-16T13:51:17Z")

</div>

This is interesting.  
If I set the serial port to deliver ASCII stings but keep the Split after timeout I see the same issue with some truncated strings.  
If I set the split to be on the \n char then it seem to work 100%.

---

<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:** [15 October 2021 13:51 UTC](https://discourse.nodered.org/t/r-pi-4-serial-port-truncates-messages/49692/14 "2021-10-15T13:51:42Z")

</div>

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