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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to output strings to COM1? 1

Status
Not open for further replies.

Tels

IS-IT--Management
Jul 10, 2001
290
GB
Hope someone can help me. I'm a newbie perl programmer, and I have a real issue to solve, (for the first time)
Basically, I want to use perl to output messages to a radio connected to COM1. It uses Hayes-AT strings to control it such as: AT@Dxxx^130,0,1,0,0,123,0,Message...
xxx is a variable (radio number to send message to)

Is is possible to use perl for this, and how..... as I could then concentrate on the interface design itself

Hope you can help.

Tels

Win2000 Network Administrator
 
I am trying to install 2 modules, SerialPort.pm and CommPort.pm
I have downloaded them, decompressed the zips, and used PERL makefile.pl on both of them, and put them in the right directories. (perl\lib\win32 and perl\lib\win32api)

The script I am using is having troubles working. From what I can gather, The script, comcall.pl loads the two .pm modules. I know this because there were errors which went away when I placed the modules in the right folders.

Now I get a similar error message:

Can't locate loadable object for moule Win32::API in @INC(@INC contains: C:/perl/lib c:/perl/site/lib .) at C:/Perl/lib/Win32API/CommPort.pm line 5
Compilation failed in require at.... etc etc etc

I figure that the CommPort.pm module is itself calling for another module, and not finding it.

How wrong am I and how do I fix this... HELP!!!!

Tels Win2000 Network Administrator
 
I've installed both of these modules and they work fine in the following directories:

SerialPort.pm - C:\perl\site\lib\Win32
CommPort.pm - C:\perl\site\lib\Win32API

I can also help if you need examples of how to use these modules.

Good Luck
 
Brilliant! I installed both modules via the Module installer, (had to re-install activestate perl first) and now the script is running without errors.

PLS NOTE - I am using Activestate Perl on Win2K PRO SP2

My issue is now this: When I use the following script the program runs fine. The modem should however re-set when given the string 'ATZ'

I have copied a text file to COM1 using the command line, and the modem resets - so the modem side works fine. However, when the script outputs the string to COM1 nothing happens. Why????!? Note - there are no errors reported on the screen - so it should be working.

# Add serial port modules
BEGIN {
eval "use Win32::SerialPort";
die "$@\n" if ($@);
eval "use Win32::API";
die "$@\n" if ($@);
} # End BEGIN
use Win32;

# Define serial port parameters
# I verified all this was correct
$Port = 'COM1';
$BaudRate=38400;
$Parity="none";
$Data=8;
$Stop=1;
$HandShake="none";

# Open serial port
$ob = Win32::SerialPort->new ($Port);
die "Can't open serial port $Port: $^E\n" unless ($ob);

# Setup serial port
$SetupStat = SetupPort($BaudRate, $Parity, $Data, $Stop, $HandShake);
die "ERROR: Could not set up serial port \n" if ($SetupStat != 0);

# Write to serial port (this line should reset the modem)
$ob->write("ATZ\n");

# Read from serial port - Disabled by me
# $DataRead = ReadPort("OK", "2000", "0");

# Close serial port
undef $ob; # Close serial port


# Subroutine to setup the serial port
sub SetupPort($$$$$) {
(my $SetBaud, my $SetParity, my $SetData, my $SetStop, my $SetHshake) = @_;
# Read serial port parameters
my $baud = $ob->baudrate;
my $parity = $ob->parity;
my $data = $ob->databits;
my $stop = $ob->stopbits;
my $hshake = $ob->handshake;

$ob->baudrate($SetBaud) || return(1);
$ob->parity($SetParity) || return(1);
$ob->databits($SetData) || return(1);
$ob->stopbits($SetStop) || return(1);
$ob->handshake($SetHshake) || return(1);
$ob->write_settings || return(1);
return(0);
} # End of SetupPort

# Read serial port until message or timeout occurs
sub ReadPort($$$) {
(my $String, my $TimeOut, my $Display) = @_;
$ob->read_const_time($TimeOut); # Setup timeout value in mS
my $Reply = ""; # Initialize message
do {
($Count, $Result) = $ob->read(1); # Read serial port
$Reply .= $Result; # Build message
print "$Result" if ($Display); # Display messages if enabled
} while($Count > 0 and $Reply !~ m/$String/);
print "\n" if ($Display); # Put carriage return at end of displayed output
if ($Reply !~ m/$String/) {
print "ERROR: Read timed out waiting for '$String' \n";
return(1);
}
return($Reply);
} # End of ReadPort

Thank you all kind sirs/ladies for your immininent help...

Tels
:cool: Win2000 Network Administrator
 
Oh PS I would LOVE some examples of how to further use those modules.. lots of ways of building on this project (endless possibilities!)

Nice one raider

Cheers
Tels Win2000 Network Administrator
 
Did you install the modules with PPM? I've never gotten that to work with the SerialPort and CommPort modules. I've always had to install them manually.

To solve your problem, try using a carriage return '\r' instead of a new line '\n'. So the line would be:

$ob->write("ATZ\r");

For some other examples, use this link

 
Great! It worked first time. I realise now that there is a big difference between /n and /r, seems a bit obvious now I suppose!

Really appreciate your help. Good link!

Cheers

Tels Win2000 Network Administrator
 
PS You're right... I only used PPM to install Win32::API. The other mods were installed manually. Win2000 Network Administrator
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top