Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the concept of linux serial port

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "what is the concept of linux serial port". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Linux serial port is the abbreviation of serial interface, which is the expansion interface of serial communication mode. Serial port is a commonly used interface of computer, which has the characteristics of few connecting lines and simple communication. As long as a pair of transmission lines can realize two-way communication, linux can use system call open () or close () to open or close serial port.

The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

What does linux serial port mean?

Serial interface (Serial Interface) refers to the sequential transmission of data bit by bit, which is characterized by simple communication lines, as long as a pair of transmission lines can achieve two-way communication (telephone lines can be directly used as transmission lines), thus greatly reducing the cost, especially suitable for long-distance communication, but the transmission speed is slow.

The communication mode in which the data of a message is transmitted bit by bit in sequence is called serial communication.

The characteristics of serial communication are:

The transmission of data bits is carried out in bit order, and at least one transmission line is needed.

The cost is low but the transmission speed is slow. The distance of serial communication can range from a few meters to several kilometers.

According to the direction of information transmission, serial communication can be further divided into three types: simplex, half-duplex and full-duplex.

Serial port is the physical interface of serial communication on computer. In computer history, serial port has been widely used to connect computers and terminal devices and various external devices. Although Ethernet and USB interfaces also transmit data over a serial stream, serial connections usually refer to the interfaces of hardware or modems that are compatible with RS-232 standards.

Serial interface is referred to as serial port (usually referred to as COM interface), which is an extended interface based on serial communication. Serial port is a commonly used interface of computer, which has few connecting lines and simple communication, so it is widely used. The characteristic of serial port is that the communication line is simple, as long as a pair of transmission lines can achieve two-way communication, which greatly reduces the cost, especially suitable for long-distance communication, but the transmission speed is slow. In Linux, there are also a large number of serial ports. In this paper, we will talk about serial ports under Linux.

First, the header file required by serial port

1: # include / * Standard input and output definition * /

2: # include / * Standard function library definition * /

3: # include / * Unix standard function definition * /

4: # include

5: # include

6: # include / * File Control definition * /

7: # include / * POSIX terminal control definition * /

8: # include / * error number definition * /

Open and close serial port

The operation of serial device files is basically the same as that of other files. You can use the system call open (), close () to open or close the serial port.

Under Linux, the serial port file is under / dev, for example, the serial port one is / dev/ttyS0 and the serial port two is / dev/ttyS1.

Open (), prototype of close () system call

1: # include2: # include3: # include4: int open (const char * path, int oflags); 5: int open (const char * path, int oflags, mode_t mode); 6: # include7: int close (int fildes); 8: example: open serial port ttyS0. 9: int fd;10: / * Open serial port in read-write mode * / 11: fd = open ("/ dev/ttyS0", O_RDWR); 12: if (- 1 = = fd) {13: / * cannot open serial port-* / 14: perror ("open serial port error"); 15:}

Third, set the serial port

Setting serial port includes baud rate setting, check bit setting and stop bit setting. In the serial port setting, the main thing is to set the value of the member of the struct termios structure.

The struct termios structure is as follows

1: # include2: struct termio3: {4: unsigned short cantilever; / * input options input mode flag * / 5: unsigned short cantilever; / * output options output mode flag * / 6: unsigned short cantilever; / * control options control mode flag * / 7: unsigned short cantilever; / * local mode flags * / 8: unsigned char cantilever lineage; / * line discipline * / 9: unsigned char cc [NCC]; / * control characters * / 10:} This is the end of the content of "what is the concept of linux serial port". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report