Benutzer:Rdiez/SerialPortTipsForLinux: Unterschied zwischen den Versionen

Aus /dev/tal
Wechseln zu: Navigation, Suche
(Weiterleitung nach Benutzer:Rdiez/PageRemoved erstellt)
 
(Eine dazwischenliegende Version desselben Benutzers wird 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 like [https://github.com/rdiez/Tools/tree/master/FindUsbSerialPort FindUsbSerialPort.sh] 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 (no hardware flow control)
+
 
+
== 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
+
+
# What the arguments mean:
+
#
+
# -t0: When terminating socat with Ctrl+C, do not wait for a short time,
+
#      but exit immediately.
+
#
+
# On the STDIO side:
+
#  raw: Do not interpret special codes.
+
#  echo=0: Do not print the input characters locally. The remote end
+
#          usually does that.
+
#  escape=0x03: Ctrl+C terminates socat (with an exit code of 0, indicating success).
+
#                If you wish to pass Ctrl+C to the remote end, you can switch to Ctrl+O
+
#                with escape=0x0F.
+
#
+
# On the /dev/mydevice side:
+
#  b115200: The serial port speed.
+
#  cs8,parenb=0,cstopb=0,clocal=0: The usual 8N1 configuration, see the stty example above.
+
#  raw,echo=0: matches the configuration on the STDIO side.
+
#  setlk: Write-lock the whole file, see below for more information on exclusive locks.
+
#  flock-ex-nb: Similar to setlk, see below for more information on exclusive locks.
+
 
+
If the backspace key does not work, try using ''Ctrl+H'' instead.
+
 
+
Depending on your device, you may need to add "crnl" to either the STDIO or the tty side, in order to convert the line termination  characters properly.
+
 
+
=== Alternatives to ''socat'' ===
+
 
+
* picocom -b 115200 -p n -d 8  /dev/serial-port-device  <br/> Exit with Ctrl+A, Ctrl+X.
+
* minicom -b 115200 -8 -D  /dev/serial-port-device
+
* screen /dev/serial-port-device 115200  <br/> Exit with Ctrl+A, '\'.
+
* C-Kermit
+
* cutecom
+
* gtkterm -s 115200 -p /dev/serial-port-device
+
* putty -serial COM8  # Only on Windows.
+
* plink  # Only on Windows.
+
 
+
''grabserial'' reads lines from a serial port and writes them to a standard output, optionally prepending timestamps, which allows you to easily time processes, like booting an embedded operating system.
+
 
+
== 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,&nbsp;F_SETLK,&nbsp;...)", and argument ''flock-ex-nb'', which uses "flock(fd,&nbsp;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.
+

Aktuelle Version vom 15. Mai 2015, 20:13 Uhr

Weiterleitung nach: