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;


--
To UNSUBSCRIBE, email to debian-user-REQUEST@xxxxxxxxxxxxxxxx
with a subject of "unsubscribe". Trouble? Contact listmaster@xxxxxxxxxxxxxxxx



Relevant Pages

  • Re: Reading from a serial port
    ... the serial port with Perl. ... # (sleep defined in Device::Serialport) ... $ser->datatype|| die 'fail setting datatype raw'; ...
    (comp.lang.perl.misc)
  • Re: Serial Ports
    ... I have a robot that sends data over a serial connection and I'd like to ... use Perl/Linux to listen to the Serial Port and record the data. ... die "$@\n" if; ... if# not supported in Linux ...
    (perl.beginners)
  • Re: Re: Modem Net Access.
    ... Sorry for the delay, but I've got to shutdown, remove the drive, put another in, reboot, install gpmand then hit the logs to find any discrepancies anywhere. ... The kernal picks up the one and only serial port on boot -I'm running a 6400+ on an Asus M3A, so it's only got the one serial port and just one IDE channel. ... UP LOOPBACK RUNNING MTU:16436 Metric:1 ... To UNSUBSCRIBE, email to debian-user-REQUEST@xxxxxxxxxxxxxxxx with a subject of "unsubscribe". ...
    (Debian-User)
  • Re: Forward System Messages to SSH
    ... Now and then, messages are output to the tty display, such as USB ... USB virtual serial port) as the console, and then connect to that using ... mailmin.sourceforge.net - remote access via secure email ... To UNSUBSCRIBE, email to debian-user-REQUEST@xxxxxxxxxxxxxxxx ...
    (Debian-User)
  • Re: Using colours in mutt (was Re: Disk Drive Order Changes - Again)
    ... the Athlon doesn't have a spare serial port or a paralell port. ... so it acts as a print server when I need to ... and a terminal server when I need to access stuff from upstairs. ... To UNSUBSCRIBE, email to debian-user-REQUEST@xxxxxxxxxxxxxxxx ...
    (Debian-User)