Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems reading from Serial device - hex

Status
Not open for further replies.

twintriode

Technical User
Jan 6, 2005
21
0
0
AU
Hi All -

I am having some troubles reading from a serial device in perl. I can send commands with the following code:

Code:
my $e1 = '\x'."$_[0]";
my $e2 = '\\'."$_[1]";
my $e3 = '\\'."$_[2]";
my $e4 = '\\'."$_[3]";
my $e5 = '\\'."$_[4]";
my $e6 = '\\'."$_[5]";
#### Send Command Bytes
system "printf '$e1$e2$e3$e4$e5$e6' > /dev/ttyS0";

I can get a return ack but it is always read as an 'x' The correct basic ack from this device should be 0x78. With printf I can specify that the the input will be in hex format. Is there a way to do that in the other direction when using some kind of read?

Here is the code I am using for the retun:

Code:
open(REC, "/dev/ttyS0");
#...
system "printf '$e1$e2$e3$e4$e5$e6' > /dev/ttyS0";
#### Sleep for a bit more than 65ms to make sure we give time for a return
usleep(80000);
ReadMode "ultra-raw";
$key = ReadKey -1, *REC;
ReadMode "normal";

print "key is $key\n";

key is always x when a valid command has been received and executed by the device.

So far I have just programmed using x as the ack, but I now need to be able to read the correct hex.

When I use cutecom I am receiving the correct hex back from the device.

Can anyone point me in the right direction? How can I tell perl to let me read raw hex from the device?

Thanks
 
0x78[sub]16[/sub]=120[sub]10[/sub]='x', that is the correct code

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
hmmm, interesting... Thanks.

When I try a command that would normally give:

78 34

in cutecom I am getting:

x4 which seems ok.

But - If I want to read a position from the device in cutecom I get a response like:

78 01 04 50

But whilst doing: cat /dev/ttyS0

I only get

x (thats an x with a box after it)

Is this normal? Or is there a way I read this return value without it being altered?

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top