I agree that Win32::SerialPort is the way to go, I have used it myself. The API is not simple and the docs are none too clear, though, so I made my own wrapper to make it simpler to use. In case it is of use to you, I am including below my wrapper class (called Win32::RS232Port) and some snippets from an application of mine that use it.
The Win32::RS232Port class:
An example of using the Win32::RS232Port (The app uses the classic hashref-as-object OO style, which is why the serial port object is stored in $self->{serport}):
# open serial port, create instance of Win32::RS232Port object
$self->{serport} = Win32::RS232Port->new($comport_name, handshake =>
+'rts', baudrate => 9600);
if (!defined $self->{serport}) { die "Could not open reference COM por
+t"; }
# manually set state of handshaking output lines:
$self->{serport}->rts_active(1);
$self->{serport}->dtr_active(1);
# transmit some data:
$self->{serport}->qtx("SYST:ERR?\r\n");
sub poll {
# call this function regularly to check for received data and to p
+ass on any queued data for transmission
my $self = shift;
if ( defined $self->{serport} ) {
# pass on any queued data for transmission
$self->{serport}->poll_tx;
# handle incoming data: here I'm just appending it to $self->{
+com_rxd} where it can be picked up by code that's interested.
my $rx = $self->{serport}->rx_data;
if ($rx ne q{}) {
$self->{com_rxd} = q{} if !defined $self->{com_rxd};
$self->{com_rxd} .= $rx;
}
}
}
sub wait_for_tx_done {
my $self = shift;
my $tstart = time;
while (defined $self->{serport} && $self->{serport}->tx_pending) {
$self->poll();
die "Timeout on serial port transmission\n" if time > $tstart
++ 10;
}
}
# close the serial port
$self->{serport}->close();
--
"Any sufficiently analyzed magic is indistinguishable from science" - Agatha Heterodyne
|