
perlmania
New User
Jan 11, 2010, 2:41 AM
Post #1 of 2
(988 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:
# 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:
No such file or directory If I examine the port from a command prompt using the mode command I get:
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:
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:
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?
|