Trouble with 0

R

Richard S Beckett

Guys,

I have managed to get this far in my quest to capture data sent into the
serial port with windows...

use strict;
use warnings;
use Win32::SerialPort;
my $port = new Win32::SerialPort ("COM1") || die "can't open COM1\n";
open (FILE, ">file.txt");
foreach (1..100000) {
my $input;
if ($input = $port->input) {print FILE "$input\n"}
}
print "Finished\n";
close FILE;

This seems to work OK, unless I get a zero sent into the port. I can't work
out how to trap the zero, and distinguish it from no input.

Can someone help me out, please?

Thanks.
 
M

Michael Budash

Richard S Beckett said:
Guys,

I have managed to get this far in my quest to capture data sent into the
serial port with windows...

use strict;
use warnings;
use Win32::SerialPort;
my $port = new Win32::SerialPort ("COM1") || die "can't open COM1\n";
open (FILE, ">file.txt");
foreach (1..100000) {
my $input;
if ($input = $port->input) {print FILE "$input\n"}
}
print "Finished\n";
close FILE;

This seems to work OK, unless I get a zero sent into the port. I can't work
out how to trap the zero, and distinguish it from no input.

Can someone help me out, please?


try:

if (defined($input = $port->input)) {print FILE "$input\n"}

hth-
 
T

Tore Aursand

foreach (1..100000) {
my $input;
if ($input = $port->input) {print FILE "$input\n"}
}

This seems to work OK, unless I get a zero sent into the port. I can't
work out how to trap the zero, and distinguish it from no input.

That's because you're really testing if your 'if()' statements returns
true or false in your code above. A '0' means false, even when it's a
"true vale" for you. :)

You could try this code instead:

foreach ( 1 .. 100_000 ) {
my $input = $port->input();
if ( defined $input ) {
print FILE $input . "\n";
}
}

Hope this helps!


--
Tore Aursand <[email protected]>

"You know the world is going crazy when the best rapper is white, the best
golfer is black, France is accusing US of arrogance and Germany doesn't
want to go to war."
 
C

ctcgag

Richard S Beckett said:
Guys,

I have managed to get this far in my quest to capture data sent into the
serial port with windows...

use strict;
use warnings;
use Win32::SerialPort;
my $port = new Win32::SerialPort ("COM1") || die "can't open COM1\n";
open (FILE, ">file.txt");
foreach (1..100000) {
my $input;
if ($input = $port->input) {print FILE "$input\n"}
}
print "Finished\n";
close FILE;

This seems to work OK, unless I get a zero sent into the port. I can't
work out how to trap the zero, and distinguish it from no input.

test $input with either "defined" or "length", depending on whether the
input you wish to skip is undef, or the empty string, respectively.

Or, just take out the "if" altogether, as printing either an empty string
or an undef is pretty much the same as not printing it.

Xho
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,139
Messages
2,570,805
Members
47,356
Latest member
Tommyhotly

Latest Threads

Top