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

Serial port is set up and programmed under Linux operating system.

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

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "Serial port setting and programming under Linux operating system". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "Serial port is setup and programming under Linux operating system".

The basic ways of data communication common to users can be divided into parallel communication and serial communication.

Parallel communication refers to the simultaneous transmission of a data by multiple data transmission lines. It is characterized by fast transmission speed, which is suitable for short-distance communication, but requires high transmission speed.

Serial communication refers to the sequential transmission of data bit by bit using a transmission line. The characteristic is that the communication line is simple, the communication can be realized by using simple cable, the cost can be reduced, and it is suitable for long-distance communication, but the transmission speed is slow. The commonly used serial port is RS-232-C interface (the full name is "Technical Standard for Serial binary data Exchange Interface between data Terminal equipment (DTE) and data Communication equipment (DCE)").

UART controller: can work in Interrupt (interrupt) mode or DMA (direct memory access) mode. According to the 16-byte FIFO (first-in first-out register), support for the highest baud rate can reach 230.4Kbps.

UART operation: data transmission, data reception, interrupt generation, baud rate generation, Loopback mode, infrared mode and automatic flow control mode.

Serial port settings include: baud rate, number of start bits, number of data bits, number of stop bits and flow control protocol. A baud rate of 115200, a start bit of 1b, a data bit of 8b, a stop bit of 1b and a no-flow control protocol can be configured here.

The corresponding device names of serial port one and serial port two are "/ dev/ttyS0" and "/ dev/ttyS1" in turn.

The reading and writing of serial port under Linux can be completed by using simple "read" and "write" functions. The difference is that other parameters of serial port need to be set separately.

6.4.2 details of serial port settings

The main purpose of the serial port setting is to set the struct termios structure member value:

# include

Struct termio

{

Unsigned short cymbals; / * enter the mode flag * /

Unsigned short clockwise; / * output mode flag * /

Unsigned short centering; / * Control mode flag * /

Unsigned short cantilfag; / * Local mode flag * /

Unsigned short cantilever line; / * line discipline*/

Unsigned short cc [NCC]; / * control characters*/

}

Through the assignment of c_cflag, the baud rate, character size, data bit, stop bit, parity bit and hardware flow control can be set.

Basic process of setting serial port properties:

1. Save the previous serial port configuration

For the sake of safety and convenience of debugging the program later, you can first save the configuration of the original serial port and use the function tcgetattr (fd,&oldtio). This function takes the parameters associated with the fd pointing to the object and stores them in the termios structure referenced by the lodtio. This function can test whether the configuration is correct, whether the serial port is available, and so on. Debug successfully, the function returns 0, fails, the function returns-1.

If (tcgetattr (fd,&oldtio)! = 0)

{

Perror ("SetupSerial 1")

Return-1

}

two。 Activation options are CLOCAL and CREAD

CLOCAL and CREAD are used for local connection and accept enablement, respectively, activating these two options through a bitmask.

Newtio.c_cflag | = CLOCAL | CREAD

3. Set baud rate

The main functions for setting baud rate are cfsetispeed and cfsetospeed.

Cfsetispeed & newtio,B115200)

Cfsetospeed & newtio,B115200)

In general, users need to set the baud rate of the input and output functions to the same. These functions return 0 on success and-1 on failure.

4. Set character size

There are no readily available functions and a bitmask is required. Generally, the bit mask in the data bits is removed and then re-set as required.

Options.c_cflag & = ~ CSIZE; / * mask the character size bits*/

Options.c_cflag | = CS8

5. Set parity bits

First activate the parity bit enable in c_cflag to mark PARENB and whether or not to perform parity, and also activate the parity enable in c_iflag. If the parity is enabled, the code is as follows:

Newtio.c_cflag | = PARENB

Newtio.c_cflag | = PARODD

Newtio.c_iflag | = (INPCK | ISTRIP)

The enable even check code is:

Newtio.c_iflag | = (INPCK | ISTRIP)

Newtio.c_cflag | = PARENB

Newtio.c_cflag & = ~ PAROOD

6. Set stop bit

By activating CSTOPB in c_cflag. If the stop bit is 1, clear CSTOPB, and if the stop bit is 0, activate CSTOPB. Here is the code with the stop bit 1:

