OT: Some advice on perl: read byte to hex string
- From: Welly Hartanto <ms.linuz@xxxxxxxxx>
- Date: Wed, 20 Sep 2006 13:29:44 +0700
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
- Follow-Ups:
- RE: Some advice on perl: read byte to hex string
- From: David Christensen
- Re: OT: Some advice on perl: read byte to hex string
- From: Mumia W.
- Re: OT: Some advice on perl: read byte to hex string
- From: Mumia W..
- Re: OT: Some advice on perl: read byte to hex string
- From: Dave Sherohman
- Re: OT: Some advice on perl: read byte to hex string
- From: Dave Carrigan
- Re: OT: Some advice on perl: read byte to hex string
- From: Michael Marsh
- RE: Some advice on perl: read byte to hex string
- Prev by Date: CD drive on a parallel port
- Next by Date: Degraded i810/i915 Rendering Performance After Xorg 7 Upgrade
- Previous by thread: CD drive on a parallel port
- Next by thread: Re: OT: Some advice on perl: read byte to hex string
- Index(es):
Relevant Pages
|