I am trying to script communication with a USB Serial port (COM9) on windows. I found this link but it is 7 years old. Any chance there is a solution out there now?

I have tried Win32::SerialPort but it fails with an error "invalid COMMPROP block length= 66".

use Win32::SerialPort; use strict; use warnings; my $Port = "COM9"; # port to watch my $Baud=15200; my $PortObj = Win32::SerialPort->new ($Port,1) || die "Can't Open $Por +t: $!"; $PortObj->write_settings || undef $PortObj; print "Can't change Device_Control_Block: $^E\n" unless ($PortObj); $PortObj->baudrate($Baud) || die "failed setting baudrate to $Baud"; $PortObj->parity("none") || die "failed setting parity"; $PortObj->databits(8) || die "failed setting databits"; $PortObj->handshake("none") || die "failed setting handshake"; my $rtn=$PortObj->write("help"); print $rtn; sleep 1; print "DONE\n"; undef $PortObj; exit;

I have tried Device::USB but it does not seem to support USB devices on windows.

I have tried to use sysopen and syswrite (code below) but I am now sure how to get to to do anything for me. I hangs on the sysread. Plus I am not sure how to set the baudrate,etc with this method.

#!perl $port="COM9"; sysopen(COM,$port,O_RDWR) || die "Can't open $port $^E"; $length=1024; while(1){ print ">"; $cmd=<STDIN>; if($cmd=~/^(x|q|exit|quit)$/is){last;} $cmd =~s/[\r\n]+$//; print "Cmd: $cmd\n"; my $packcmd=pack("A*",$cmd); print "Send: $packcmd\n"; syswrite(COM,$packcmd); #sends $string to COM1 print "Reading...\n"; #code to read data from port sysread(COM,$buf,$length); #read $length bytes print "Buf: $buf\n"; $received = unpack("C*",$buf); #unpack them print "Receive: $received\n"; } exit;
Any help would be greatly appreciated. If I use hyperterminal, I can simply choose COM9 and set the baudrate to 115200 and I connect fine.
s/te/ve/

In reply to Connecting to a USB Serial Device on Windows by slloyd

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.