concept

A serial port communication

  • Concept; Serial Communications Sends and receives bytes in bits.
    • A serial port can use one wire (Tx) to send data while using another wire (Rx) to receive data.
  • Implementation: by opening the JNI call, open the serial port. Obtain the input and output streams in serial communication, and use the serial port to receive and send data by operating the IO stream

Noun explanation

// Open the serial port
private native FileDescriptor open(String absolutePath, int baudrate, int dataBits, int parity, int stopBits, int flags);
Copy the code
  • AbsolutePath: Physical address of the serial port. Hardware engineers will tell you such as ttyS0, ttyS1, etc., or use the SerialPortFinder class to obtain the available serial port address.
  • Baudrate: Serial port transmission rate. The number of bits of data a device sends (or receives) in one second. It is used to measure the speed of data transmission.
    • That is, the number of carrier parameter changes per unit time. For example, 240 characters are transmitted per second, and each character format contains 10 bits (1 start bit, 1 stop bit, 8 data bits). In this case, the baud rate is 240Bd, and the bit rate is 10 bits *240 bits/second =2400bps.
    • The baud rate is inversely proportional to the distance. The higher the baud rate, the shorter the transmission distance.
  • DataBits: Data bit length, standard values are 6, 7, and 8 bits.
  • Parity: parity bit, a simple method of error checking for serial communication, 0-none, 1-parity, and 2-even
    • In the case of even and odd check, the serial port sets the check bit (the bit after the data bit), a value that ensures that the data being transmitted has an even or odd logical high place.
  • StopBits: Indicates the stop bit, which indicates the last bit of a single packet. The standard value is 1 or 2 bits.
    • Since the data is timed on the transmission line and each device has its own clock, it is likely that there is a small mismatch between the two devices in the communication.
    • The stop bit therefore not only indicates the end of transmission, but also provides the computer with the opportunity to correct clock synchronization.
    • The more bits that apply to stop bits, the greater the tolerance of different clock synchronization, but the slower the data transfer rate.
  • Flags: The default is 0, indicating read-write. Flags can be set to serial mode by bit-counting with the default O_RDWR (read-write)
    • fd = open(path_utf, O_RDWR | flags);

Serial port file opening mode

  • O_RDONLY: opens the file in read-only mode
  • O_WRONLY: opens a file in write only mode
  • O_RDWR: Opens files in read/write mode
  • O_APPEND: added to the end of the file when data is written
  • O_CREATE: generates a file if it does not exist. To use this flag, you need to set the access bit mode_t
  • O_EXCL: Specifies this flag, and the O_CREATE flag is specified to generate an error if the open file exists
  • O_TRUNC: If the file exists and is successfully opened in write or write-only mode, all contents of the file are cleared, making the file length 0
  • O_NOCTTY: If a terminal device is opened, the program will not be the control terminal corresponding to the port. Without this flag, any input, such as keyboard stop signal, will affect the process.
  • O_NONBLOCK: This flag is similar to the O_NDELAY flag used earlier. The program does not care about the state of the DCD signal line. If this flag is specified, the process will sleep until the DCD signal line is 0.

In practice, blocking mode is always used to save resources. But if you want to read and write simultaneously in one thread, the thread will block and wait until there is no data to write.

A serial port address

The following table lists the serial port addresses of different operating systems. Generally, the serial port address of an Android device is /dev/ttys0…

System Port 1 Port 2
IRIX ® /dev/ttyf1 /dev/ttyf2
HP-UX /dev/tty1p0 /dev/tty2p0
Solaris ® / SunOS ® /dev/ttya /dev/ttyb
Linux ® /dev/ttyS0 /dev/ttyS1
Digital UNIX ® /dev/tty01 /dev/tty02

tool

  • Android port Google official serial port library support check bit, data bit, stop bit, flow control configuration: juejin.cn/post/684490…
  • Windows friendly serial debugging tools: www.darkwood.me/serialport/
  • Google Open Source Android serial communication Demo: github.com/licheedev/A…