Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting DTR high, RTS low using Linux bash?

Tags:

stty

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?

like image 889
user1447903 Avatar asked Dec 07 '25 03:12

user1447903


2 Answers

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.

like image 67
Benoit-Pierre DEMAINE Avatar answered Dec 12 '25 20:12

Benoit-Pierre DEMAINE


socat controls DTR when hucpcl=1

(Atmel EDBG USB Serial needs DTR high)

sleep 1; while true; do echo -en "\x01\x02"; sleep 0.1; done | socat -T1 -t1 - /dev/ttyUSB0,hupcl=1,raw,b1000000,cs8,echo=0

man socat - may be find more solutions for your serial problem.

like image 22
jkl Avatar answered Dec 12 '25 19:12

jkl