Linux serial port operation functions (1)

Source: Internet
Author: User

1. open the serial port: fd = open ("/dev/ttyf1", O_RDWR | O_NOCTTY | O_NDELAY); fcntl (fd, F_SETFL, 0 ); the O_NOCTTY option prevents programs from being affected by the keyboard-controlled stop operation keys. o_NDELAY tells UNIX not to enable another port. (detect DCD signal line status)

2. Send data n = write (fd, "ATZ \ r", 4) to the serial port );

3. when the serial port is opened in raw data mode, the read system call will return no matter how many characters are read in the serial port input cache. if no data exists, it is blocked until a character arrives or the timer times out. after this option is set in the serial port, read calls are immediately returned. if no data is readable, read returns 0 fcntl (fd, F_SETFL, FNDELAY );

To disable this function

Fcntl (fd, F_SETFL, 0); 4. Disable the serial port

Close (fd );

Ii. Standard POSIX configuration serial port parameters serial port data transmission and receiving mainly requires port configuration, including , Define the terminal control structure and

POSIX control functions

Termios Structure

Table 3-Termios Structure Members

Member Description

C_cflag Control options

C_lflag Line options

C_iflag Input options

C_oflag Output options

C_cc Control characters

C_ispeed Input baud (new interface)

C_ospeed Output baud (new interface)

Struct termios termios_old, termios_new;

1) Get the serial port attributes

Tcgetattr (fdcom, & termios_old );

2) configure the input rate

Cfsetispeed (& termios_new, baudrate); cfsetospeed (& termios_new, baudrate); 3) control mode to ensure that the program will not become a port occupying termios_new.c_cflag | = CLOCAL; Control Mode, enable the port to read the input data termios_new.c_cflag | = CREAD; 4) control mode, mask character size bits, and set the number of digits termios_new.c_cflag used for data transmission through the serial port ~ CSIZE; termios_new.c_cflag | = CS5; // CS6, CS7, CS8

5) parity check

// No parity check

Termios_new.c_cflag & = ~ PARENB;

// Even check

Termios_new.c_cflag | = PARENB; termios_new.c_cflag & = PARODD;

// Odd check

Termios_new.c_cflag | = PARENB; termios_new.c_cflag | = PARODD;

6) set the stop bit

Termios_new.c_cflag | = CSTOPB; // 2 stop bits termios_new.c_cflag & = ~ CSTOPB; // 1 stop bits.

7) Other property configurations

Termios_new.c_oflag & = ~ OPOST; // output mode. The output of raw data is termios_new.c_cc [VMIN] = 1; // control character. The minimum number of characters to be read is termios_new.c_cc [VTIME] = 1; // control character, the waiting time for reading the first character, with the order of 0.1

Bit

8) set new attributes

Tcsetattr (fdcom, TCSANOW, & termios_new );

// TCSANOW: The change takes effect immediately

// TCSADRAIN: Set it when everything is sent

// TCSAFLUSH: set after overflow of all input and output buffer

Use the select system call to read serial port data and other socket and device data


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.