Go Down

Topic: Problems with serial communication using PuTTY and Win32::SerialPort for Perl (Read 2 times) previous topic - next topic


I've got a lot of elecrtonics and programming background, but I'm just getting started with Arduino tinkering.

I have been going crazy just trying to get Perl's Win32::SerialPort working with very trivial things, but I'll get to that in more detail in a moment.

First, the Arduino code:
Code: [Select]
static int LED = 13;
String inputString;
boolean stringComplete;

void setup() {
  inputString = "";
  stringComplete = false;
  // turn off the LED, we will use it for debug

  // start serial communication send a "ready" message
  Serial.println("Arduino ready");


void loop() {
  if (stringComplete) {

    // echo the string received
    Serial.print("ECHO : ");

    // reset the string
    inputString = "";
    stringComplete = false;

    // blink the LED as a sign that we got the command

void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    else {
      // add it to the inputString:
      inputString += inChar;

When I connect to the board using the built-in Serial Monitor that comes with the Arduino software, everything is great.  Every command I send causes the "TX" and "RX" LEDs to blink, and the LED attached to pin 13 also blinks as per the code.  Everything is echoed back to me as expected:
Code: [Select]
Arduino ready
ECHO : this works fine
ECHO : over and over
ECHO : every time

But then I tried using PuTTY to connect to the board.  I do get the "ready" prompt sent to the terminal, but the commands I try to send don't do anything, and nothing is echoed back to PuTTY. This is what prompted me to add the LED blinking as debug assistance, and I do not even see the LED blink.  The "TX" blinks, but "RX" and the pin 13 LED do nothing, so this would seem to imply that my commands are getting transmitted (TX), but not handled (pin13) or echoed (RX).

Code: [Select]
Arduino ready
I expect this to echo
try again

SO, that's question 1, I guess.  What's up with my PuTTY?

From there, things just get worse.  I have code that is trying to establish communication through win32 perl.  Here it is:
Code: [Select]
#! perl
use Win32::SerialPort;
$| = 1; #enable autoflush

# Constructor
my $com = "com5";
my $ob;
unless ($ob = new Win32::SerialPort($com))
    printf "! could not open port $com\n";
    exit 1;
    # next test would die at runtime without $ob

$ob->baudrate(9600)  || die "! bad baudrate";
$ob->parity("none")  || die "! bad parity";
$ob->databits(8)     || die "! bad databits";
$ob->stopbits(1)     || die "! bad stopbits";


my $sleep = 5;
print ": write_settings() done\n: sleeping $sleep second to let arduino get ready...\n";
sleep $sleep;

if($ob->write("test\n")) {
    print ": command sent\n";
} else {
    print "! failed to send command\n";

undef $ob;

When I try that script, it does set up the COM5 port properly, but my commands are not even reaching the board.  None of the LEDs blink and the actual ->write() call fails and causes the "!failed to send command" message to be printed out.  There are other hacked versions of my perl script lying around that I was trying to use to just read the "ready" command from the board, but that one was failing on the ->read() just like the above fails on the ->write().

Sort of at my wits end here, and I am sure this is something stupid that I missed after all my searching and reading.

Can anyone lend a hand with EITHER of these?


More info:

the issue with PuTTY seems to be related to it stripping off the "\n" on the commands I send or something.  I changed my Arduino code to look for a white space as the command terminator instead of the "\n" and now I can get echoing of commands back using PuTTY provided that I throw a white space after the command.  I'd be interesting in knowing why this bit of weirdness is happening, though.

My Perl attempts, however, are still dead in the water.  They can't even accomlish a write to the serial port at all.


I got PuTTY sorted out.  It was a matter of newline and linefeeds.  The built in serial monitor (and my Arduino code) was looking for newline to end a command, but putty was using linefeeds.

I've made the Arduino code smarter in that regards and I am able to use PuTTY with no issues.

Anway, the PuTTY was a sidetrack I went down.  My real concern and issue is with Perl.


More digging....

Perl thinks that the serial port is a modem and not an rs232.  The port is blocked waiting for a dial tone to be detected.

I'm looking through documentation about Win32API::CommPort and Win32:SerialPort, but I can't find anything that would allow me to set this.  There are only api calls that will querey "is_modem" and "is_rs232".

Go Up