NewtownGuy
Technical User
Hello,
I had this script working when it was printing one character at a time to the output, but now I need to form the entire string before I send it to the output. I must be doing something wrong forming the string. I should get 7 bytes, based on the string I send it from IE, but I only get one byte and its value is always numeric 0.
What am I doing wrong ? I've highlighted the relevant string lines in blue:
Thank you.
-- NewtownGuy
I had this script working when it was printing one character at a time to the output, but now I need to form the entire string before I send it to the output. I must be doing something wrong forming the string. I should get 7 bytes, based on the string I send it from IE, but I only get one byte and its value is always numeric 0.
What am I doing wrong ? I've highlighted the relevant string lines in blue:
Code:
#!/usr/bin/perl
# output a sequence of
# (a) sync -- an initial 8-bit whose value is always 0xFF,
# (b) n 8-bit values, each of which is sent to this script as two hex
# characters by caller, and
# (c) one final 8-bit value, the checksum, which is the modulo 256 sum
# of the n 8-bit values sent by caller, to a COM port
use CGI qw/:standard/;
use strict;
my $cgi = new CGI;
my $value = $cgi->param('value');
# Output of CGI Script
# - dummy no-value returned to web page to avoid error messages
print header(-status=>'204 No Content');
close (STDOUT); # Ensure nothing else gets sent.
# open(TEST, ">/var/[URL unfurl="true"]www/log/input_string.txt")[/URL] or die "Can't open input_string.txt : $!\n";
# print TEST $value;
# close(TEST);
# Open output file and redirect to COM port
# open(OUT, ">/dev/ttyS0") or die "Can't open : ttyS0 $!\n";
open(OUT, ">/var/[URL unfurl="true"]www/log/log.bin")[/URL] or "Can't open log.bin : $!\n";
binmode(OUT);
flock (OUT, 2); # Lock the file to protect it from writing by others
# Output one fixed 8-bit value whose value is 0xFF
# print OUT chr(hex('FF'));
[COLOR=blue] my $output = chr(hex('FF')); # form the entire string, then output all at once, starting with 'FF' byte[/color]
# each 8-bit value to be output is sent by caller as two hex characters,
# so the number of bytes to be output (not counting the leading sync and
# trailing checksum bytes) is half the number of characters sent by
# caller
my $bytes = length($value) / 2; # each 8-bit value to be output is sent by caller as two hex characters, so the number of bytes to be output (not counting the leading sync and trailing checksum bytes) is half the number of characters sent by caller
# Initialize checksum, which will be computed as the sum of
# the $bytes (number of) 8-bit values output
my $checksum = 0; # it's a numeric value, not a character at this stage
for (my $i = 0; ($i < $bytes); $i++) {
my $hex1 = substr($value, (2 * $i), 1); # get m'th character, m = 2 * i
my $hex2 = substr($value, (2 * $i) + 1, 1); # get (m+1)'th character
my $hex1_hex2 = hex("$hex1$hex2"); # combine two hex characters into one byte from left to right
# print OUT chr($hex1_hex2); # output one byte formed from from two hex characters
[COLOR=blue] $output = $output . chr($hex1_hex2); # concatenate one byte formed from two hex characters to the string[/color]
$checksum = $checksum + $hex1_hex2; # add 8-bit value from new character to $checksum
}
# Get the mod 256 remainder of $checksum, i.e., the 8 lsb's, and output as the final byte
$checksum = $checksum % 256; # kill off any high order bits
# print OUT chr($checksum);
[COLOR=blue]$output = $output . chr($checksum); # concatenate $checksum byte to string
print OUT chr($output); # output whole string at once[/color]
# Unlock and close the file
flock(OUT, 8);
close(OUT);
Thank you.
-- NewtownGuy