sawtooth has asked for the wisdom of the Perl Monks concerning the following question:

Greetings patient Monks.
I am parsing and converting a real-time binary data stream that is coming in on the serial port (COM port). This is working fine, but every 4th byte is a 'sample delimiter' that I need to check and confirm. Currently I am using a reg exp in the ARE_MATCH function of the serialport module to grab the first 3 bytes:
$ob->are_match("-re", '\x{0a}');
The problem with this is that if one of the first 3 bytes is a 0x0a, the match will return too few bytes and mess the whole thing up. Also, if the fourth byte is anything other than 0x0a, we have a problem that we need to know about. So what I need to do is count the number of bytes as they stream in, and check every fourth byte to confirm that it is indeed a 0x0a. This would be easily done if I were reading a file using the READ function. But since I am reading a real-time datastream from the COM port instead, I am puzzled about how to do this. I have tried assigning the COM port a filehandle (with OPEN) and READing that, but of course the COM port is locked by then by the serialport module.
My apologies for the rambling explanation, but I hope that the explanation was sufficient enough to convey the issue.
Many Humble Thanks for reading this query

Replies are listed 'Best First'.
Re: Counting bytes in an input stream instead of a file
by dsheroh (Monsignor) on Jun 06, 2006 at 20:23 UTC
    Take another look at the Win32::SerialPort docs... Way down in the "NOTES" section, there's an example of using tie to associate a filehandle with the serial port. It looks like that should then allow you to use read.
Re: Counting bytes in an input stream instead of a file
by sgifford (Prior) on Jun 06, 2006 at 21:04 UTC
    What about just using the read method of your Win32::SerialPort object? Keep in mind it can return less than the number of characters you ask for (just like a standard read), so you'll probably want to call it in a loop.
Re: Counting bytes in an input stream instead of a file
by GrandFather (Saint) on Jun 06, 2006 at 20:42 UTC

    What is the serial module that you are using and on what OS? If $ob is the serial object, can you not simply pull the first n characters out of it? At some point surely you end with a string or array of characters that you can process piecemeal?

    Show us a little more code.


    DWIM is Perl's answer to Gödel