Pages: [1]   Go Down
Author Topic: Arduino doesn't listen to data sent via virtual serial port until Serial Monitor  (Read 1293 times)
0 Members and 1 Guest are viewing this topic.
Melbourne
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


I have a BotBoarduino (a Duemilanove with a few extras) that I'm trying to communicate with using a Perl script. If I open the Serial Monitor from the Arduino IDE, I can send and receive data without a problem. Following this, my Perl script can communicate without any problems. However, if the Arduino is disconnected then reconnected to the PC, the Arduino doesn't seem to listen to commands sent from my Perl script until Serial Monitor is opened again.

I also tried using PuTTY to communicate with the Arduino, and this works the same way as Serial Monitor from the Arduino IDE - the Arduino doesn't listen to my Perl script until the connection has been opened once.

Here is a sample of the way I'm communicating with the Arduino using Perl:

Code:
#!perl -w

use Win32::SerialPort;

my $PortName = "COM4";
my $sendData = "c";

### SERIAL PORT SETUP ###
my $PortObj = new Win32::SerialPort($PortName) or die "Can't open $PortName: $^E\n";
$PortObj->baudrate(115200);
$PortObj->parity("none");
$PortObj->databits(8);
$PortObj->stopbits(1);
#$PortObj->dtr_active(1);
#$PortObj->rts_active(0);
#$PortObj->handshake("xoff");

$PortObj->lookclear();
$PortObj->write($sendData);

$PortObj->close();

I have commented out the dtr_active, rts_active and handshake bits. I played around with these settings as they were mentioned as possible culprits somewhere.

Also, I have used a 120 Ohm resistor to stop the Arduino from auto-resetting as described at http://playground.arduino.cc/Main/DisablingAutoResetOnSerialConnection.

Does anyone have any suggestions for the settings needed to get the Arduino to listen to my Perl program without having to open PuTTY/Serial Monitor first?
« Last Edit: January 17, 2013, 08:24:20 pm by m0nk3y » Logged

0
Offline Offline
God Member
*****
Karma: 39
Posts: 988
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You are probably being bit by the "bootloader delay eats serial input" bug.  Delay two seconds in your perl script after opening the serial port before writing to it and your script will probably be happy.

When your perl script opens the port, the Arduino resets.  During the reset, the bootloader is in control, listening for the command to start an upload.  When you send data to the serial port right after opening the port, it ends up being ignored by the bootloader because your program isn't running yet.

Best practice would be to listen for a string from the Arduino to let you know your program is up and going.

-br
Logged

Melbourne
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks billroy, I'll give your suggestion a go later today and post the results.
Logged

Melbourne
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I added a 5 second delay (to be safe), but the Arduino still doesn't seem to listen to my Perl program. I have the auto-reset disabled, so I don't think the bootloader is started again when I open the Serial port.

Any other suggestions for what I could try?
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 615
Posts: 49426
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The Serial Monitor application sets all the right settings for opening the serial port. Apparently, your perl script does not. When the Serial Monitor corrects the missing settings, communications happens.

There have been numerous threads about how to use perl to write to the serial port. I know that at least one of them touched on exactly what was missing from the usual attempts to open the serial port. Since I don't use other than Windows, and the problem you are having can not be reproduced on Windows (since, on Windows, two applications can not open the same serial port), I didn't bookmark the thread. But, you should be able to search for it.
Logged

Melbourne
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

My problem was that I was not saving the serial port settings in my Perl script. I did indeed find this suggestion in an older thread after doing an exhaustive search (http://arduino.cc/forum/index.php/topic,21051.0.html). It was on the last page of search results, which I don't think I got up to the first time I searched the forums for a solution. Thanks for the suggestion PaulS and kicking my butt into gear to search for longer.

Here is my short Perl test script that works without having to open Serial Monitor/PuTTY:

Code:
#!perl -w

use Win32::SerialPort;
use strict;
use warnings;

$| = 1; #enable autoflush

my $PortName = "COM4";
my $sendData = "o";

### SERIAL PORT SETUP ###
my $PortObj = new Win32::SerialPort($PortName) or die "Can't open $PortName: $^E\n";
$PortObj->baudrate(57600);
$PortObj->parity("none");
$PortObj->databits(8);
$PortObj->stopbits(1);
$PortObj->write_settings(); #very important!

$PortObj->write($sendData);

$PortObj->close() || warn "\nClose failed\n";

Hopefully this will be of use to someone - maybe me in a few years time when I forget what I did.
Logged

Pages: [1]   Go Up
Jump to: