I'm wanting a serial port monitor that displays the received data as hexadecimal and when it receives "01" it moves to a new line and starts the line with the time or elapsed time (format of time is not critical, I just want at least 1/10 second resolution for the time between messages). Seemed like a fairly simple thing to get started with but I'm not getting very far. Before I added the ord() it would just hang with no errors or output. After adding the ord() it outputs a continuous string of repeating "30". This is on XP with ActiveState perl.

#!/usr/bin/perl use strict; use warnings; use Win32::SerialPort; use Time::HiRes; my $PORT = "COM1"; # port to watch my $now; my $hex; my $inbyte; print "prog start \n"; my $starttime; $starttime = Time::HiRes::time(); $now = Time::HiRes::time() - $starttime; print "Before serial port iniialization time is: $now \n"; my $port = Win32::SerialPort->new($PORT) || die "Can't Open $PORT: $!" +; $port->databits(8); $port->baudrate(9600); $port->parity("none"); $port->stopbits(1); while(1) { $inbyte=ord($port->read(1)); $hex = unpack('H2', "$inbyte"); if ($inbyte == 1){ # assume this is start of new packet $now = Time::HiRes::time() - $starttime; print "\n $now $hex"; } else { print " ", $hex; } }

In reply to Getting started - serial, bytes and hex output by JimLS

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.