Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

serial port hex only

by ccherri (Acolyte)
on Apr 26, 2012 at 20:41 UTC ( #967460=perlquestion: print w/replies, xml ) Need Help??
ccherri has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to interface to a device that accepts serial data signals, but the OS in the device only responds to very limited inputs and all the prefix and suffix stuff that goes with a typical serial port configuration is screwing it up.

The device accepts only data in a hex format....

for instance, I can send (using RealTerm 'Send Numbers' button): 0xc9

the device will respond with hex 'F401' which results in it not being processed at all by the receiving application (perl, hyperterm, etc). RealTerm will show the F401 response if I set the display settings to hex, but Perl does not register any response at all. If I send 0xC9 as ascii, the device cannot correct interpret all the ascii values of 0xC9 and all the prefix and suffix that goes with it.

So, I have two problems

1. How to limit the perl output to the port to be strictly the hex values I specify (not the ascii representation of the hex values)?

2. How to get perl to process back into my script the hex response?

any help at all is much appreciated. I am new this year to perl and so far am having a great time with it.

Cheers UPDATE: I think I am on to something...instead of


I tried


and it sends only and exactly c9

now to find 'read_char'...but if there are other tips, happy to hear them, and thanks for the quick suggestions!

Replies are listed 'Best First'.
Re: serial port hex only
by Marshall (Abbot) on Apr 26, 2012 at 21:59 UTC
    I keep putting off a project to use this thing. But I did look at Win32::SerialPort docs.

    I have written binary data to disk files before. One way, open file, set binmode, use syswrite() to write a binary buffer. Looks like something similar can be done here. You don't say how you are doing the write, but if you are using tied file handles, I would : binmode FH; and then use syswrite(). There are some examples in the docs. The buffer that syswrite uses are bytes, so you need to think about pack and unpack if you are say trying to send something that is not already a binary byte.

    Update: This makes a binary file on my machine. I always have to stare at pack a long time to get it right - so I did something simple here - however it did work. I'd suggest using a disk file like this sort of thing to get the "right incantation" to generate the binary write sequence that you need based on the values that you want to send for testing, then see if you can do the same thing on the serial port.

    #!/usr/bin/perl -w use strict; open FH, ">", "testingbin" or die "unable to open testingbin $!"; binmode FH; foreach (1,2,3,4,5,6,7,8,9) { my $buf = chr($_); syswrite FH,$buf; } close FH; __END__ my text editor says: 0102030405060708 09 as binary bytes
    My Update to your Update:

    Yes, this transmit one char at a time thing looks fine and is similar to the above test code. The pack and unpack functions can generate/decode a whole buffer (maybe many KB) of stuff that can be sent/received at one time. This makes a difference when mucking around with big binary files like maybe .WAV files, but sounds like your serial port application will do just fine reading or writing a character at a time - computers are so fast nowadays that it will keep up with the serial port at even a high baud rate.

    PS: Welcome to Perl! This stuff is just amazing!

Re: serial port hex only
by ww (Archbishop) on Apr 26, 2012 at 21:01 UTC

    1. Insufficient information
    2. why not have your script expect an ASCII representation and handle it in a way that allows the next function/process/line-of-code to treat it as hex... for which, prepending '0x' may suffice.

      processing it once I get it back in the perl script is not the problem. What I need is bit level control of the serial port within my script. Perhaps win32::serialport is not the way to go...
        Have you looked at Win32API::CommPort? It provides the low level/raw access that Win32::SerialPort is built-upon. It's included with Wn32::SerialPort, so you should already have it.

        Elda Taluta; Sarks Sark; Ark Arks
        My deviantART gallery

Re: serial port hex only
by zentara (Archbishop) on Apr 27, 2012 at 10:20 UTC
      Final UPDATE...the link zentara posted pointed me to


      which brings the data in and 'unpack H4, $data' works to get it in hex format. problem is solved! (though I am not sure where I am picking up 'input', but as long as it works... Thanks everyone!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://967460]
Approved by ww
and a moth chases the moon...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2018-05-21 15:38 GMT
Find Nodes?
    Voting Booth?