Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Sending and reading hex commands on a Win32 serial port

by c4onastick (Friar)
on Jan 25, 2008 at 23:02 UTC ( #664389=perlquestion: print w/ replies, xml ) Need Help??
c4onastick has asked for the wisdom of the Perl Monks concerning the following question:

Greeting fellow monks!

I have been trying to write a bit of perl to talk to a piece of equipment at work. I'm fortunate enough that the manufacturer has thouroughly documented the protocol to issue commands and talk to the instrument. I believe I've narrowed the problem to the mysterious pack function.

I'd like to send the hex:

CA 00 01 00 00 FE

Over the serial port (pretty sure I've got the serial port working, thanks to Win32::SerialPort). I'm actually using a little USB to serial converter that comes with a nice little monitor utility that can show the hex dump of what goes over the port. Here's the code I've come up with so far:

#! C:\perl\bin\perl.exe use strict; use warnings; use Win32::SerialPort; my $port_name = 'COM4'; my $config_file = 'thermo.cfg'; my $port = new Win32::SerialPort($port_name, $config_file) || die "Una +ble to open: $^E\n"; my $status = pack('HHHHHH', 'ca', '00', '01', '00', '00', 'fe'); $port->write($status."\n"); sleep 1; my $result = $port->input; print "result = $result\n"; $port->close || warn "Close Failed!\n"; undef $port;

The script executes fine with no results to speak of, but the port monitor sees:

00009 8 (351.485.411): TX: C0 00 00 00 F0 00

I've been through the limited amount of examples and tutorials that I could find on Win32::SerialPort but to no avail. I loved process control in college and would love to learn how to control process equipment with perl! (Who needs LabVIEW? is where I'd like to be able to go with it.)

Any insights would be greatly appreciated. Thanks!

Comment on Sending and reading hex commands on a Win32 serial port
Select or Download Code
Re: Sending and reading hex commands on a Win32 serial port
by hipowls (Curate) on Jan 25, 2008 at 23:39 UTC

    The pack needs to be

    my $status = pack( 'H[12]', 'ca00010000fe' );
    I figured it out by printing $status to a file and then using a hex editor (and reading pack ;-).

Re: Sending and reading hex commands on a Win32 serial port
by starbolin (Hermit) on Jan 26, 2008 at 05:49 UTC

    hipowls beat me to it.

    #!/usr/bin/perl -w open FH, '>foobar'; print FH pack 'H2H2H2H2', 'ab','cd','ef','00'; print FH pack 'H8','abcdef00'; print FH pack 'H2'x 4, 'ab', 'cd', 'ef', '00'; print `hd "foobar"`,"\n";
    Yeilds:
    00000000 ab cd ef 00 ab cd ef 00 ab cd ef 00 + 0000000C


    s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}
Re: Sending and reading hex commands on a Win32 serial port
by ikegami (Pope) on Jan 27, 2008 at 10:20 UTC
    An alternative:
    pack('C*', 0xca, 0x00, 0x01, 0x00, 0x00, 0xfe);
Re: Sending and reading hex commands on a Win32 serial port
by c4onastick (Friar) on Jan 28, 2008 at 18:09 UTC

    Thanks, everyone. You solutions worked perfectly!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://664389]
Approved by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (16)
As of 2014-10-23 14:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (125 votes), past polls