Set RTS On Close equivalent for Ubuntu Linux?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Set RTS On Close equivalent for Ubuntu Linux?

by timv on Wed Dec 26, 2007 3:55 pm

I can't get into my office this week, so I brought my Boarduino home. I have an icky XP box in my office and use the FTDI USB/Serial cable, and I had to do the "Set RTS On Close" thing, described here:

Without that, I can upload one sketch successfully, but the second time I try it Arduino says "programmer is not responding." (I forgot to re-enable it after the IT guys installed a new disk image, and that's why I remember so well what happens without it.)

I use Ubuntu at home and I'm getting exactly the same behavior: works the first time, fails every time after that. Unplug and replug the USB cable and it's all cool again, but just for one time.

I'm gonna look around and see if I can figure it out, and post a fix if I find it. But if someone knows how to do the equivalent under Linux, or can even tell me a little more about what this switch does and why it's needed, that would be a huge help!!

Posts: 76
Joined: Thu Nov 01, 2007 4:13 pm

by timv on Fri Dec 28, 2007 9:01 pm

Whew, that was a thrash, but I finally found a fix for it.

It turned out that the Arduino application opens the serial port for some reason after it calls Avrdude, and it was leaving DTR high when it closed the port.

Avrdude works the first time because DTR is low when it opens the port, and so raising DTR generates a reset on the Boarduino. But every subsequent time I tried to upload code, DTR was still high, so trying to raise it from within Avrdude had no effect and the chip didn't get reset.

The solution I went with was to download source for the RXTXComm package that Arduino uses:


I modded the file src/SerialImp.c, added the termios "HUPCL" flag in the configure_port() function, which will clear DTR (apparently the same as setting RTS???) when the port is closed:
Code: Select all | TOGGLE FULL SIZE
--- src/SerialImp.c.orig   2006-01-29 17:19:04.000000000 -0500
+++ src/SerialImp.c   2007-12-28 17:45:11.000000000 -0500
@@ -380,7 +380,8 @@
    ttyset.c_iflag = INPCK;
    ttyset.c_lflag = 0;
    ttyset.c_oflag = 0;
-   ttyset.c_cflag = CREAD | CS8 | CLOCAL;
+// timv 122807 : clear DTR on close
+   ttyset.c_cflag = CREAD | CS8 | CLOCAL | HUPCL;
    ttyset.c_cc[ VMIN ] = 0;
    ttyset.c_cc[ VTIME ] = 0;

I ran "./configure" and "make" and then installed the modified native code library in Arduino's "lib" directory. I also copied the rebuilt .jar file just for superstition I guess:
Code: Select all | TOGGLE FULL SIZE
cp RXTXcomm.jar ../arduino-0010/lib
cp i686-pc-linux-gnu/.libs/librxtxSerial.so ../arduino-0010/lib

I don't know where the serial port is getting re-opened in the Arduino app software. I couldn't find a downloadable copy of the source for it anywhere.

Is it just me or is that berlios.de site a total train wreck? There didn't seem to be any function to search the source, and Google didn't have any hits for
it either. SVN locked up dead halfway through an update operation when I tried to get the code that way. And I kept getting ominous messages from
my browser about attempts to access sites like "koder.com" with invalid certificates.

Has that site been kracked or what? And would it kill them to tar up a snapshot of the source for each release and post it along with the binaries, like decent folks do? </rant>

I also made a similar change to ser_posix.c in the avrdude-5.4-arduino-0010-src package:
Code: Select all | TOGGLE FULL SIZE
--- box/avrdude-5.4-arduino-src/ser_posix.c   2007-07-18 16:24:11.000000000 -0400
+++ avrdude-5.4-arduino-src/ser_posix.c   2007-12-28 05:56:23.000000000 -0500
@@ -114,7 +114,9 @@
   termios.c_iflag = IGNBRK;
   termios.c_oflag = 0;
   termios.c_lflag = 0;
-  termios.c_cflag = (CS8 | CREAD | CLOCAL);
+// timv 12/28/07
+//  termios.c_cflag = (CS8 | CREAD | CLOCAL );
+  termios.c_cflag = (CS8 | CREAD | CLOCAL | HUPCL );
   termios.c_cc[VMIN]  = 1;
   termios.c_cc[VTIME] = 0;

I built the Avrdude binary and copied it to Arduino's hardware/tools directory.

I actually tried that change first, and it almost solved the problem. It would fail the first time after Arduino had closed the port with DTR set, but worked the next time--and kept working if I kept running Avrdude from the command line.

But from within the Arduino app, it worked every other time, since Arduino apparently only messes with the port when Avrdude succeeds in programming the chip.

So that Avrdude change isn't really necessary as long as the Arduino app behaves itself. But I'm leaving it in because that seems like the right way for things to work.

One more thing: Somewhere towards the end of this adventure, I figured out that I could hook a test clip onto the serial header underneath the cord's connector, and use an LED to watch the status of that line. Here's a pic taken with my new junky old camera, which works a whole lot better than my old junky old camera:

The flash washed out the LED but it's actually on in this photo. Turns out that DTR low is logic high, but so is the AVR's Reset pin so that's how it should be when the serial port is closed. It would have save me a lot of swearing and head-scratching if I had thought to do this earlier in the process. But I fixed the problem and it's the tits now.
Posts: 76
Joined: Thu Nov 01, 2007 4:13 pm

Linux with FTDI cable

by pbadger on Tue Jun 10, 2008 11:06 am


I'm still hazy with what you've done to fix this. Is it possible to get you to post your entire code fix here, or maybe better, post it to the software bugs topic on the Arduino site?

Paul Badger
pbadger splice verizon splice net

Posts: 3
Joined: Thu Jan 10, 2008 1:08 am

by timv on Tue Jun 10, 2008 2:50 pm

HI Paul. The changes aren't major. There are just two of them.

In src/SerialImp.c in the RXTXComm package, I changed the line
Code: Select all | TOGGLE FULL SIZE
ttyset.c_cflag = CREAD | CS8 | CLOCAL;

to be
Code: Select all | TOGGLE FULL SIZE
ttyset.c_cflag = CREAD | CS8 | CLOCAL | HUPCL;

That's line 303.

And in ser_posix.c in the Avrdude source, I changed
Code: Select all | TOGGLE FULL SIZE
termios.c_cflag = (CS8 | CREAD | CLOCAL );

to be
Code: Select all | TOGGLE FULL SIZE
termios.c_cflag = (CS8 | CREAD | CLOCAL | HUPCL );

That's line 117.

That's all I did. The rest of what I wrote is about why, and about rebuilding the apps, and ranting about a website that didn't work very well, and other stuff.

I made the same change in both places, just adding the "HUPCL" flag. This has the effect of automatically resetting the Boarduino when the serial port is closed so that it works the same way as the Windows Arduino app.
Posts: 76
Joined: Thu Nov 01, 2007 4:13 pm

Please be positive and constructive with your questions and comments.