Hi Danny,

I ran into a similar situation recently where I wanted to be able to write to and read from a serial port. I found some documentation, but there didn't seem to be much out there.

I needed to be able to control a line simulator via a script using a serial connection to the device. In this case I was using Perl 5.8 on Win32. Here is my script prototype that worked...

#!/usr/bin/perl -w

use strict;
use vars qw($loopdist);

$loopdist = 0;

foreach $loopdist (0,500,1000,1500,2000) {
   &ChangeLoopLength;
}

##########  Change the Loop Length  ##########
sub ChangeLoopLength {
   my $z = "";
   my $bytes = 0;

   if ($loopdist == 0) {
      $bytes = 15;
   }
   if ($loopdist == 500) {
      $bytes = 16;
   }
   if ($loopdist > 999) {
      $bytes = 17;
   }
   # open a filehandle for COM port for rw access
   # change "COM5" to "/dev/ttys0" or whatever for your platform
   open( PORT, "+>COM5" ) or die "Can't open COM5: $!";

   # set the $loopdist length
   print PORT ("SL:0,0,0,$loopdist,N\r");

   read (PORT, $z, $bytes);
   print "$z\n";
   sleep 2;

   close PORT;
}

Now, granted, this doesn't deviate much from the documentation you've probably seen out there, but it may be enough to get you started. The weird assignment for $bytes to read seemed necessary to clean up the buffers. Making the filehandle hot did not solve the buffer problem I had. You may have a different experience writing to your serial device.

If you are looking for a module, Win32::SerialPort is available as was mentioned earlier by Steven in this thread. It may sound strange at first, but Net::Telnet ("print" and "waitfor" I believe) can also be used to write and read to serial ports.

I also tried to use Expect.pm, but was unsuccessful because there was a component needed that I couldn't compile on my machine and I didn't have time when I was doing this script to debug the cause.

Hope this helps,
Jeff



Wong, Danny H. wrote:

Does anyone know how I can send commands to the device? What is the
command. I read the documentation, it wasn't very clear on send input to
thte device? Any help will be greatly appreciated!

Thanks
Danny Wong
SCM Engineer
PowerTV Inc.,
"We turn again to the unending struggle for the common good of all
Americans and for those multitudes around the world who look to us for
leadership in the cause of freedom." Al Gore


-----Original Message-----
From: Steven Satelle (Service Desk)
[mailto:[EMAIL PROTECTED] Sent: Monday, October 27, 2003 11:15 PM
To: Wong, Danny H.; [EMAIL PROTECTED]
Subject: RE: Detect serial connection?



Wong, Danny H. wrote:


Hi All,
        I was wondering if there is a way in perl to see if my com1 port
is connect to another device?


take a look at http://members.aol.com/Bbirthisel/SerialPort.html You could try and talk to the device, if you get an answer there's something connected :-)



_______________________________________________
Perl-Win32-Admin mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to