Solved

Perl USB Com port issue

Posted on 2013-12-19
8
947 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Resolved by myself with no expert assistance here.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This is an explanation of a simple data model to help parse a JSON feed
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now