Sending a data string from Perl script that requires Start (0x02) and End (0x03) of Text data string layout is
Results of code below are not STX = 0x02 and ETX = 0x03 but $hex02_ascii = E2 98 BB $hex02_ascii = E2 99 A5 example: ☻0000052500000000<payload>♥ Note "byte count" is correct as is the "reserved"#### STX 1 byte (0x02) #### byte count 8 bytes, leading zeros, inclusive + of wrapper (payload length + 18) #### reserved 8 bytes, all zeros (required and +reserved for future use) #### payload (Data record to send) #### ETX 1 byte (0x03)
my $hex02 = 0x2; my $hex03 = 0x3; my $hex02_ascii = sprintf "%c", $hex02; my $hex03_ascii = sprintf "%c", $hex03; my $MsgLgth = 0; my $Message = " "; chomp $Message; $Message = $_; # Calculate Length of Message $MsgLgth = length($_)+18; if ( $MsgLgth < 100 ) { $Message = $hex02_ascii . '000000' . $MsgLgth . '00000000' . $Message +. $hex03_ascii; }elsif ( $MsgLgth < 1000 ) { $Message = $hex02_ascii . '00000' . $MsgLgth . '00000000' . $Message +. $hex03_ascii; }elsif ( $MsgLgth < 10000 ) { $Message = $hex02_ascii . '0000' . $MsgLgth . '00000000' . $Message +. $hex03_ascii; }elsif ( $MsgLgth < 100000 ) { $Message = $hex02_ascii . '000' . $MsgLgth . '00000000' . $Message +. $hex03_ascii; }elsif ( $MsgLgth < 1000000 ) { $Message = $hex02_ascii . '00' . $MsgLgth . '00000000' . $Message +. $hex03_ascii; }else{ # Max length is 300000 # If hit here, too long ... by a LOT print "Formated Message exceeds MaxMessageLength\n\n";
In reply to Help adding STX and ETX to data string in Perl by CuriousMark
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |