Pages: [1]   Go Down
Author Topic: Simple Perl code for interfacing with Arduino  (Read 612 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 1
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi All,

I did a quick search on the web site and the forum and found that there is not much code on how to interface Arduino with Perl. The only sensible thread that I found linking to an external site was using a Perl module to do the interfacing. I managed to get it working without using any additional Perl modules, just using the Linux TTY at the beginning. I also did a bit of a workaround to make sure that the autoloading of the NG does not get triggered when you do the communication. Here is some simple code to write to the Arduino.

Code:
#!/usr/bin/perl

$|=1; # to ensure that there is no buffering delaying the outputs

#Initialize the port using the Linux TTY code. Change the 19200 to whatever baud rate that you are using in Serial.begin()
#Also change /dev/ttyUSB0 to whatever the Arduino is connected to.
`stty -F /dev/ttyUSB0 cs8 19200 ignbrk -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts`;

#open the port for output
open OUT, ">/dev/ttyUSB0" or die "Couldn't open usb device";

#use bin mode on the file if you will be sending pure binary data
#binmode OUT;

#delay for about 1 second to avoid the Arduino NG auto loading getting triggered
sleep(1);

# send output to the Arduino
print OUT "Hello World";


I will post one on how to read using Perl once I get the code cleaned up a bit. For the moment I just use
Code:
$tail -f /dev/ttyUSB0
to read the output coming from the Arduino.

Enjoy.
Logged

Australia
Offline Offline
Jr. Member
**
Karma: 0
Posts: 63
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

stty raw is your friend

Also, if you stty -hup, it shouldn't reset next time the port is opened.

Check out http://hc11-ide.funkmunch.net/bart-v3/sender.pl for an example of a perl serial I/O utility I use for a different microcontroller.
Logged

Pages: [1]   Go Up
Jump to: