redirection of stdout to serial port

People tend to use serproxy, python, or ruby scripts to handle serial comms with /dev/usbserial-A800eyBK rather than redirecting from stdout, even for simple unidirectional output.

For example, if I want to use a bash script to send the letter 'A' to down the serial port to an awaiting Arduino, I might be inclined to do this:

#!/bin/bash
echo A > /dev/usbserial-A800eyBK

Presumably, this is impossible because per RS-232 spec, RTS/CTS handshaking has to be set up to handle the data, but I can't find documentation of that anywhere. Can anyone give me confirmation of this or a simple explanation of the reason that shell redirection to /dev/usbserial- does not work?

-Brock

Putting aside whether or not that actual command would work, the comms with the Arduino does not use RTS/CTS because it only uses two wires (Rx/Tx). Thus it uses "no" handshaking. Generally you have three handshaking options with RS232:

  • Hardware (RTS/CTS)
  • Software (Ctrl/S ... Ctrl/P)
  • None

My test (on a Mac) with the logic analyzer shows that this does in fact send an "A" to the Arduino:

echo A > /dev/cu.usbmodemfd1321

Further testing shows there is still a problem.

Whilst the “A” is sent to the Arduino using the command above, that causes it to reset. During the reset process the line is lost while it “reboots”.

There is some discussion about it here:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1234284114

So you don’t really have a problem with sending the data, you have a problem with receiving it. On my Mac the suggested fix of doing this didn’t work:

stty -f /dev/cu.usbmodemfd1321 -hupcl

After more testing what does work is this:

  • Upload your sketch
  • Start the Arduino serial monitor (this will reset it)
  • Now, doing this works:
echo Hi there > /dev/cu.usbmodemfd1321

My example code is this:

#include <Wire.h>
#include <LiquidCrystal.h>

// Connect via i2c
LiquidCrystal lcd(0);

void setup()
{
  // start serial port at 9600 bps:
  Serial.begin(9600);
  // set up the LCD's number of rows and columns: 
  lcd.begin(16, 2);
}

void loop()
{
  if (Serial.available() > 0) {
    char c = Serial.read();
    lcd.print (c);
  }
}

Once you have loaded your program in the arduino you can get a 100 ohm resistor and stick one end in the reset pin hole and the other end in the +5v pin hole. This will keep the arduino from being reset when the the serial port is opened.

Nick, starting the console after oading won’t work for me because I am using a script to send the command
echo A > hi there /usbserial.HEXADDY and thus, not loading the console.

I’m going to try out zoomkat’s suggestion to prevent reset and see how that goes.

-B