# Message Datagram with fixed length, padding texts

**URL:** https://discourse.nodered.org/t/message-datagram-with-fixed-length-padding-texts/40326
**Category:** Developing Nodes
**Created:** [3 February 2021 17:29 UTC](https://discourse.nodered.org/t/message-datagram-with-fixed-length-padding-texts/40326 "2021-02-03T17:29:52Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![wvsbsp](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wvsbsp/32/36440_2.png) [@wvsbsp](https://discourse.nodered.org/u/wvsbsp)
#### Post date: [3 February 2021 17:29 UTC](https://discourse.nodered.org/t/message-datagram-with-fixed-length-padding-texts/40326/1 "2021-02-03T17:29:52Z")

</div>

I try to create a datagram that contains variable texts. The datagram has fixed lenght for the texts, so i need ti add trailing blanks to the datagram.  
unfortunately, JavaScript automaticalls removes trailing blanks and i get format errors from the receiver.

```auto
var ACK = msg.payload.ACK;
var Order_NR = msg.payload.ON;
var Empty16 = " "; // JS removes the content...
var Sta_Source = "Lernfabrik";
var Sta_Dest1 = "Montage01";
var Sequence_ID_Out = context.flow.get('ID_OUT'); // get last sequence number

Sta_Source += Empty16; // add trailing blanks
Sta_Source = Sta_Source.slice(0,16); // cut to 16 characters

Sta_Dest1 += Empty16; Sta_Dest1 = Sta_Dest1.slice(0,16);

msg.payload="STA01STA02"+Sequence_ID_Out.toString().padStart(4,"0");
if(ACK) msg.payload+="A0NEORNE"; // send with acknowledge
else msg.payload+=" 0NEORNE"; // send no acknowledge
msg.payload=msg.payload+Order_NR.toString().padStart(5,"0")+
     Sta_Source+ // source target should be 16 chars
     Sta_Dest1+ // destination target should be 16 chars
     "00000"+ // Priority 5 chars
     "00001"; // AGV Type 5 chars

Sequence_ID_Out ++;
if(Sequence_ID_Out>9999) Sequence_ID_Out=0; // sequence number rolls ober after 9999
context.flow.set('ID_OUT',Sequence_ID_Out); // store new sequence number

return [msg];

```

Instead of (This editor also eats blanks, even in preformatted text, so \_ means "Blank")  
`"STA01STA020123 0NEORNE00001Lernfabrik _ _ _ _ _ Montage01 _ _ _ _ _ _ 0000000001"`  
i only get  
`"STA01STA020123 0NEORNE00001Lernfabrik Montage01 0000000001"`  
any help on this? I can't change the protocol since it is given by the manufacturer of the AGV.

Regards  
Bernhard

---

<div class="post-metadata">

### Author: ![wvsbsp](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wvsbsp/32/36440_2.png) [@wvsbsp](https://discourse.nodered.org/u/wvsbsp)
#### Post date: [3 February 2021 19:57 UTC](https://discourse.nodered.org/t/message-datagram-with-fixed-length-padding-texts/40326/2 "2021-02-03T19:57:09Z")

</div>

DUH! After almost one hour of trial and error, i found, that the original code is working as intended.  
The message ist 69bytes long, which is correct. It just looks too short in the debug output.

![Stringlänge](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/9/a9264f929a92a28e57d56cde1172ef18d35bbe78.png)

When i copy the string to a textfile, all trailing blanks are present.

---

<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: [17 February 2021 19:57 UTC](https://discourse.nodered.org/t/message-datagram-with-fixed-length-padding-texts/40326/3 "2021-02-17T19:57:19Z")

</div>

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