Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need serial port access for very simple communication

Status
Not open for further replies.

Riverdome

Technical User
Feb 28, 2005
5
US
I'm fairly new at Perl but have been fighting with SerialPort.pm and Commport.pm for too long now. All I need to do is send a very simple command out of the serial port into an electrical device. A batch file won't work for this project but if it would this is how it would read

MODE COM1: BAUD=96 PARITY=N DATA=8 STOP=1
ECHO T0>COM1
ECHO T1>COM1

Is there a relatively simple way to accomplish this with perl in a Windows environment? If it was much simplier in a unix world I would consider that as well.
 
You could start the HyperTerminal from windows and pass your commands from there to your device.

Or you could try

Device::SerialPort (for *nix) from ftp://cpan.org/modules/by-module/Device/Device-SerialPort-1.tar.gz

or

Win32::SerialPort (for Win) from ftp://cpan.org/modules/by-module/Win32/Win32-SerialPort-0.19.tar.gz

Be sure to read the 'readme' file first and then the 'documentation'

I hope these will help.
 
Thanks for the reply perluser,

I can successfully achieve this through hyperterm but I guess I didn't elaborate enough in my first post. I need to be able to trigger this event via a button on my webpage, thus I turned to perl. Seemed so simple a month ago but I still don't have an answer. I need to transmit two characters (could be any two from a list of ~20 commands) through the serial port just by clicking the corresponding button on my site. Must use the serial port on the web server (@ home) and not on the client.
 
Paul,

Currently I'm only running MS Pers Web Server. This is a smaller home automation project so up 'til now it has been usable. I'm not tied to PWS and would make changes if I know those changes will help me reach my goal.
 
Can you post some code, obfuscate passwords, hostnames, IP's and anything else that's private

--Paul

cigless ...
 
#!/usr/bin/perl -w
use strict;
use Win32::SerialPort;
sub openPort($);
sub closePort($);
my $DEVICE = "COM1";
my $serial = openPort ($DEVICE);
$serial->write("T1");
closePort ($serial);
sub openPort($)
{my ($device) = @_;
my $serial = Win32::SerialPort->new ($device, 1);
die "Can't open serial port $serial: $^E\n" unless ($serial);
$serial->user_msg(1);
$serial->databits(8);
$serial->baudrate(19200);
$serial->parity("none");
$serial->stopbits(1);
$serial->handshake("rts");
return $serial;}
sub closePort($)
{
my ($serial) = @_;
$serial->close();}



This script calls the SerialPort.pm module. I obtained SerialPort.pm from CPAN. It calls the CommPort.pm module, also acquired from CPAN. My specific error is actually in the CommPort file, something about FAME HLI missing ~~ sorry don't have that available right now.

After digging into both modules it just seemed like there had to be a way to acomplish this w/o hundreds of lines of code. Maybe not, but thats why I\'m asking :)
 
RD,

try this, I assume it's a modem you're talking to here, don't have one in this box, sorry can't test

Code:
#!/usr/bin/perl -w
 use strict;
 use Win32::SerialPort;
  my $DEVICE = "COM1";
  my $serial = openPort ($DEVICE);
  $serial->write("T1");
  closePort ($serial);

sub openPort {
  my ($device) = @_;
  my $serial = Win32::SerialPort->new ($device, 1);
  die "Can't open serial port $serial: $^E\n" unless $serial;
  $serial->user_msg(1);
  $serial->databits(8);
  $serial->baudrate(19200);
  $serial->parity("none");
  $serial->stopbits(1);
  $serial->handshake("rts");
  return $serial;
}
sub closePort {
  my ($serial) = @_;
  $serial->close();
}

You had multiple declarations of close port, and you were passing $ in parens to the sub - I've never seen this before

It passes a syntax check, but it did before as well, no guarantees

Give it a lash

--Paul

cigless ...
 
Paul,

It's actually isn't a modem but I don't think that will matter. Once the "black box" receives the T1 signal it does a function (in this case allows electricity to flow to port T1 thus turning on a light) If I send T0 or T2 it effect different devices within the home. All testing works when I manually use hyperterm or a batch file so I know it's a perl syntax/logic problem. I'll try the new file tonight, Thank You.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top