OT: Some advice on perl: read byte to hex string
I've managed to create an application for reading some bytes of data
from an id card reader connected into a serial port.
But it's under Win$ ...
So, now I've been trying for at least capturing the data then convert
it the way I want.
The convertion is called "byte to hex string" convertion.
Below is the perl script I have so far but the result is not what I
expect it.
The same algorithm works perfectly in java and vc++
Thanks.
::mslinuz::
#Serial Reader
#!/usr/bin/perl
use Device::SerialPort 0.12;
#Create the device object
$PORT = "/dev/ttyS0";
$serialPort = Device::SerialPort->new($PORT)
|| die "Can't create serial device for communicating...\n";
#Setup the parameters
$serialPort->baudrate(9600)
|| die "Can't set serial port baudrate ...\n";
$serialPort->parity("none")
|| die "Can't set serial port parity ...\n";
$serialPort->databits(8)
|| die "Can't set serial port databits ...\n";
$serialPort->stopbits(1)
|| die "Can't set serial port stopbits ...\n";
$serialPort->write_settings
|| die "Can't write setting ...\n";
$serialPort->lookclear; #clear the buffer
my $gotit = "";
until ("" ne $gotit) {
$gotit = $serialPort->lookfor; #pool until data ready
die "Aborted\n" unless (defined $gotit);
sleep 1;
}
#Loop through the whole data
for ($i=0; $i < (length($gotit)); $i++) {
my $c = substr($gotit, $i, 1);
$c1 = $c & 0xF0; #get the high nibble
$c1 = $c1 >> 4; #
$c1 = $c1 & 0x0F; #
$str1 = hex($c1); #convert to hex
$c2 = $c & 0x0F; #get the low nibble
$str2 = hec($c2); #convert to hex
print $str1 . $str2; #display the string
}
undef $serialPort;
Reply to: