Linux serial Programming (c)

Source: Internet
Author: User
Tags control characters strcmp vmin

Linux c: Serial port settings
Serial operation is no more than the following:
1 Open
2 Setting the serial port properties
3 Read Write

struct Termios can show all the serial port properties, which are not explained in detail here.
See "Linux Open Class" Serial port property settings 2hx5ros7br3*gbjvmzq0om2x3kmaonfwdt1sspb2fmdoc68n3k6nqofousmf3uwy8hmv58imyit4xiugpdsqvepsubo8osdt* bcwwuvgdqvsuypvfgz8arph5*9qxamrlcafmopa9fk42fwijitw6a==
and the Linux application development, this article has done the detailed explanation to the serial port structure body

struct Termios {
tcflag_t C_iflag; /* Input Parameters */
tcflag_t C_oflag; /* OUTPUT Parameters */
tcflag_t C_cflag; /* Control parameters */
tcflag_t C_ispeed; /* Input baud rate */
tcflag_t C_ospeed; /* Output baud rate */
cc_t C_line; /* Line Control */
cc_t C_cc[nccs]; /* Control characters */

Xereno The serial code is too Fanluan, only to do the contrast to confirm with.
Here is a snippet of network code for analysis:
Serial Port Operation Example
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <asm/termios.h>

#include "serial.h"

#define DEV_NAME "/dev/ttys1"

int set_port_attr (int fd,int baudrate, int databit, const char *stopbit, char parity, int vtime,int vmin);
static void Set_baudrate (struct termios *opt, unsigned int baudrate);
static void Set_data_bit (struct termios *opt, unsigned int databit);
static void Set_stopbit (struct Termios *opt, const char *stopbit);
static void Set_parity (struct termios *opt, char parity);

int main (int argc, char *argv[])
int FD;
int Len, I,ret;
Char buf[] = "Hello zlg!";

FD = open (Dev_name, O_RDWR | O_noctty);
if (FD < 0) {
Perror (Dev_name);

ret = set_port_attr (FD, B115200, 8, "1", ' N ', 150,255); /* 115200 8N1 */
if (Ret < 0) {
printf ("set UART ARRT faile \ n");
Exit (-1);

Len = Write (fd, buf, sizeof (BUF)); /* Send string to serial port */
if (Len < 0) {
printf ("Write data error \ n");

Len = Read (fd, buf, sizeof (BUF)); /* Read string in serial port */
if (Len < 0) {
printf ("read error \ n");

printf ("%s \ n", buf); /* Print the string read in the serial port */

return (0);

The custom Terminal property setting function is defined as follows
When setting terminal properties, note that some items are passed with &amp, while others are through or |. Don't confuse misunderstandings.
int set_port_attr (int fd,int baudrate, int databit, const char *stopbit, char parity, int vtime,int vmin)
struct Termios opt;
Tcgetattr (FD, &opt); Get Initial settings

Set_baudrate (&opt, baudrate);
Set_data_bit (&opt, databit);
Set_parity (&opt, parity);
Set_stopbit (&opt, stopbit);

Opt.c_cflag &= ~crtscts;//does not use hardware flow control
Opt.c_cflag |= clocal | Cread; clocal--Ignore modem control line, local connection, no data machine control function, cread--enable to receive the flag

ixon--Xon/xoff flow control with output enabled
ixoff--Xon/xoff flow control with input enabled
ixany--allows any character to start the output again
igncr--Ignore carriage return in input
Opt.c_iflag &= ~ (Ixon | Ixoff | Ixany);
Opt.c_oflag &= ~opost; Enable output processing
icanon--Enable standard mode (canonical modes). Allows the use of special characters EOF, EOL,
EOL2, ERASE, KILL, Lnext, reprint, STATUS, and Werase, as well as buffer by row.
echo--echo Input characters
echoe--if Icanon is set at the same time, the character ERASE erase the previous input character, Werase erase the previous word
isig--when receiving a character INTR, QUIT, SUSP, or DSUSP, generate the corresponding signal
Opt.c_lflag &= ~ (Icanon | ECHO | Echoe | ISIG);
Opt.c_cc[vmin] = VMIN; To set the timeout length and minimum number of characters in non-canonical mode:
Opt.c_cc[vtime] = Vtime; Vtime in conjunction with Vmin, which is the maximum time limit for transmission or waiting

Tcflush (FD, Tciflush); /* tciflush--update the options and do it now */
Return (Tcsetattr (FD, Tcsanow, &opt)); /* tcsanow--change occurs immediately */

Custom Set_baudrate () function
Use the Set_baudrate () function to set the serial port input/output baud rate to 115200 for the code: set_baudrate (&opt, B115200));
In general, the serial port input and output baud rate are set to the same value, if you want to set up separately, you need to call Cfsetispeed, Cfsetospeed
static void Set_baudrate (struct termios *opt, unsigned int baudrate)
Cfsetispeed (opt, baudrate);
Cfsetospeed (opt, baudrate);

Custom Set_stopbit functions
In the Set_stopbit () function, the Stopbit parameter can take the following values: "1" (1-bit stop bit), "1.5" (1.5-bit stop bit), and "2" (2-bit stop bit).
static void Set_stopbit (struct Termios *opt, const char *stopbit)
if (0 = = strcmp (stopbit, "1")) {
Opt->c_cflag &= ~CSTOPB; /* 1-bit stop bit t */
} else if (0 = = strcmp (stopbit, "1.5")) {
Opt->c_cflag &= ~CSTOPB; /* 1.5-bit stop bit */
}else if (0 = = strcmp (Stopbit, "2")) {
Opt->c_cflag |= CSTOPB; /* 2-bit stop bit */
}else {
Opt->c_cflag &= ~CSTOPB; /* 1-bit stop bit */

Set_data_bit function
csize--the character length mask. Values are CS5, CS6, CS7, or CS8
static void Set_data_bit (struct termios *opt, unsigned int databit)
Opt->c_cflag &= ~csize;
Switch (databit) {
Case 8:
Opt->c_cflag |= CS8;
Case 7:
Opt->c_cflag |= CS7;
Case 6:
Opt->c_cflag |= CS6;
Case 5:
Opt->c_cflag |= CS5;
Opt->c_cflag |= CS8;

set_parity function
In the Set_parity function, the parity parameter can be evaluated as: ' n ' and ' n ' (no parity), ' e ' and ' e ' (for parity), ' o ', and ' O ' (for the odd checksum).
static void Set_parity (struct termios *opt, char parity)
Switch (parity) {
Case ' N ':/* No check */
Case ' n ':
Opt->c_cflag &= ~parenb;
Case ' E ':/* parity check */
Case ' E ':
Opt->c_cflag |= Parenb;
Opt->c_cflag &= ~parodd;
Case ' O ':/* odd Check */
Case ' O ':
Opt->c_cflag |= Parenb;
Opt->c_cflag |= ~parodd;
Default:/* Other options are no check */
Opt->c_cflag &= ~parenb;

Linux serial Programming (c)

Related Article

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: 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.