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

Perl with Arduino

Status
Not open for further replies.

AMiSM

Technical User
Jan 26, 2006
128
US
Hi, folks!
I've been searching and reading and racking my brain, but this is just eating my lunch! It's vitally important that I learn this. It seems like one of those things you easily over-think, then it comes to you in a flash of insight.

I need to communicate with my Arduino microcontroller board via the USB serial connection. I have gotten things to work, kinda, but it's acting weird.

This is the Perl side:
use Win32::SerialPort;

$|=1;

my $port = Win32::SerialPort->new("COM4");
$port->databits(8);
$port->baudrate(19200);
$port->parity("none");
$port->stopbits(1);

$port->lookclear();

while (1) {
for $r (0..9) {
print $r . "\n";
$port->write($r."\n");
for (0..1000000){}
}
}

I know the second 'for' is vulgar, but I didn't want to mess with one second intervals.

This is the Arduino side:
int incomingByte;

void
setup ()
{
beginSerial (19200);
}


void
loop ()
{
if (Serial.available () > 0) {
incomingByte = Serial.read ();
analogWrite(9,int(incomingByte));
}
}


An LED on pin 9 just flickers, where is should get steadily bright, suddenly dark, then steadily bright, over and over.
I got this example to work:

...but when I tried to modify it, I only got gobbledygook back out. It's almost like the data types are getting confused, or something. I think the two might be related, but it defies my current understanding.

Another problem, is how do you poll the serial port in Perl without getting locked down until the buffer fills up?

Anybody have any experience with this? I can't do this part on my own without a good book in front of me.
 
On the Perl side,

for $r (0..9) {

...actually goes to 255. I just tried this because I thought the Arduino was only getting the first digit, somehow.
 
Is there some code here you did not post?

for (0..1000000){}

that will do nothing unless you have something inside the block for perl to evaluate.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Code:
        for (0..1000000){}
    }
}

  I know the second 'for' is vulgar, but I didn't want to mess with one second intervals.

I think he did that to slow Perl down a bit. Was sleep(1) too long of a delay? You can use select() to do a delay of shorter time and it's easy and doesn't require loading any modules.

Code:
select (undef,undef,undef,0.01); # 0.01 of a second

-------------
Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Yeah, I forgot about select().
 
If you want to sleep for less than one second, you could also take a look at the usleep function in Time::HiRes.
 
Actually, I don't want to sleep at all. I just want to get integers to my Arduino, but that doesn't seem to be happening.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top