Serial communication with Windows and and Uno.

I'm looking to pull some data off an Uno and I have it almost working. But there's one thing I don't understand.

I'm pulling the data on the windows side with perl and Win32::SeriaPort. But before the script works, I must start serial communications with the board via either the Arduino serial monitor or a terminal emulator like Putty. If I don't do that, I don't get anything back.

I can toggle DTR and make the arduino reset (just like the other programs do automatically), but that doesn't make the serial communication work. So something else is happening when the serial monitor runs that my program isn't doing. Anyone know what that "magic" is?

Here's the bulk of what I'm doing on the windows side. If I run it after the serial monitor has been opened one time, it returns data sent from the arduino. If I run it before then, it hangs after printing "sent newline".

Any suggestions? Thanks!

use strict;
use warnings;
use Win32::SerialPort;
my $port = Win32::SerialPort->new("COM3")
  or die "New port failed. $!\n";
$port->baudrate(9600); # you may change this value
$port->databits(8); # but not this and the two following
$port->parity("none");
$port->stopbits(1);

unless ($port->write_settings)
{
  print "Serial port settings not ready. $^E\n";
  exit;
}

#$port->pulse_dtr_on(500); # resets the arduino, but doesn't make anything work better.
print "Sending newline\n";
$port->write("\012\015");
print "Sent newline\n";

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

please post the arduino code too.

Original code is complex. Here’s something that reproduces it:

//Echo info
// Detect a newline and print some text
#include <stdio.h>

char input[100];
int inputPos;
int current;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  if (!Serial.available())
  {
    return;
  }
  current = Serial.read();
  if (current != '\015')
  {
    input[inputPos] = current;
    inputPos++;
  }
  else
  {
    input[inputPos]='\0';
    Serial.println("Got newline");
    inputPos=0;
  }
  //Serial.println(inputPos);
  //delay(1000);
}

Immediately after uploading this sketch to the board, running my perl code always prints “sent newline” and then stops. I have to ^C it. If I then open and close the serial monitor in the IDE, the program then will communicate. It prints the “Got newline” from the arduino and exits.

So what did opening the serial monitor do that I need to do on the computer side?

I am experiencing this exact same problem bowlofred reported with Windows and my Arduino. I'm using PHP to open the port (COM6), which works fine; it writes to the Arduino fine, but when reading back from the Arduino, the code hangs. Then if I open the Serial Monitor and then close it, problem is solved PHP reads from Arduino fine. I haven't tried putty yet.

I was curious if this also occurred on my Ubuntu box, but that is not the case. I do have the Arduino IDE installed, but not running. Plugging the Arduino in and running the code works very nicely on Ubuntu, although I did notice that when the code opens the serial port, it invokes a reset on the Arduino (this is not the case on Windows).

Still curious if anyone has also encountered this along with a fix?

Then if I open the Serial Monitor and then close it, problem is solved PHP reads from Arduino fine.

PHP is not designed to read from the serial port on windows.