pwillard.com

Floobydust — Open Source experimentation

30 Jun

More on Serial Ports


(Please note: None this applies to LINUX.  Linux is much smarter about “devices” IMHO)

When testing out an idea, it sometimes a lot easier to write a little tidbit of code in PERL to do some concept verification.

Case in point:  I’m developing a a protocol to run over a RS485 bus and need to “act” like a master host from my PC.  Now I haven’t got the MASTER host code written yet and getting a protocol tested from a Serial Terminal program is tedious so I needed a quick way to send oddball strings.  PERL to the rescue…  and then I hit the brick wall.

I tested the code with stationary PC that has a “real” serial port on the motherboard.  It worked as planned so I went to the LAB table where I have a laptop (with no built in serial) and I tried the code.  I have an IOGEAR 2-port USB to SERIAL adapter on that laptop but I had not until then tried to access the device with PERL.

I am using the PERL module WIN32::SerialPort(0.19) and I kept getting “can’t open port” error messages.  I was not getting these on the other PC.  The only difference was that the IOGEAR USB-RS232 adapter was now using COM ports 13 & 14.   When I changed the PERL code to read COM13, it broke.

Here is the fix:

my $myPort = 11;

# COM PORT
# Deal with ports higher than 9 if($myPort < 10){ $myPort_Str = "COM".$myPort; print "low port: $myPort_Str\n"; } else { $myPort_Str = "\\\\.\\COM".$myPort; print "high port: $myPort_Str\n"; }

I guess Microsoft never imagined you would have more than 9 serial ports.

Ports 10 and Higher will not respond to being called “COMx”.  You need to use the following naming method: “\\.\COMxx”.

So when I open the port, I use:

my $port = Win32::SerialPort->new($myPort_Str);

This way, you can use higher number ports that would by default have the system saying, “sorry, port doesn’t exist” .   USB-RS232 drivers have a tendency to assign ports higher than COM9.


Filed under: ramblings

Post a Comment