Setting DTR high, RTS low using Linux bash?

user1447903 picture user1447903 · Jul 22, 2014 · Viewed 7.7k times · Source

I have a serial device that has no flow control, but is powered from the RS232 port by holding the RTS high and DTR low

I was hoping to read from this device using a simple bash script, but can't find any way to set the handshaking lines, using stty or otherwise, to allow for the above configuration.

Any ideas if this is possible?

Answer

Benoit-Pierre DEMAINE picture Benoit-Pierre DEMAINE · Oct 21, 2017

I don't have an answer about setting RTS without touching DTR, because I don't have any DTR pin on my dongle; but, trying to set RTS was already very tricky un pure shell.

You may need to play with stty crtscts and clocal flags.

I have published a detailed answer here: https://forums.gentoo.org/viewtopic-p-8132756.html#8132756

Here is the short version:

#!/bin/bash
MySerialPort="/dev/ttyUSB0"
MyLatency="2"
echo "#include <fcntl.h>
#include <sys/ioctl.h>
main()
{ int fd; fd = open(\"${MySerialPort}\",O_RDWR | O_NOCTTY );
int RTS_flag; RTS_flag = TIOCM_RTS;
ioctl(fd,TIOCMBIS,&RTS_flag);
sleep (${MyLatency});
ioctl(fd,TIOCMBIC,&RTS_flag);
close(fd); } " | tcc -run -

Note that sending data on TX will probably mess RTS; see the Gentoo forum for details.