Perl problem - COM ports and filehandles
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);
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
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:
my $ob = Win32::SerialPort->new ('COM8') || die;
$ob->user_msg(1); # misc. warnings
$ob->error_msg(1); # hardware and data errors
$ob->stty_icrnl(1)||die"icrnl set fail";
... do stuff with $ob
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...
More information about the london.pm