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: Intermediate:
Accessing virtual COM port in perl

 



perlmania
New User

Jan 11, 2010, 2:41 AM

Post #1 of 2 (1295 views)
Accessing virtual COM port in perl Can't Post

I'm trying to write a bit of Perl code to access and read data from a serial port. The device is actually a USB GPS dongle that presents a serial port. Depending on which USB port I plug it into it presents as COM8, COM11 or COM14.

I'm using the following simple method:


Code
# Port parameters 
$Port = "COM11";
$Baud = 38400;
$Prty = "N";
$Data = 8;
$Stop = 1;


# Program
$Cmd = "mode ".$Port." baud=".$Baud." parity=".$Prty." data=".$Data." stop=".$Stop;
print "Configuring port ".$Port." operating parameters...\n";
system($Cmd);

print "Trying to open port ".$Port."...\n";
open (COM, '<', $Port) or die $!;

#while (<COM>) {
# print $_;
#}

close COM;
print "Port ".$Port." closed.\n";
print "Done.\n";


If, as per the above example, the device is on COM11 (or COM14) I get the following error:


Quote
No such file or directory


If I examine the port from a command prompt using the mode command I get:


Quote
Status for device COM11:
------------------------
Baud: 38400
Parity: None
Data Bits: 8
Stop Bits: 1
Timeout: OFF
XON/XOFF: OFF
CTS handshaking: OFF
DSR handshaking: OFF
DSR sensitivity: OFF
DTR circuit: ON
RTS circuit: ON


This proves that the port is present. I can also use mode to set the baud rate and other parameters if I choose.

I've also modified my code to use the Win32::SerialPort module but I get the same result when trying to open the port. I have not yet added the code to configure the port:


Code
use Win32::SerialPort; 

# Port parameters
$Port = "COM11";
$Baud = 38400;
$Prty = "N";
$Data = 8;
$Stop = 1;

# Program

print "Trying to open port ".$Port."...\n";
$ComPort = new Win32::SerialPort($Port, $Quiet)
|| die "Can't open port ".$Port."$^E\n";

#while (<COM>) {
# print $_;
#}

close COM;
print "Port ".$Port." closed.\n";
print "Done.\n";


The strange thing is that if I plug the dongle into the USB port that presents it as COM8 then the code seems to work.

I also get something similar if I try the following:


Quote
C:\>type com11
The system cannot find the file specified.


Whereas if I try this with com8 I do get the GPS data spewing out to the console.

I've also tried this with a PL2303 based USB to serial adapter and I get exactly the same problem - works on COM8 but not on COM11 or COM14.

The basic GUI application that came with the dongle on the other hand works on all ports. I initially thought that this might be a windows command environment issue but the mode command does work so I'm rather puzzled.

Can anyone help me understand what's going on here please?


perlmania
New User

Jan 13, 2010, 12:59 AM

Post #2 of 2 (1271 views)
Re: [perlmania] Accessing virtual COM port in perl [In reply to] Can't Post

After a lot of digging I found an answer.

I found a website that discussed COM ports in connection with an unrelated prouct:

http://www.stepperboard.com/stepperboard_com_port_names.htm

In this atricle it mentions the Microsoft naming convention for COM ports larger than 9. According to the article, COM ports above 9 require the "network predicate string "\\.\" to be prepended to the COM port name".

I tried this in my perl program and found that it worked! Of course, in perl, "\" is a special character so I needed to specify the above string as "\\\\.\\".

I tried searching the Microsoft website for further information but I couldn't find anything specific to confirm this finding.

I have now added a piece of code to my program that automatically prepends the appropriate string to the port reference. I just thought I'd post my findings here in case anyone else comes across this issue.


(This post was edited by perlmania on Jan 13, 2010, 1:16 AM)

 
 


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

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