CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Perl/Linux and Serial Port Communications

 



bryandj23
New User

May 25, 2013, 7:36 AM

Post #1 of 3 (459 views)
Perl/Linux and Serial Port Communications Can't Post

Hi All. I'm a perl noob for sure, and am trying to write a quick perl script that will poll a device's information continuously via serial port.

The device accepts various hex commands, and immediately outputs status based on the command. I'm trying to obtain the device's LCD screen readout (the device returns 70 bytes of data for this), but am having a bit of difficulty.

I've verified that sending the command to the device works, as I can run 'cat /dev/ttyUSB0' in a second session and see the device's output. My perl script however, is not capturing the output - almost as if the output occurs to quickly for perl to pick it up.

Below is my code:


Code
!/usr/bin/perl -w 
use Device::SerialPort qw ( :PARAM :STAT 0.07);
my $port = new Device::SerialPort("/dev/ttyUSB0");
$port->user_msg(ON);
$port->baudrate(115200);
$port->parity("none");
$port->databits(8);
$port->stopbits(1);
$port->write_settings;

my $answer="";

$port->write( pack('H*', q[024C034F]));


# my $answer = $port->lookfor;
$answer = $port->read(70);
print $answer;


I've also tried other sample scripts found online, but the same behavior results.

If anyone has a suggestion, I would be greatful.

Thanks much!

Bryan


Laurent_R
Veteran / Moderator

May 25, 2013, 8:37 AM

Post #2 of 3 (455 views)
Re: [bryandj23] Perl/Linux and Serial Port Communications [In reply to] Can't Post

Hi,

I've never used this module, but I think that $port->read(70) returns an array, not a scalar.

Try maybe something like this:


Code
my @answers = $port->read(70);



bryandj23
New User

May 25, 2013, 8:44 AM

Post #3 of 3 (454 views)
Re: [Laurent_R] Perl/Linux and Serial Port Communications [In reply to] Can't Post

Hi Laurent. Thanks for your suggestion.

I modified the script as you suggested, but the same behavior results. I see the output in my second window running 'cat /dev/ttyUSB0', but the output is not printed via the perl script.


Code
my @answers = $port->read(70); 
print @answers;


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives