|
|
(7 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt) |
Zeile 1: |
Zeile 1: |
− | {{BenutzerSeitenNichtVeraendernWarnung|rdiez}}
| + | #REDIRECT [[Benutzer:Rdiez/PageRemoved]] |
− | | + | |
− | = Serial Port Tips for Linux =
| + | |
− | | + | |
− | == Addressing a Serial Port ==
| + | |
− | | + | |
− | Built-in serial ports tend to get the same device filename everytime, like ''/dev/ttyS0'' for the first one, ''/dev/ttyS1'' for the second one, and so on.
| + | |
− | | + | |
− | However, USB virtual serial ports get a different filename each time, depending on the kind of device and the order with which the USB ports are connected to the system. Typically, such device filenames look like ''/dev/ttyACM0'' or ''/dev/ttyUSB0'', but such names are often unpredictable.
| + | |
− | | + | |
− | You have several options:
| + | |
− | | + | |
− | * Manually inspect the ''tty*'' files under ''/dev'', in order to find out which one gets created when a particular USB device is connected.
| + | |
− | * Use a filename under ''/dev/serial/by-id'' or ''/dev/serial/by-path''.
| + | |
− | * Use a script in order to find a USB serial port filename by manufacturer name, serial number, etc.
| + | |
− | * Write a UDEV rules file that assigns a fixed filename to your particular USB device.
| + | |
− | | + | |
− | == Configuring a Serial Port under Linux ==
| + | |
− | | + | |
− | Set the default configuration with stty to 9600 bps, 8N1, no flow control:
| + | |
− | | + | |
− | stty -F /dev/serial_port cs8 -parenb -cstopb -clocal raw speed 9600
| + | |
− |
| + | |
− | # What the arguments mean:
| + | |
− | # cs8: 8 data bits
| + | |
− | # -parenb: No parity (because of the '-')
| + | |
− | # -cstopb: 1 stop bit (because of the '-')
| + | |
− | # -clocal: Disable modem control signals
| + | |
− | | + | |
− | == Opening a Serial Port ==
| + | |
− | | + | |
− | Say you have the following scenario:
| + | |
− | * Your device reacts to single keypresses. <br/> Therefore, you do not want line editing on the local side, because characters need to be sent straight away. <br/> An example device would be the [http://en.wikipedia.org/wiki/Bus_Pirate Bus Pirate].
| + | |
− | * You want to use a standard text console.
| + | |
− | * You want to terminate the connection when you press ''Ctrl+C''.
| + | |
− | | + | |
− | You can connect to your device with the following command:
| + | |
− | | + | |
− | socat -t0 STDIO,raw,echo=0,escape=0x03 /dev/mydevice,b115200,cs8,parenb=0,cstopb=0,clocal=0,raw,echo=0,setlk,flock-ex-nb
| + | |
− | | + | |
− | == No exclusive lock when accessing serial ports ==
| + | |
− | | + | |
− | I was surprised to learn that several Linux processes can open a given serial port at the same time. This usually makes a mess of the data. If you make a mistake and try to use the same port from several concurrent scripts, it may take a while to figure out why the data is getting chopped and mixed up in random ways.
| + | |
− | | + | |
− | There is no exclusive lock when opening serial port files, like there is under Windows. As far as I know, there is no way to enforce such a protection against concurrent access at system level. If you do know how to achieve that, please [[Benutzer:rdiez|drop me a line]]!
| + | |
− | | + | |
− | There is a work-around if all processes cooperate. Tool ''soca''t implements arguments ''setlk'', which uses "fcntl(fd, F_SETLK, ...)", and argument ''flock-ex-nb'', which uses "flock(fd, LOCK_EX|LOCK_NB)". Interestingly enough, those two file-locking mechanisms seem to be independent from each other, at least under Linux. If a process uses one method, then other processes that use the same method will realise that the file is already locked. However, processes that do not use any locking method will not be affected at all.
| + | |