I needed to talk to two serial port devices, one I had working some time ago on Linux, and the other I got working within 10 minutes on Linux, but porting to windows proved to be quite hard. I got ActivePerl, installed Win32::Serial using the package manager:
ppm install Win32::SerialPoint ppm install Win32::API
the code that works on Linux opens a file handle and uses stty to set the baud rate:
use strict; my $deckbox = "/dev/ttyUSB0"; open(DECK, "+<$deckbox") || die('Couldn\'t open GPS'); system("stty -F $deckbox 19200"); while (my $line = <DECK>) { print $line; print DECK "\$CCPDT,1,4,0,0,3000,1,0,0,0\r\n"; }
Elegant and simple, right? On windows, there is no stty, so barring installing cygwin, which I would rather not have to do, I had to use the Win32::Serial package. It works fine for a GPS, but for this device, inexplicably no. Perhaps its one of many parameters of the serial port, so I will paste in the output to stty -a -F/dev/ttyUSB0 on linux:
stty -a -F /dev/ttyUSB0 speed 19200 baud; rows 0; columns 0; line = 0; intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; +eol2 = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; + min = 1; time = 0; -parenb -parodd cs8 hupcl -cstopb cread clocal -crtscts -ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixo +n -ixoff -iuclc -ixany -imaxbel opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs +0 vt0 ff0 isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -ec +hoprt echoctl echoke
The windows code that gives back gibberish:
require 5.003; use Win32::SerialPort qw( :STAT 0.19 ); use strict; my $line; my $deck = openSerial("com3:", 19200, 'none', 8, 1); # $deck->handshake("xon"); # $deck->buffers(4096,4096); do { my $bytesWritten = $deck->write('$CCPDT,1,4,0,0,3000,1,0,0,0' . "\r\n"); $line = rawReadSerial($deck); } while (1); sub readLineSerial { my ($ser) = @_; my ($numChars, $c); my $line = ''; do { do { ($numChars, $c) = $ser->read(1); } while ($numChars < 1); $line .= $c; } while ($c ne "\n"); return $line; } sub rawReadSerial { my ($ser) = @_; my ($numChars, $c); my $line = ''; do { do { ($numChars, $c) = $ser->read(1); #print "$numChars read=$c\n"; } while ($numChars < 1); print $c; $line .= $c; } while ($c ne "\n"); return $line; }
Can anyone tell me what I'm missing?

In reply to serial port control on Windows by hydrodog

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.