Re: Unable to read the string



Arun wrote:

Hi this is Arun here, i am new to perl.Here i am trying to read a
string from the serial port but i am not able to and this is my
program:

# 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);

You shouldn't use prototypes for Perl subroutines: they don't do what you think
they do, and they are more likely to break things than be useful.

Unless you have your own reasons for naming your variables with mixed case, many
people would thank you if you changed to using all lower-case. Names beginning
with a capital letter are usually package or module names.

What problem are you having? It looks basically alright to me, although it may
not be wise to use the value of $result if $count is zero. How about a loop like
this:

{
my ($count, $result) = $ob->read(1);
last unless $count;
print $result if $display;
$reply .= $result;
redo unless $reply =~ /\Q$string/;
}

HTH,

Rob
.



Relevant Pages

  • Unable to read the string
    ... Hi this is Arun here, i am new to perl.Here i am trying to read a ... string from the serial port but i am not able to and this is my ... # Display messages if enabled ...
    (perl.beginners)
  • Re: serialPort receive problem
    ... That would be expected, as the serial port data could not have been transmitted, and a response received, in the time that you have allowed between the Write command and the ReadExisting call. ... Every thing works fine except whenever I receive data I cannot display ...
    (microsoft.public.dotnet.languages.vb)
  • Re: cross-platform serial api for gforth
    ... serial port on Unix and Windows? ... The project we are working on is an interactive display for a device ... way for me to use Gforth on a Winblows machine? ...
    (comp.lang.forth)
  • Re: Worker thread in VC++ 6
    ... LRESULT OnUpdateStatus(WPARAM wParam, LPARAM lParam); ... It is just there to test if data from serial port is read corectly. ... Once you send it to a message that is handled by your UI thread then you can do just about anything with the data (display, log to file, accumulate, etc.) ...
    (microsoft.public.vc.mfc)
  • Re: cross-platform serial api for gforth
    ... implement serial port I/O and the top level code be the same. ... If you start from what Windows and Unices offer at the raw API ... the serial port stream is just that, ... emulation for display of the data that is updated in real time. ...
    (comp.lang.forth)