Perl problem - COM ports and filehandles

Darren Harwood darren.harwood at pb.com
Wed Feb 1 16:59:17 GMT 2012


good afternoon all,

me=long time lurker, first time poster!

(warning! there is code in the post below - but as a first time poster i have no idea how well
that is going to format itself for your consumption...we'll see....i apologise now if its a garbled mess.
i did try to find posting advice on the london.pm website, but no joy)

i am trying to convert a perl script from *nix to win32.
i am having difficulty with a couple of things:

1) the unix script opens a filehandle to a device like this:

open my $pipe, '+<', '/dev/ttyUSB0' or die "Couldn't open pipe for reading and writing";
my $mb = myDevice::myObject->new($pipe);

this filehandle is then referenced in the module via a mechanism containing:
my $rin = '';
vec($rin,fileno($pipe),1) = 1;
my ($nfound, $timeleft) = select($rin, undef, undef, $timeDelimiter_s);
last if($nfound == 0);
my $bytes;
sysread($pipe, $bytes, $nfound);

my (clearly incorrect) approach was to substitute the "open" for a use Win32::SerialPort; 
(because the device is connected via a virtual serial port over usb)
with the following:

my $PortObj = tie (*FH, 'Win32::SerialPort', $Configuration_File_Name) || die "Can't tie: $^E\n";

which i would expect to allow me to code:
my $mb = myDevice::myObject->new(FH);

but perl complains about the FH bareword.
using *FH in the new method allows the script to compile, but i am unsure of the impact!?!

with my (dodgy?) *FH, i then hit the problem:
Can't locate object method "FILENO" via package "Win32::SerialPort"
when it hits the line:
vec($rin,fileno($pipe),1) = 1;

and i go no further. i clearly dont have a valid filehandle in my FH at this point - but i'm not sure why
or what alternate approach to use. (aside - how do i know if my "tied" filehandle is opened for input/output/both in this
context?)
i appreciate that the "vec" and "fileno" commands are quite rare (to me, they were brand spanky new) and primarily
associated with low level tty operations - so perhaps they dont work the same on win32 as *nix?

thoughts on equivalent code?


2) when (if) i get problem 1 fixed......i need to open the COM port at a non-standard
baud rate of 1000000. 
so....i have tried the code i normally use for my other (working!) serial communication perl scripts:

use strict;
use Win32::SerialPort;

my $Configuration_File_Name="port_config.ini";

my $ob = Win32::SerialPort->new ('COM8') || die;
$ob->user_msg(1); 	# misc. warnings
$ob->error_msg(1); 	# hardware and data errors
$ob->baudrate(1000000);
$ob->parity("none");
$ob->databits(8);
$ob->stopbits(1);
$ob->stty_icrnl(1)||die"icrnl set fail";
$ob->handshake('none');
#$ob->read_const_time(15000);
$ob->write_settings||die"setting failed";
$ob->save($Configuration_File_Name);
... do stuff with $ob
exit 0;

which gives me the error:
Could not set baudrate on COM8

any pointers on custom baud rates?
have looked at docs for Win32::SerialPort and (tried to) understand the intricacies of Win32API::CommPort (which Win32::SerialPort uses), but no joy.

sorry if i haven't posted quite the right snippets here.....once you have been looking at things for so many hours,
you can't see the wood through the trees...and what's obvious to me, won't be to others i'm sure..

there is no panic on this....it is "play" rather than "work" (by default it is therefore far more interesting!).

thanks for reading! sorry to disturb. hello. goodbye. etc...

dch26










More information about the london.pm mailing list