42

How do I set the baud speed for a specific tty (in this case a serial port)? I tried using

stty -F /dev/tty.iap ispeed 19200

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?

newenglander
  • 593
  • 2
  • 5
  • 7
  • Why the iphone tag? are you really using iPhone? – enzotib May 05 '12 at 13:41
  • 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. – newenglander May 05 '12 at 14:17
  • 1
    Try just leaving out the "ispeed" word. – Keith May 05 '12 at 21:19
  • @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;`. – newenglander May 05 '12 at 22:26
  • 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? :) – poige Oct 11 '12 at 21:47
  • @poige: 1) the verification was for me that i was able to recognize the input coming to on that port ;) 2) doesn't the `/private/etc/ttys` file store the settings for `stty`? – newenglander Oct 13 '12 at 19:15
  • @newenglander, well, I'd have verified that with `stty` itself — it could pour some light upon… 2) I dunno where `/private` corresponds to, but `/etc/ttys` is BSD-flavor's [thing][1] with SysV [counterpart /etc/inittab][2] (to some extent). [1]: http://www.freebsd.org/cgi/man.cgi?query=ttys&apropos=0&sektion=0&manpath=FreeBSD%209.0-RELEASE&arch=default&format=html [2]: http://www.freebsd.org/cgi/man.cgi?query=inittab&apropos=0&sektion=0&manpath=SuSE%20Linux/i386%2011.3&arch=default&format=html – poige Oct 13 '12 at 21:49

4 Answers4

47

I use the command stty -F /dev/ttyUSB0 9600.

Kotte
  • 2,467
  • 22
  • 26
  • 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` – Gabriel Staples Jan 30 '19 at 18:16
7

I use screen for this purpose

# screen /dev/ttyUSB0 38400

works like a charm

maxxvw
  • 308
  • 3
  • 8
  • 1
    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. – Gabriel Staples Jan 30 '19 at 18:15
  • 1
    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. – Oleg Rudenko Mar 08 '21 at 10:44
1

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

To edit the file:

sudo vipw -g

the file will look like this:

enter image description here

look for dialout group and add you username at the end, then logout/login from your session or reboot. enter image description here

symcbean
  • 5,008
  • 2
  • 25
  • 37
Federico
  • 11
  • 1
0

On my machine, neither stty -F /dev/ttyUSB0 9600 or screen /dev/ttyUSB0 9600 worked for me, but sudo stty speed 9600 was successful.