- Best way to set serial port speeds on boot?
- 3 Answers 3
- Linux stty command
- Description
- Syntax
- Options
- Special characters
- Special settings
- Control settings
- Input settings
- Output settings
- Local settings
- Combination settings
- Examples
- set baud speed for tty
- 4 Answers 4
- You must log in to answer this question.
- Linked
- Related
- Hot Network Questions
- Subscribe to RSS
Best way to set serial port speeds on boot?
I’ve got instruments hooked up to /dev/ttyS1 through ttyS9 on a machine running Debian Wheezy. The various instruments communicate at various speeds. Is there a better way to set the baud rates than to have a bunch of lines like
stty -F /dev/ttyS1 19200 raw -echo stty -F /dev/ttyS2 4800 raw -echo etc.
The kernel drivers might understand some kernel command line parameters, which you could use by default. Depends on the hardware (read driver), I guess.
@peterph yeah, but /etc/rc.local seems way more elegant than to screw with the kernel command line. I agree with slm, /etc/rc.local is fine.
3 Answers 3
Install the setserial package. It can save serial port settings and restore them at boot time.
Alternatively, run a program when the port is detected via an udev rule. Put something like this (untested) in /etc/udev/rules.d/zzz_serial.rules :
KERNEL=="ttyS1", RUN+="/bin/stty -F /dev/ttyS1 19200 raw -echo"
When using RUN commands, you need to specify the full path, in this case /bin/stty. Otherwise you’ll end up with : failed to execute ‘/lib/udev/stty’ No such file or directory.
The /etc/rc.local file is the best location for creating customization that are specific to the box. It was specifically created for these types of custom changes and is the first place that most system administrators are conditioned to look when dealing with Unix/Linux boxes.
One might also create a separate script for that, that will be sourced/executed from within /etc/rc.local
@peterph — excellent point, yes if it’s more elaborate, you might want to source a script with the contents of these commands in them instead.
The setserial package will restore the serial settings at boot time. Once installed, simply edit the file /var/lib/setserial/autoserial.conf
###PORT STATE GENERATED USING AUTOSAVE-ONCE### ###AUTOSAVE-ONCE### # # If you want to configure this file by hand, use # dpkg-reconfigure setserial # and change the configuration mode of the file to MANUAL. # If you do not do this, this file may be overwritten automatically the next # time you upgrade the package # /dev/ttyS0 uart 16550A port 0x03f8 irq 4 baud_base 115200 spd_normal skip_test /dev/ttyS1 uart 16550A port 0x02f8 irq 3 baud_base 115200 spd_normal skip_test /dev/ttyS2 uart 16550A port 0x03e8 irq 0 baud_base 115200 spd_normal skip_test /dev/ttyS3 uart 16550A port 0x02e8 irq 0 baud_base 115200 spd_normal skip_test /dev/ttyS4 uart 16550A port 0x02d0 irq 0 baud_base 115200 spd_normal skip_test /dev/ttyS5 uart 16550A port 0x02c0 irq 0 baud_base 115200 spd_normal skip_test
From my experience, modifying the file directly is ok, even though the comments in the file say otherwise. You can also use setserial ‘interactively’, by moving the line ###AUTOSAVE-ONCE### at the top of the file. Then calling setserial as many times as needed until you’re satisfied with the configuration. Rebooting the machine at that point will save the latest configuration into /setserial/autoserial.conf .
Linux stty command
On Unix-like operating systems, the stty command changes and prints terminal line settings.
This page covers the GNU/Linux version of stty.
- Description
- Syntax
- Special characters
- Special settings
- Control settings
- Input settings
- Output settings
- Local settings
- Combination settings
- Examples
- Linux commands help
Description
stty displays or changes the characteristics of the terminal.
Syntax
stty [-F DEVICE | --file=DEVICE] [SETTING].
stty [-F DEVICE | --file=DEVICE] [-a|--all]
stty [-F DEVICE | --file=DEVICE] [-g|--save]
Options
-a, —all | Print all current settings in human-readable form. |
-g, —save | Print all current settings in a stty-readable form. |
-F, —file=DEVICE | Open and use the specified DEVICE instead of stdin. |
—help | Display a help message, and exit. |
—version | Output version information, and exit. |
Special characters
* dsusp CHAR | CHAR sends a terminal stop signal once input flushed. |
eof CHAR | CHAR sends an end of file (terminate the input). |
eol CHAR | CHAR ends the line. |
* eol2 CHAR | Alternate CHAR for ending the line. |
erase CHAR | CHAR erases the last character typed. |
intr CHAR | CHAR sends an interrupt signal. |
kill CHAR | CHAR erases the current line. |
* lnext CHAR | CHAR enters the next character quoted. |
quit CHAR | CHAR sends a quit signal. |
* rprnt CHAR | CHAR redraws the current line. |
start CHAR | CHAR restarts the output after stopping it. |
stop CHAR | CHAR stops the output. |
susp CHAR | CHAR sends a terminal stop signal. |
* swtch CHAR | CHAR switches to a different shell layer. |
* werase CHAR | CHAR erases the last word typed. |
Special settings
N | Set the input and output speeds to N bauds. |
* cols N | Tell the kernel that the terminal has N columns. |
* columns N | Same as cols N. |
ispeed N | Set the input speed to N. |
* line N | Use line discipline N. |
min N | With -icanon, set N characters minimum for a completed read. |
ospeed N | Set the output speed to N. |
* rows N | Tell the kernel that the terminal has N rows. |
* size | Print the number of rows and columns according to the kernel. |
speed | Print the terminal speed. |
time N | With -icanon, set read timeout of N tenths of a second. |
Control settings
[—]clocal | Disable modem control signals. |
[—]cread | Allow input to be received. |
* [—]crtscts | Enable RTS/CTS handshaking. |
csN | Set character size to N bits, N is [5..8]. |
[—]cstopb | Use two stop bits per character (one with ‘—‘). |
[—]hup | Send a hangup signal when the last process closes the tty. |
[—]hupcl | Same as [—]hup. |
[—]parenb | Generate parity bit in output and expect parity bit in input. |
[—]parodd | Set odd parity (or even parity with ‘—‘). |
Input settings
[—]brkint | Breaks cause an interrupt signal. |
[—]icrnl | Translate carriage return to newline. |
[—]ignbrk | Ignore break characters. |
[—]igncr | Ignore carriage return. |
[—]ignpar | Ignore characters with parity errors. |
* [—]imaxbel | Beep and do not flush a full input buffer on a character. |
[—]inlcr | Translate newline to carriage return. |
[—]inpck | Enable input parity checking. |
[—]istrip | Clear high (8th) bit of input characters. |
* [—]iutf8 | Assume input characters are UTF-8 encoded. |
* [—]iuclc | Translate uppercase characters to lowercase. |
* [—]ixany | Let any character restart output, not only start character. |
[—]ixoff | Enable sending of start/stop characters. |
[—]ixon | Enable XON/XOFF flow control. |
[—]parmrk | Mark parity errors (with a 255-0-character sequence). |
[—]tandem | Same as [—]ixoff. |
Output settings
* bsN | Backspace delay style, N is [0..1]. |
* crN | Carriage return delay style, N in [0..3]. |
* ffN | Form feed delay style, N in [0..1]. |
* nlN | Newline delay style, N in [0..1]. |
* [—]ocrnl | Translate carriage return to newline. |
* [—]ofdel | Use delete characters for fill instead of null characters. |
* [—]ofill | Use fill (padding) characters instead of timing for delays. |
* [—]olcuc | Translate lowercase characters to uppercase. |
* [—]onlcr | Translate newline to carriage return-newline. |
* [—]onlret | Newline performs a carriage return. |
* [—]onocr | Do not print carriage returns in the first column. |
[—]opost | Post-process output. |
* tabN | Horizontal tab delay style, N in [0..3]. |
* tabs | Same as tab0. |
* -tabs | Same as tab3. |
* vtN | Vertical tab delay style, N in [0..1] |
Local settings
[—]crterase | Echo erase characters as backspace-space-backspace. |
* crtkill | Kill all line by obeying the echoprt and echoe settings. |
* -crtkill | Kill all line by obeying the echoctl and echok settings. |
* [—]ctlecho | Echo control characters in hat notation (‘^c‘). |
[—]echo | Echo input characters. |
* [—]echoctl | Same as [—]ctlecho. |
[—]echoe | Same as [—]crterase. |
[—]echok | Echo a newline after a kill character. |
* [—]echoke | Same as [—]crtkill. |
[—]echonl | Echo newline even if not echoing other characters. |
* [—]echoprt | Echo erased characters backward, between ‘\‘ and ‘/‘. |
[—]icanon | Enable erase, kill, werase, and rprnt special characters. |
[—]iexten | Enable non-POSIX special characters. |
[—]isig | Enable interrupt, quit, and suspend special characters. |
[—]noflsh | Disable flushing after interrupt and quit special characters. |
* [—]prterase | Same as [—]echoprt. |
* [—]tostop | Stop background jobs that try to write to the terminal. |
* [—]xcase | With icanon, escape with ‘\‘ for uppercase characters. |
Combination settings
* [—]LCASE | Same as [—]lcase. |
cbreak | Same as -icanon. |
-cbreak | Same as icanon. |
cooked | Same as brkint ignpar istrip icrnl ixon opost isig icanon: eof and eol characters to their default values |
-cooked | Same as raw. |
crt | Same as echoe echoctl echoke. |
dec | Same as echoe echoctl echoke -ixany intr ^c erase 0177 kill ^u. |
* [—]decctlq | Same as [—]ixany. |
ek | Erase and kill characters to their default values |
evenp | Same as parenb -parodd cs7. |
-evenp | Same as -parenb cs8. |
* [—]lcase | Same as xcase iuclc olcuc. |
litout | Same as -parenb -istrip -opost cs8. |
-litout | Same as parenb istrip opost cs7. |
nl | Same as -icrnl -onlcr. |
-nl | Same as icrnl -inlcr -igncr onlcr -ocrnl -onlret. |
oddp | Same as parenb parodd cs7. |
-oddp | Same as -parenb cs8. |
[—]parity | Same as [—]evenp. |
pass8 | Same as -parenb -istrip cs8. |
-pass8 | Same as parenb istrip cs7. |
raw | Same as -ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl -ixon -ixoff -iuclc -ixany -imaxbel -opost -isig -icanon -xcase min 1 time 0. |
-raw | Same as cooked. |
sane | Same as cread -ignbrk brkint -inlcr -igncr icrnl -iutf8 -ixoff -iuclc -ixany imaxbel opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0 isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke, all special characters to their default values. |
Handle the tty line connected to standard input. Without arguments, it prints baud rate, line discipline, and deviations from stty sane. In settings, CHAR is taken literally, or coded as in ^c, 0x37, 0177 or 127; special values ^- or undef are used to disable special characters.
Examples
Reset all terminal settings to «sane» values; this has the effect of «fixing» the terminal when another program alters the terminal settings to an unusable condition.
Disable echoing of terminal input.
Re-enable echoing of terminal input.
Display all current terminal settings.
set baud speed for tty
But get the error Invalid argument for every speed I try (2400, 4800, etc.) except 9600 (the default). I can successfully change the speed in a C program using cfsetspeed . Do I need to make a change somewhere else, like the ttys file?
well actually a jailbroken ipod touch, basically the same. (iap is ipod access protocol.) I figured this question would fit here better than the apple stackexchange site. It is a Unix/Linux question, have the same problem on my mac.
@Keith, then I don’t get an error message, but I run stty -F /dev/tty.iap to get details, it still says speed 9600 baud; .
1) «I can successfully change the speed in a C program using cfsetspeed» — have you verified it with stty then? 2) «like the ttys file?» — what ttys file exactly? 🙂
4 Answers 4
I use the command stty -F /dev/ttyUSB0 9600 .
Perfect. Now I can echo commands out over serial, at the correct baud rate, as follows: echo -ne ‘my message to send over serial\n’ > /dev/ttyUSB0
I use screen for this purpose
I think the goal here is to set the baud rate for the device so you can then echo commands straight over serial. Ex: echo -ne ‘my message to send over serial\n’ > /dev/ttyUSB0 . I think screen is great too, but @Kotte has the correct answer on how to do what is being asked.
Yes, Kotte has correct answer for the question. Still often the problem is not just in baud rate, but also in other parameters. In my case setting baud rate with stty was not enough, but screen /dev/ttyUSB0 115200 has automagically configured all parameters and it just worked.
This may be a permissions issue.
/dev/ttyUSB0 is an IO port, to avoid using sudo or root you should add your user to the group which owns the device. You can check that with, e.g.
$ ls /dev/ttyS0 crw-rw---- 1 root dialout 4, 64 Mar 29 09:12 /dev/ttyS0
the file will look like this:
look for dialout group and add you username at the end, then logout/login from your session or reboot.
On my machine, neither stty -F /dev/ttyUSB0 9600 or screen /dev/ttyUSB0 9600 worked for me, but sudo stty speed 9600 was successful.
You must log in to answer this question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.13.43531
Linux is a registered trademark of Linus Torvalds. UNIX is a registered trademark of The Open Group.
This site is not affiliated with Linus Torvalds or The Open Group in any way.
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.