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

serial port access... 5

Status
Not open for further replies.

simanek

Programmer
Joined
Jan 19, 2001
Messages
137
Location
US
Does anyone know how I'd write a script that would listen on a serial port or transmit on a serial port? Thanks.

Mike
 
I did a search on for "serial port" and came up with several pages of hits, including api's for raw win32 serial port calls. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
I've used Perl for serial port communication on WinNT. The modules that I used were Win32-SerialPort and Win32-API. The following code snippets show how I used these modules.

# 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
$Port = 'COM1';
$BaudRate=9600;
$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
$ob->write("hello world\r");

# Read from serial port
$DataRead = ReadPort("hello world", "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
 
any ideas for the modules in solaris?
 
Serial port control for Solaris is simpler. You don't need to download any special Perl modules. You can use "stty" to setup the port, syswrite to write, and sysread to read. Here's some code samples.

use POSIX qw(:termios_h); # Used terminal IO library

# Setup terminal settings
sub SetupTerm() {
$Port="/dev/cua/b"; # Define port to communicate with
# Open PORT
open(PORT, &quot;+<$Port&quot;) || die &quot;FATAL ERROR - Cannot open $Port. Check for tip windows&quot;);
$FHport = fileno(PORT);
$Term = POSIX::Termios->new();
$Term->getattr($FHport);
$Oterm = $Term->getlflag();
$Echo = ECHO | ECHOK | ICANON;
$NoEcho = $Oterm & ~$Echo;
$Term->setlflag($NoEcho); # Disable echo
$Term->setcc(VTIME, 5); # Set VTIME to 0.5 seconds
$Term->setcc(VMIN, 0); # Return immediately if byte received
$Term->setispeed(&POSIX::B38400); # Set input to 38400
$Term->setospeed(&POSIX::B38400); # Set output to 38400
$Term->setattr($FHport, TCSANOW); # Set the termio attributes
system(&quot;stty -a < $Port&quot;) if $Debug; # Display terminal settings
} # End of SetupTerm

# Write message one byte at a time
sub SendMsg($) {
$Msg = &quot;@_&quot;;
@CharList = split //, $Msg; # Split message into bytes
# Send message
foreach $Char (@CharList) {
$Written = syswrite(PORT, $Char, 1, 0); # Send a byte
die &quot;FATAL ERROR - syswrite() error: $!&quot; unless (defined($Written));
$BytesRead = sysread(PORT, $Junk, 1, 0); # Read a byte
die &quot;FATAL ERROR - sysread() error: $!&quot; unless ($BytesRead);
}
$Written = syswrite(PORT, &quot;\r&quot;, 1, 0); # Send \r
$BytesRead = sysread(PORT, $Junk, 1, 0); # Throw away \r
die &quot;FATAL ERROR - sysread error: $!&quot; unless ($BytesRead);
$BytesRead = sysread(PORT, $Junk, 1, 0); # Throw away \r
die &quot;FATAL ERROR - sysread error: $!&quot; unless ($BytesRead);
} # End of SendMsg

# Read all bytes
sub ReadBytes() {
$Offset=0;
do {
$BytesRead = sysread(PORT, $Reply, 1, $Offset); # Read a byte into $Reply
$Offset++; # Increase $Reply offset
die &quot;FATAL ERROR - sysread error: $!&quot; unless (defined($BytesRead));
} while ($BytesRead); # Read until no bytes left
return($Reply); # Return the string read
} # End of ReadBytes
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top