Newtio.c_cflag & = ~ CSTOPB

7. Set the minimum number of characters and wait time

If there are no special requirements for receiving characters and wait time, you can set it to 0:

Newtio.c_ c [VTIME] = 0

Newtio.c_ cc [VMIN] = 0

8. Processing reference objects to be written

After the serial port is reset, the reference object to be written before is reprocessed, and the function tcflush (fd,queue_selector) can be called to handle the reference object to be written. For data that is transmitted or received but not read, how it is processed depends on the value of the queue_selector.

Possible values for Queue_selector:

TCIFLUSH: refresh received data but not read it

TCOFLUSH: refreshes written data but does not transfer

TCIOLFLUSH: at the same time refresh received data but not read, and refresh written data but not transfer

This example uses one:

Tcflush (fd, TCIFLUSH)

9. Activate configuration

Use the function tcsetattr:

Function prototype: tcsetattr (fd,OPTION,&newtio)

Newtio here is a variable of type termios. The possible values of OPTION are as follows:

TCSANOW: the changed configuration takes effect immediately

TCSADRAIN: the changed configuration takes effect after all the output written to the fd is finished

TCSAFLUSH: the changed configuration favors that all outputs written to fd reference objects take effect after the end, and all accepted but read-in inputs are discarded before the change occurs.

The function call successfully returns 0 and fails-1.

If ((tcsetattr (fd,TCSANOW,&newtio))! = 0)

{

Perror ("com set error")

Return-1

}

/ * the complete function of serial port configuration. For the versatility of the function, the commonly used options are usually listed in the function, which can greatly facilitate the debugging and use of users in the future.

* / int set_opt (int fd,int nSpeed,int nBits,char nEvent,int nStop) {struct termios newtio,oldtio; / *

Save the existing serial port parameter settings for the test. Here, if there is an error such as a string number, there will be an error message.

* / if (tcgetattr (fd,&oldtio)! = 0) {perror ("SetupSerial 1")

Return-1;} bzero (& newtio,sizeof (newtio)); / *

Step 1, set the character size

* / newtio.c_cflag | = CLOCAL | CREAD

Newtio.c_cflag & = ~ CSIZE; / *

Set stop bit

* / switch (nBits) {case 7:

Newtio.c_cflag | = CS7

Break; case 8:

Newtio.c_cflag | = CS8

Break;} / *

Set parity bits

* / switch (nEvent) {case 'Odd / Odd number

Newtio.c_cflag | = PARENB; newtio.c_cflag | = PARODD

Newtio.c_iflag | = (INPCK | ISTRIP); break; case'Elevlevue / even number

Newtio.c_iflag | = (INPCK | ISTRIP); newtio.c_cflag | = PARENB

Newtio.c_cflag & = ~ PARODD; case 'non-parity newtio.c_cflag / No parity bit PARENB; break;} / *

Set baud rate

* / switch (nSpeed) {case 2400:

Cfsetispeed & newtio,B2400)

Cfsetospeed & newtio,B2400)

Break; case 4800:

Cfsetispeed & newtio,B4800)

Cfsetospeed & newtio,B4800)

Break; case 9600:

Cfsetispeed & newtio,B9600)

Cfsetospeed & newtio,B9600)

Break; case 115200:

Cfsetispeed & newtio,B115200)

Cfsetospeed & newtio,B115200)

Break; case 460800:

Cfsetispeed & newtio,B460800)

Cfsetospeed & newtio,B460800)

Break; default:

Cfsetispeed & newtio,B9600)

Cfsetospeed & newtio,B9600)

Break;} / *

Set the stop bit * / if (nStop==1)

Newtio.c_cflag & = ~ CSTOPB; else if (nStop==2)

Newtio.c_cflag | = CSTOPB; / *

Set wait time and minimum received characters

* / newtio.c_ cc [VTIME] = 0; newtio.c_ cc [VMIN] = 0; / *

Handle unaccepted characters

* / tcflush (fd, TCIFLUSH); / *

Activate a new configuration

* / if ((tcsetattr (fd,TCSANOW,&newtio))! = 0)

{

Perror ("com set error")

Return-1

}

Printf ("set done!\ n")

Return 0

}

At this point, I believe you have a deeper understanding of "serial port setting and programming under the Linux operating system". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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