just a warning up front, I think this is a perl problem, but it MAY end up being a linux problem. if so, i apologize ahead of time.

i've a device that connects via an RS232 serial cable, and I'm trying to read/write data to/from it. here's the thing, it works fine in windows using Win32::SerialPort, but in linux with Device::SerialPort, it print()s data to the device (/dev/tty0) with no problem, but it read()s nothing at all, whatever it seems to return doesnt print() to screen, isnt a blank string, and isnt undef.

here's the test script i'm using:

#!/usr/bin/perl -w use strict; use Device::SerialPort qw( :PARAM :STAT 0.07 ); use Term::ANSIColor qw(:constants); print YELLOW,"Location of device: ",RESET; my $port = <>; # prompt for device, eg: /dev/tty0 chomp $port; my $rootDir = "~"; my $config = "$rootDir/.testconf"; open(LOG,">>$rootDir/test.log"); print LOG "started\n"; # protocol-specific control chars my %IM = ( 'SOM' => chr(2), #Start of message (STX) 'EOM' => chr(3), #End of message (ETX) 'AFF' => chr(6), #Affirmative Acknowledge (ACK) 'NEG' => chr(21), #Negative Acknowledge (NAK) 'POL' => chr(5), #Poll for data (ENQ) 'SEL' => chr(7), #Alarm (BEL) 'EOR' => chr(13), #End of record (CR) 'DLE' => chr(16), #DLE 'RES' => chr(4), #Reset (EOT) 'ESOM' => chr(15), #On Error, Start of Message (SI) 'EEOM' => chr(14), #On Error, End of Message (SO) ); print "\nCreating config file...\n"; my $PortObj = new Device::SerialPort ($port) || die("Can't open $port: + $^E\n\n"); $PortObj->databits(7); $PortObj->baudrate(19200); $PortObj->parity("even"); $PortObj->stopbits(1); $PortObj->handshake("xoff"); $PortObj->buffers(4096, 4096); $PortObj->write_settings || undef $PortObj; $PortObj->save("$config"); $PortObj->close || die("failed to close\n\n"); if(defined $PortObj) { print YELLOW, "Success!\n", RESET; } else { die("PortObj is undefined!\n\n"); } undef $PortObj; print LOG "config file created and saved\n"; print "\nTie()ing to port...\n"; $PortObj = tie (*SERIAL, 'Device::SerialPort', "$config") || die("Can't open $port: $^E\n\n"); # use non-blocking read on serial port $PortObj->read_char_time(5); $PortObj->read_const_time(500); print YELLOW, "Success!\n",RESET; print LOG "tied to serial port\n"; my $EXIT = 0; $SIG{'INT'} = sub { $EXIT = 1 }; my $data; while($EXIT == 0) { print "\nPolling for data...\n"; $data = pollForData(); print YELLOW, "Success!\n",RESET; print "\nData: ",GREEN,"$data",RESET; } print LOG "stopping\n"; close(LOG); print "\n\n"; $PortObj->close || die("failed to close"); untie *SERIAL; undef $PortObj; exit; sub pollForData { my($length,$buffer,$lrc,$input); $buffer = join('',RED,"NO DATA",RESET); print SERIAL $IM{'POL'}; $length = read(SERIAL,$buffer,4096); if($buffer eq '') { return join('',RED,"BLANK STRING",RESET); } if(!defined $buffer) { return join('',RED,"UNDEFINED STRING",RESET); } if($buffer ne $IM{'RES'}) { print SERIAL $IM{'AFF'}; } # otherwise, return data return $buffer; }
when this is run, it saves and loads the config file without a problem, tie()s to a filehandle without a problem, and then prints "NO DATA" in the loop, meaning it doesnt match '' or undef...

i'm stumped, could anyone please help me?


In reply to cant read serial data with Device::SerialPort by Anonymous Monk

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.