Initializing/resetting serial port?

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
bowlofred
 
Posts: 31
Joined: Thu Jan 19, 2012 8:01 pm

Initializing/resetting serial port?

Post by bowlofred »

I'm trying to read some data from an UNO USB(serial port), but I'm having one hangup.

My perl code works great, unless the board gets reset.

After a reset, then the first time I use "putty" or the Arduino serial monitor to open the port and connect I can see the board resets again. But If I run my perl code first (which tries to open the serial port), then the board does not reset and my code gets no data.

What "magic" is putty and the serial port monitor doing to initialize serial comms that my perl code is not? Right now I have to use one of them to get things rolling. After that, the code works great.

I don't know that the problem is here, but this is the perl code I'm running:

Code: Select all

#!/perl64/bin/perl
use strict;
use warnings;
use Win32::SerialPort;
my $port = Win32::SerialPort->new("COM3")
  or die "New port failed. $!\n";

# 19200, 81N on the USB ftdi driver
$port->baudrate(9600); 
$port->databits(8); 
$port->parity("none");
$port->stopbits(1);

print "Sending newline\n";
$port->write("\012\015");

my $byte;
my $maxlines = 0;
my $newlines=0;
while(1)
{
$byte = $port->read(80);
  print $byte;
  $newlines += ($byte =~ tr/\n//);
  if ($newlines > $maxlines) { last; }
}
print "done.\n";

Thanks!

User avatar
adafruit_support_bill
 
Posts: 88087
Joined: Sat Feb 07, 2009 10:11 am

Re: Initializing/resetting serial port?

Post by adafruit_support_bill »

The serial DTR line will reset the Arduino if it is pulled low. The Serial Monitor in the IDE, (and most terminal emulators as well) do this upon opening of the COM port.

User avatar
baldengineer
 
Posts: 127
Joined: Sun Sep 26, 2010 11:49 pm

Re: Initializing/resetting serial port?

Post by baldengineer »

Try adding:

$port->dtr_active(1);

User avatar
bowlofred
 
Posts: 31
Joined: Thu Jan 19, 2012 8:01 pm

Re: Initializing/resetting serial port?

Post by bowlofred »

Ugh. Okay. So it's obviously a windows port initialization thing then.... I need to find where the magic lives.

Adding $port->rts_active(1) makes the script stop working. ->rts_active(0) has the same behavior as not having it (works after serial monitor has worked, not otherwise).

I can reset the arduino with ->pulse_dtr_on(500), but otherwise that doesn't seem to help me. Comm still fails after that step. (I tried several other values as well).

Thanks!

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”