User Tools

Site Tools


Sidebar

Network and communication

k24:k24.1:k24.1.5:k24.1.5.1:start

24.1.5.1 RS232

Many computers today hardly have a real serial interface with an RS232 connector such as 'Sub-D-9-pin'. Replacements are USB-RS232 adapters, which enable data transfer with peripheral devices with serial interface via a USB port of your computer. Such a USB-RS232-Adapter offers for example (Figure 24.1.5.1.1) an interface with 1×RS232-plug (Sub-D-9-pin) and 1×USB-A-plug. A hardware chip (USB UART chip as USB2ToSerial bridge) is built into the RS232 connector, which also performs the necessary USB to serial conversion. Linux drivers are available in all modern distributions. The appropriate driver creates a virtual serial port in the operating system, which is used for communication with the USB-RS232 adapter. The adapter behaves like a real serial RS232 interface to software and is also recognized as such.

B1
Figure 24.1.5.1.1: USB-RS232 adapter with extension cable

B2
Figure 24.1.5.1.2: USB-RS232 adapter (USB)

B3
Figure 24.1.5.1.3: USB-RS232-Adapter (RS232)

24.1.5.1.1 Serial interfaces

If you have little or no experience with serial interfaces, then a look at the content of the web pages at http://tldp.org/HOWTO/Serial-HOWTO.html is recommended.

Use the following commands in a console to find out if the computer you are scanning has serial ports.

For a list of recognized serial I/O devices, use the following commands:

hans@mint-183 ~ $ dmesg | grep ttyS
hans@mint-183 ~ $ dmesg | grep ttyUSB

Conclusion of the review of the author's laptop (year of construction 2017): far and wide no serial interface can be detected - both issues are empty.

After connecting two USB-RS232 adapters to the computer, the following display was displayed, since only USB-RS232 adapters were searched for:

hans@mint-183 ~ $ ls -l /dev | grep serial
drwxr-xr-x  4 root root  80 Apr 20 18:14 serial

If the output is not empty and the folder /dev/serial exists, then there is at least one serial USB-RS232 adapter on the computer. This is confirmed by the following display of the devices:

hans@mint-183 ~ $ ls -l /dev/ttyUSB*
crw-rw---- 1 root dialout 188, 0 Apr 20 14:30 /dev/ttyUSB0
crw-rw---- 1 root dialout 188, 1 Apr 20 14:31 /dev/ttyUSB1

clues

  • The device files /dev/ttyUSB0 and /dev/ttyUSB1 are only created temporarily in the /dev folder if such an I/O device is connected as a USB-RS232 adapter.
  • The device name is at the end of the line. This makes it easier to parse the individual lines when you use the command in a shell or EXEC instruction in your RS232 projects.
  • The identifiers ttyUSB0 and ttyUSB1 are the device names assigned to the serial ports by the USB2Serial driver.

It is not recommended to use the dmesg command if you plug in and unplug the USB-RS232 adapter several times, because the redundant outputs can become very extensive:

hans@mint-183 ~ $ dmesg | grep ttyUSB
[   97.789809] usb 3-1: pl2303 converter now attached to ttyUSB0
[  129.010906] usb 2-1.3: pl2303 converter now attached to ttyUSB1
...
[ 8186.990521] pl2303 ttyUSB0: pl2303 converter now disconnected from ttyUSB0
[ 8189.033382] pl2303 ttyUSB1: pl2303 converter now disconnected from ttyUSB1
[ 9009.588711] usb 2-1.3: pl2303 converter now attached to ttyUSB0
[ 9015.188868] usb 3-1: pl2303 converter now attached to ttyUSB1
[10652.765735] pl2303 ttyUSB0: pl2303 converter now disconnected from ttyUSB0
hans@mint-183 ~ $

The last lines are decisive for two USB-RS232 adapters. When parsing the output, you must detect whether both USB-RS232 adapters are plugged in or both have been removed or only one USB-RS232 adapter is plugged in.

A typical output of dmesg on real serial interfaces looks like this:

hans@mint-183 ~ $  dmesg | grep ttyS | grep 00:
[    0.962453] 00:02: ttyS0 at I/O 0x3f8 (irq = 4, base_baud = 115200) is a 16550A

24.1.5.1.2 Access to a serial interface


Figure 24.1.5.1.4: Error message

hans@mint-183 ~ $ ls -l /dev/ttyUSB*
crw-rw---- 1 root dialout 188, 0 Apr 20 14:30 /dev/ttyUSB0
crw-rw---- 1 root dialout 188, 1 Apr 20 14:31 /dev/ttyUSB1

As you can see, by default only the root user and the members of the 'dialout' group have access to the two recognized serial ports (USB-RS232 adapter). Therefore, the user 'hans' must become a member of the group 'dialout'.

Temporary activation of a user

With this command the current user 'hans' from the group 'hans' becomes owner for the device /dev/ttyUSB0 for the current session:

hans@linux:~$ sudo chown -R hans:hans /dev/ttyUSB0
[sudo] password for hans:
hans@linux:~$

The 'sudo adduser username group name' command permanently adds the specified - already existing - user to the specified group.

First, the user'hans' is added to the group'dialout':

hans@mint-183 ~ $ sudo adduser hans dialout
[sudo] Passwort für hans:
Füge Benutzer »hans« der Gruppe »dialout« hinzu ...
Benutzer hans wird zur Gruppe dialout hinzugefügt.
Fertig.

Then the group membership is activated - which is done without confirmation if the activation was successful. Pay attention to the switch - which triggers immediate activation!

hans@mint-183 ~ $ newgrp - dialout
hans@linux:~$

Finally, the groups to which the user'hans' belongs are displayed for control purposes:

hans@mint-183 ~ $ groups hans
hans : hans adm dialout cdrom sudo dip www-data plugdev lpadmin sambashare

The page http://www.netzmafia.de/skripten/hardware/RasPi/RasPi_Serial.html contains many valuable hints for the use of the serial interfaces on the Raspberry Pi.

24.1.5.1.3 Data transmission between computers and RS232 peripherals

Serial interfaces are used for data transmission between computers and peripheral devices. After byte-parallel bit-serial conversion, the individual bytes in the data stream are transmitted byte for byte in a serial bit data stream from the computer via a connection line to the serial peripheral devices. The data from the peripheral devices is converted in the serial interface in bit-serial-byte-parallel mode. The transmission time of an individual bit depends on the set baud rate. The duration for the transmission of a bit is the reciprocal of the baud rate.

With an RS-232 interface, data transmission is asynchronous. This means that the pause between two bytes can be any length. Therefore, the transmitter and receiver synchronize again for each individual character to be transmitted. Synchronization takes place via a so-called start bit and the stop bits, of which there is at least one for the stop bits. During the pause, the signal level is logic 1.

If a character is to be transmitted, a level change from logical 1 to logical 0 marks the beginning of the transmission of the data word by setting the start bit. Then the individual bits of a data word are transmitted with the constant clock (1/baud rate), whereby the least significant bit is transmitted first. A data word consists of 5 to 8 bits, whereby 7 or 8 bits long ASCII-coded data words are usual values as user data. Finally, at least one stop bit follows an optional parity bit - the signal level is set to logical 1. The pause starts and the receiver waits for the next start bit… .

24.1.5.1.4 Notes on the transmission parameters

Transmission parameters Baud rate - SerialPort.speed:

The Gambas documentation indicates for the SerialPort.Speed property that the baud rate must be a valid default value supported by the serial port driver. For example, if you knew the type of UART (Universal Asynchronous Receiver/Transmitter) in the two recognized USB-RS232 adapters, then statements about the maximum supported baud rate would also be possible:

hans@mint-183 ~ $ setserial -g /dev/ttyUSB[01]
/dev/ttyUSB0, UART: 16654, Port: 0x0000, IRQ: 0
/dev/ttyUSB1, UART: 16654, Port: 0x0000, IRQ: 0

A data sheet for the 16654-UART stated: Serial ports controlled by a 16654-UART allow baud rates up to 115200 baud.

The adjustable baud rates in the SerialPort (gb.net) class include 50 baud, 75 baud, 110 baud, 150 baud, 200 baud, 300 baud, 600 baud, 1200 baud, 2400 baud, 4800 baud, 9600 baud, 19200 baud and 38400 baud. Higher baud rates have not been tested.

Transmission Parameters DataBits - SerialPort.DataBits

You can use the SerialPort.DataBits parameter to specify how many data bits a data word consists of. Use one of the following constants: SerialPort.Bits5, SerialPort.Bits6, SerialPort.Bits7 or SerialPort.Bits8 The setting SerialPort.DataBits = SerialPort.Bits8 is common.

Transmission Parameters Parity Check - SerialPort.Parity

A parity check makes it possible to check for 1-bit errors during the transmission of a data word. During the parity check, if it was switched on as even or odd, the number of 1-value bits in the data word is determined on the transmitter side first. If the determined number is odd, the parity bit is set to 1 with even parity, for example, so that the number of 1-value bits is even. These are the settings for this transmission parameter: Even parity (E), No parity (N), Odd parity: (O) or SerialPort.Parity = SerialPort.None, SerialPort.Parity = SerialPort.Even, SerialPort.Parity = SerialPort.Odd. Since the same transmission parameters must be used on the receiver side, only 1-bit data errors can be detected by comparison. For this reason, a parity check is often waived and the transmission parameter is set to No parity (N).

Transmission Parameters StopBits - SerialPort.StopBits

Normally, you can set 1 stop bit (SerialPort.stop bits = SerialPort.SerialPort.bits1 ) or 2 stop bits (SerialPort.stop bits = SerialPort.SerialPort.bits2). Note that for a 5-bit data word, setting 2 stop bits results in 1.5 stop bits, as level-time measurements confirmed. There is no SerialPort.Start bit property in the SerialPort class, because there is only one start bit with a fixed signal level (logical 0) and this is set automatically.

24.1.5.1.5 Notes on data flow control

Data flow control is only required when a device - such as a computer - can deliver data faster than a serial peripheral can process it. In this case, the peripheral device must signal to the computer that it cannot currently receive any further data via the RS232 interface. The signaling can be carried out either via two special control characters in the data stream (software handshake) or via setting and reading the levels of certain control lines (hardware handshake).

Software handshake:
The control character Xoff (DC3, 13hex, 19dec) or the control character Xon (DC1, 11hex, 17dec) are used in the data stream to detect the status of the two serial devices. The two control characters must understandably not be contained in the user data package to be transmitted!

Hardware handshake:
The DTR control line (out) signals that the data terminal (here the PC) is ready. The DSR line (in) signals to the data terminal that the remote station is ready. The two control lines RTS (out) and CTS (in) are used for data flow control.

Use one of the following constants: SerialPort.Hardware, SerialPort.Software, or SerialPort.Both to specify the type of flow control. If flow control is not required, set the SerialPort.FlowControl property to SerialPort.None.

24.1.5.1.6 Initialization of SerialPort object

If you open an RS232 interface which you have either dragged in the Gambas IDE from the tool collection in the'Network' tab onto the form or create in the source code, for example with hRS232 = New SerialPort As “hRS232”, the RS232 interface is initialized with the following transfer parameters:


Figure 24.1.5.1.5: Default transfer parameters for a (new) serial port (Gambas IDE)

You can make changes to the transfer parameters in the object inspector or in the source code. Then use the given constants of the class SerialPort (gb.net):

 ...
 hRS232.Parity = SerialPort.None
 hRS232.StopBits = SerialPort.Bits1
 hRS232.FlowControl = SerialPort.None
 ...
The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k24/k24.1/k24.1.5/k24.1.5.1/start.txt · Last modified: 18.06.2022 (external edit)

Page Tools