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

Win32::SerialPort question

Status
Not open for further replies.

ThomVF

Programmer
Feb 8, 2001
270
US
I am using this module to communicate asynchronously with a "DBC Signal" box (Stock quotes) thru COM1. I am able to send commands to the box - but cannot tell if I am in asynch verus polling-mode.

The synopsis says it handles both modes but doesn't tell me how to get into one or the other....I'm not a COM genius either...when I send a command, I use the command "$stringsent = $Port->input;" to see what I sent to the box, but this command actually gets me the box's reply !! I expected the command "$Port->read($InBytes)" to get the results - anyone been here before ??
 
Not sure about your particular situation, but the following code worked for my serial port application.

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

# Write command
$ob->write("command");

# Read reply - timeout after 4 seconds
$DataRead = ReadPort("$Prompt", "4000", "1");
if ($DataRead == 1) {
print "Read timed out \n";
}

# Read serial port until expected message or timeout occurs
sub ReadPort($$$) {
(my $String, my $TimeOut, my $Display) = @_;
$ob->read_const_time($TimeOut); # timeout value in mS
my $Reply = ""; # Initialize message
do {
($Count, $Result) = $ob->read(1); # Read 1 byte
$Reply .= $Result; # Build message
} while($Count > 0 and $Reply !~ m/$String/);
return(1) if ($Reply !~ m/$String/);
return($Reply);
} # End of ReadPort ------------------------------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top