Message Datagram with fixed length, padding texts

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.

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

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

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

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