Hello Monks, I'm having troubles in getting a binary stream out of a RS232 device in my code. I know that the messages are all 14 bytes long. THeir structure is clear and are sent each 250ms. I wrote below code but problem is that I get absolutely no output on the console. If I remove the infinite loop, I get the first message and can process it but as I'm looking for a data logger feature it's useless :-) My code is below. Anybody can tell me what I should change/do/read/etc. to make it happen? Thanks!
#!/usr/bin/perl -w # use strict; use warnings; use threads; use Thread::Queue; use Win32::SerialPort; use Data::Dump qw(dump); # GLOBALS my %CONFIG = ( ReaderCOMPort => "COM56", ReaderCOMPortOpen => 1, testTime => 5 # testing time in seconds ); <readmore> my $ob; # Initialize the serial port of the reader initRS232(); $ob->read_interval(250); $ob->read_const_time(10000); $ob->lookclear; while (1) { my ($count, $str) = $ob->read(14); DumpString($str); sleep 1; } undef $ob; exit; sub initRS232 { eval { $ob = Win32::SerialPort->new ($CONFIG{ReaderCOMPort}); # || r +eturn "Can't open $PortName: $^E\n" }; if($@) { # We know it died, but is it an object or a # printable message? if( ref $@ ) { # Assume it's an exception object. Not a # great way to do it, but it works. # # Now, what kind of exception is it? # if( $@->isa( 'IOException' ) ) { } elsif( $@->isa( 'OtherException' ) ) { } else { } } else { # Assume it's an error string } } $CONFIG{ReaderCOMPortOpen} = 1; $ob->user_msg(1); # misc. warnings $ob->error_msg(1); # hardware and data errors $ob->baudrate(2400); $ob->parity("none"); $ob->parity_enable(1); # for any parity except "none" $ob->databits(8); $ob->stopbits(1); #$ob->xon_limit(20); # bytes left in buffer #$ob->xoff_limit(100); # space left in buffer $ob->write_settings; $ob->save("tekpower.cfg"); #$ob->are_match("\xE8"); } # dump the contents of a string as decimal and hex bytes and character +s sub DumpString { my @a = unpack('C*',$_[0]); my $o = 0; while (@a) { my @b = splice @a,0,16; my @d = map sprintf("%03d",$_), @b; my @x = map sprintf("%02x",$_), @b; my $c = substr($_[0],$o,16); $c =~ s/[[:^print:]]/ /g; printf "%6d %s\n",$o,join(' ',@d); print " "x8,join(' ',@x),"\n"; print " "x9,join(' ',split(//,$c)),"\n"; $o += 16; } } </readmore>

In reply to Blocking on serial port! by jabidof

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.