Ian Gralinski
Ian Gralinski

Reputation: 109

Arduino doesn't listen to data sent via virtual serial port until Serial Monitor has been opened

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:

#!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 here.

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?

Upvotes: 0

Views: 1696

Answers (1)

Ian Gralinski
Ian Gralinski

Reputation: 109

Turns out I wasn't saving the serial port settings, as described in a thread on the Arduino Forum. Adding $PortObj->write_settings(); after setting the serial port parameters got rid of the problem. My final Perl code that worked was:

#!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";

Upvotes: 1

Related Questions