jismake has asked for the wisdom of the Perl Monks concerning the following question:

I am not able to write or read to/from virtual serial port. The virtual serial ports are created by a software app. the virtual ports are paired-ie one port can spy on other. if i use a hyperterminal, it works with one write to COM port seen on the other.
use Win32::SerialPort; use strict; use warnings; $| = 1; #enable autoflush my $PortName = "COM1"; my $sendData = "12345678"; ### SERIAL PORT SETUP ### my $PortObj = new Win32::SerialPort($PortName) or die "Can't open $Por +tName: $^E\n"; $PortObj->baudrate(57600); $PortObj->parity("none"); $PortObj->databits(8); $PortObj->stopbits(1); $PortObj->write_settings(); #very important! $PortObj->write($sendData); $PortObj->close() || warn "\nClose failed\n";

what could be wrong? I have windows 7, laptop with no real COM ports. i use a virtual serial port kit software to create virtual ports.

any help appreciated. jis

Replies are listed 'Best First'.
Re: Virtual COM port write
by Anonymous Monk on Apr 20, 2014 at 10:53 UTC

    Please wrap your text in <p> and your code in <c> tags so it's readable.

    A few things to try:

    1. Check the return value of write_settings(): $PortObj->write_settings or die "failed to write_settings";

    2. Turn off handshake: $PortObj->handshake("none");

    3. Check the return value of write():

    my $count_out = $PortObj->write($sendData); warn "write incomplete\n" if $count_out != length($sendData);

    4. Wait a little while before closing the port - add a sleep(3); before $PortObj->close(). (Note: Although this may not be needed, a quick skim of the module's docs don't make it clear whether write() is blocking or not.)

      Not so lucky yet. I got following error Use of uninitialized value $count_out in numeric ne (!=) at test2.pl line 22. write incomplete

        That means write() returned undef, which means the write failed entirely. Try checking if $^E contains a useful error message: my $count_out = $PortObj->write($sendData) or die "write failed: $^E";

        Try $PortObj->error_msg(1); $PortObj->user_msg(1); right after new() as shown in the module's docs.

        Make sure that your handshake really is none, that depends on how the other side of the serial connection is configured.