Solved

Perl USB Com port issue

Posted on 2013-12-19
8
1,011 Views
Last Modified: 2014-01-14
I'm running activeperl on a windows XP box. My code is below:

#!/usr/bin/perl -w

use strict;
use Win32::SerialPort qw( :PARAM :STAT 0.07 );

my $PORT = "COM5";
my $ob = Win32::SerialPort->new($PORT);

$ob->user_msg(1);
$ob->databits(8);
$ob->datatype('raw'); 
$ob->handshake('none');
$ob->parity("none");
$ob->stopbits(1);
$ob->baudrate(57600);
$ob->write_settings;

open( PORT, "$PORT" ) or die "Can't open $PORT: $!";
while (my $line = <PORT>) {
    if ($line =~ m!<ch1><watts>0*(\d+)</watts></ch1>.*<tmpr> *([\-\d.]+)</tmpr>!) {
        my $watts = $1;
        my $temp = $2;
        print "$watts, $temp\n";
    }
}
close(PORT);

Open in new window


However, this gives me an error (I'm running this from a command prompt currently):
Can't open COM5: Permission denied at currentcost.pl line 19.

Open in new window


I am logged in as an administrator account. I have confirmed the port settings match exactly to both my device, and those listed in "device manager" under windows.

The com port is a USB/Serial adapter, which works perfectly under hyperterminal etc and I can read output from the com port just fine.

Could someone please help?

Thanks!

Ross
0
Comment
Question by:RossDagley1
  • 2
  • 2
8 Comments
 
LVL 34

Expert Comment

by:Duncan Roe
ID: 39766425
Under Windows I seem to recall you get "Permission denied" on a COM port that is in use by another process. Could there be 2 instances of your Perl program running?
Can you verify that your program gets further using, say, COM1
0
 
LVL 19

Expert Comment

by:simon3270
ID: 39766712
Ah, good old CurrentCost!

I've got the same code running under Linux. The only real difference is in the open line - I have:
    open(DEV, "<$PORT") || die ....
(I.e. a "<" before the $PORT)
0
 
LVL 2

Accepted Solution

by:
RossDagley1 earned 0 total points
ID: 39768931
Yes. Unfortunately I needed this sorted in a timely manner and never did fix the actual issue. I worked around it by migrating to a linux box to run my stats.

Thanks for dropping in though.
0
 
LVL 19

Expert Comment

by:simon3270
ID: 39769117
Sorry I wasn't in time (didn't see it until shell scripting was added), but glad you got it sorted.

If you get a chance to test it, it would be interesting to see whether the "<" fix worked on Windows.
0
 
LVL 2

Author Closing Comment

by:RossDagley1
ID: 39778774
Resolved by myself with no expert assistance here.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question