$arduino->parity("none");
-
$arduino->stopbits(1);
15
$arduino->read_const_time(1);
-
$arduino->read_char_time(1);
-
-
sleep(2);
-
while (1) {
20
$arduino->write("a0\n");
-
my ($count, $line) = $arduino->read(255);
-
print $line;
-
}
-
We check whether the name of a serial port was passed on the command line.
Then we create a new
Device::SerialPort
instance in line 11. We configure all
serial port parameters, and in line 16, we set a timeout value for
read
calls. If
we did not set it,
read
would return immediately, giving the Arduino no time
to respond.
read_char_time
sets a timeout for the waiting period between two
characters.
Then we sleep for two seconds and start an infinite loop. Here we send the
string “a0” to the serial port and read the Arduino’s response using the
read
method.
read
expects a maximum number of bytes to be read, and it returns
the actual number of bytes read and the data it received. Finally, we output
the result to the console. A typical program run looks as follows:
maik> perl analog_reader.pl /dev/tty.usbmodem24321
a0: 496
a0: 366
a0: 320
^C
That’s it! It takes only about twenty lines of Perl code to create a client for the
analog reader Arduino sketch. So, Perl is a good choice for programming
Arduino clients, too.
report erratum • discuss
Serial Communication Using Various Languages • 265
www.it-ebooks.info