Linux串口读写:
#include <stdio.h> /*标准输入输出定义*/
#include <stdlib.h> /*标准函数库定义*/
#include <unistd.h> /*Unix 标准函数定义*/
#include <sys/types.h>
#include <sys/stat.h>
#include "string.h"
#include <fcntl.h> /*文件控制定义*/
#include <termios.h> /*PPSIX 终端控制定义*/
#include <errno.h> /*错误号定义*/
#define FALSE -1
#define TRUE 0
/*********************************************************************/
int OpenDev(char *Dev)
{
int fd = open( Dev, O_RDWR | O_NOCTTY ); //| O_NOCTTY | O_NDELAY
if (-1 == fd)
{
perror("Can't Open Serial Port");
return -1;
}
else
return fd;
}
/**
*@brief 设置串口通信速率
*@param fd 类型 int 打开串口的文件句柄
*@param speed 类型 int 串口速度
*@return void
*/
int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,
B38400, B19200, B9600, B4800, B2400, B1200, B300, };
int name_arr[] = {38400, 19200, 9600, 4800, 2400, 1200, 300, 38400,
19200, 9600, 4800, 2400, 1200, 300, };
void set_speed(int fd, int speed)
{
int i;
int status;
struct termios Opt;
tcgetattr(fd, &Opt);
for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++) {
if (speed == name_arr[i]) {
tcflush(fd, TCIOFLUSH);
cfsetispeed(&Opt, speed_arr[i]);
cfsetospeed(&Opt, speed_arr[i]);
status = tcsetattr(fd, TCSANOW, &Opt);
if (status != 0) {
perror("tcsetattr fd1");
return;
}
tcflush(fd,TCIOFLUSH);
}
}
}
/**
*@brief 设置串口数据位,停止位和效验位
*@param fd 类型 int 打开的串口文件句柄
*@param databits 类型 int 数据位 取值 为 7 或者8
*@param stopbits 类型 int 停止位 取值为 1 或者2
*@param parity 类型 int 效验类型 取值为N,E,O,,S
*/
int set_Parity(int fd,int databits,int stopbits,int parity)
{
struct termios options;
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*Input*/
options.c_oflag &= ~OPOST; /*Output*/
if ( tcgetattr( fd,&options) != 0) {
perror("SetupSerial 1");
return(FALSE);
}
options.c_cflag &= ~CSIZE;
switch (databits) /*设置数据位数*/
{
case 7:
options.c_cflag |= CS7;
break;
case 8:
options.c_cflag |= CS8;
break;
default:
fprintf(stderr,"Unsupported data size/n"); return (FALSE);
}
switch (parity)
{
case 'n':
case 'N':
options.c_cflag &= ~PARENB; /* Clear parity enable */
options.c_iflag &= ~INPCK; /* Enable parity checking */
break;
case 'o':
case 'O':
options.c_cflag |= (PARODD | PARENB); /* 设置为奇效验*/
options.c_iflag |= INPCK; /* Disnable parity checking */
break;
case 'e':
case 'E':
options.c_cflag |= PARENB; /* Enable parity */
options.c_cflag &= ~PARODD; /* 转换为偶效验*/
options.c_iflag |= INPCK; /* Disnable parity checking */
break;
case 'S':
case 's': /*as no parity*/
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;break;
default:
fprintf(stderr,"Unsupported parity/n");
return (FALSE);
}
/* 设置停止位*/
switch (stopbits)
{
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
fprintf(stderr,"Unsupported stop bits/n");
return (FALSE);
}
/* Set input parity option */
if (parity != 'n')
options.c_iflag |= INPCK;
tcflush(fd,TCIFLUSH);
options.c_cc[VTIME] = 150; /* 设置超时15 seconds*/
options.c_cc[VMIN] = 0; /* Update the options and do it NOW */
if (tcsetattr(fd,TCSANOW,&options) != 0)
{
perror("SetupSerial 3");
return (FALSE);
}
return (TRUE);
}
int main(int argc, char **argv)
{
int fd;
int nread;
char buff[512];
char *dev = "/dev/ttyS0"; //串口二
fd = OpenDev(dev);
set_speed(fd,4800);
if (set_Parity(fd,8,1,'N') == FALSE)
{
printf("Set Parity Error/n");
exit (0);
}
int i;
i = getchar();
if ( i == '1')
{
while (1) //循环读取数据
{
while((nread = read(fd, buff, 512))>0)
{
printf("/nLen %d/n",nread);
buff[nread+1] = '/0';
printf( "/n%s", buff);
}
}
}
if ( i == '2')
{
while (1) //循环写入数据
{
gets(buff);
printf("------buff--->%s<--------/n",buff);
int num = strlen(buff);
printf("--------num---->%d<--------------/n",num);
if ( num > 0)
{
printf("Wirte num not NULL./r/n");
nread = write(fd, buff ,num);
if(nread == -1)
{
printf("Wirte sbuf error./n");
}
printf("--nread---->%d<-----------/n",nread);
}
}
}
close(fd);
//exit (0);
}
2. linux下的rs232串口通讯c代码
补充:
针口的叫“公头”,有孔的叫“母头”,如果没有两个母头的串口线的话,可以使用虚拟机,两个虚拟机之间采用"管道"的方式连接,可达到几乎和双机互联一样的效果。
另外还可以下载一个串口调试助手,用于观察或者发送数据。
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <stdlib.h>
int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop)
{
/*
设置串口属性:
fd: 文件描述符
nSpeed: 波特率
nBits: 数据位
nEvent: 奇偶校验
nStop: 停止位
*/
struct termios newtio,oldtio;
if ( tcgetattr( fd,&oldtio) != 0) {
perror("SetupSerial 1");
return -1;
}
bzero( &newtio, sizeof( newtio ) );
newtio.c_cflag |= CLOCAL | CREAD;
newtio.c_cflag &= ~CSIZE;
switch( nBits )
{
case 7:
newtio.c_cflag |= CS7;
break;
case 8:
newtio.c_cflag |= CS8;
break;
}
switch( nEvent )
{
case 'O':
newtio.c_cflag |= PARENB;
newtio.c_cflag |= PARODD;
newtio.c_iflag |= (INPCK | ISTRIP);
break;
case 'E':
newtio.c_iflag |= (INPCK | ISTRIP);
newtio.c_cflag |= PARENB;
newtio.c_cflag &= ~PARODD;
break;
case 'N':
newtio.c_cflag &= ~PARENB;
break;
}
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;
default:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
}
if( nStop == 1 )
newtio.c_cflag &= ~CSTOPB;
else if ( nStop == 2 )
newtio.c_cflag |= CSTOPB;
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 0;
tcflush(fd,TCIFLUSH);
if((tcsetattr(fd,TCSANOW,&newtio))!=0)
{
perror("com set error");
return -1;
}
printf("set done!\n");
return 0;
}
int open_port(int fd,int comport)
{
// char *dev[]={"/dev/ttyS0","/dev/ttyS1","/dev/ttyS2"};
long vdisable;
if (comport==1)
{ fd = open( "/dev/ttyS0", O_RDWR|O_NOCTTY|O_NDELAY);
// { fd = open( "/dev/ttyUSB0", O_RDWR|O_NOCTTY|O_NDELAY);
if (-1 == fd){
perror("Can't Open Serial Port");
return(-1);
}
else
printf("open ttyS0 .....\n");
}
else if(comport==2)
{ fd = open( "/dev/ttyS1", O_RDWR|O_NOCTTY|O_NDELAY);
if (-1 == fd){
perror("Can't Open Serial Port");
return(-1);
}
else
printf("open ttyS1 .....\n");
}
else if (comport==3)
{
fd = open( "/dev/ttyS2", O_RDWR|O_NOCTTY|O_NDELAY);
if (-1 == fd){
perror("Can't Open Serial Port");
return(-1);
}
else
printf("open ttyS2 .....\n");
}
if(fcntl(fd, F_SETFL, 0)<0)
printf("fcntl failed!\n");
else
printf("fcntl=%d\n",fcntl(fd, F_SETFL,0));
if(isatty(STDIN_FILENO)==0)
printf("standard input is not a terminal device\n");
else
printf("isatty success!\n");
printf("fd-open=%d\n",fd);
return fd;
}
int main(void)
{
int fd;
int nread,i;
char buff[]="Hello\n";
if((fd=open_port(fd,1))<0){
perror("open_port error");
return;
}
if((i=set_opt(fd,115200,8,'N',1))<0){
perror("set_opt error");
return;
}
printf("fd=%d\n",fd);
nread=read(fd,buff,8);
printf("nread=%d,%s\n",nread,buff);
close(fd);
return;
}
linux下串口通讯大致就是先打开设备文件,获得文件描述符,然后设置通讯参数,(如波特率什么的),然后就可以像一般文件一样read,write了。
你可以根据上面的例子稍微改动一下做一个发送的程序,然后用串口线双机互联,观察一下效果。
3. Linux串口连接ttyS0、ttyS1是什么意思
这是通信串口名称。
在Linux环境下,串口名从ttyS0开始依次是ttyS1、ttyS2等。在本程序中,使用ttyS0作为通信串口。在打开ttyS0的时候,选项 O_NOCTTY 表示不能把本串口当成控制终端,否则用户的键盘输入信息将影响程序的执行; O_NDELAY表示打开串口的时候,程序并不关心另一端 的串口是否在使用中。在Linux中,打开串口设备和打开普通文件一样,使用的是open()系统调用。比如我么打开串口设备1也就是COM1,只需要:
fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY );
打开的串口设备有很多设置选项。本文中使用int setup_com(int fd)设置。在系统头文件中 定义了终端控制结构struct termios,tcgetattr()和tcsetattr()两个系统函数获得和设置这些属性。结构 struct termios中的域描述的主要属性包括:
c_cflag : 控制选项
c_lflag : 线选项
c_iflag : 输入选项
c_oflag :输出选项
c_cc :控制字符
c_ispeed :输入数据波特率
c_ospeed :输出数据波特率
如果要设置某个选项,那么就使用"|=“运算,如果关闭某个选项就使用”&=“和”~"运算。本文使用的各个选项的意义定义如下:
c_cflag:
CLOCAL 本地模式,不改变端口的所有者
CREAD 表示使能数据接收器
PARENB 表示偶校验
PARODD 表示奇校验
CSTOPB 使用两个停止位
CSIZE 对数据的bit使用掩码
CS8 数据宽度是8bit
c_lflag:
ICANON 使能规范输入,否则使用原始数据(本文使用)
ECHO 回送(echo)输入数据
ECHOE 回送擦除字符
ISIG 使能SIGINTR,SIGSUSP, SIGDSUSP和 SIGQUIT 信号
c_iflag:
IXON 使能输出软件控制
IXOFF 使能输入软件控制
IXANY 允许任何字符再次开启数据流
INLCR 把字符NL(0A)映射到CR(0D)
IGNCR 忽略字符CR(0D)
ICRNL 把CR(0D)映射成字符NR(0A)
c_oflag: OPOST 输出后处理,如果不设置表示原始数据(本文使用原始数据)
c_cc[VMIN]: 最少可读数据
c_cc[VTIME]: 等待数据时间(10秒的倍数)
4. Linux中串口read怎样阻塞的方式读入数据
open(dev, O_NONBLOCK|O_RDWR)..... 非阻塞
------解决方案--------------------
C/C++ code fd = open( Dev, O_RDWR | O_NOCTTY);
options.c_cc[VTIME] = 0; /* 等待100ms* 该值等待时间就返回 */
options.c_cc[VMIN] = 1; /* 接收到该值数量字节就返回 */
/* 上面两个条件为非零时才有效,两个都为非零时任意一个条件达到都返回,如果两个条件都为零,则马上返回 */
tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */
if (tcsetattr(fd,TCSANOW,&options) != 0){
return ERRCOM_SETATTR;
}
return ERRCOM_OK;
------解决方案--------------------
两个都为非零时任意一个条件达到都返回
如果两个条件都为零,则马上返回
如果一个为非零,则仅仅关注该非零条件
5. linux串口通信代码解释,一句一句来
简单说几句吧,来linux下的设备都是文件自,流程也无非是open, read/write, close等
当然,串口你得设置各种属性才行对不对,比如在win下的超级终端就设置了波特率啊,停止位啊,奇偶校验啊什么的,这些属性都通过
int tcgetattr(int fd, struct termios *termios_p);
int tcsetattr(int fd, int optional_actions, const struct termios *termios_p);函数来设置。
完整代码吗自己去google,一把一把的,其实最重要的是设置好属性,剩下的就是read,write的问题咯。
希望对你有用
对了,了解终端函数的详情请在linux命令行终端获取: man termios
6. 嵌入式代码open("/dev/ttyso",o_RDWR/O_NOCTTY/O_NDELAY);这个代码说的含义
这是文件I/O的常用函数,open函数,open函数用来打开一个设备,他返回的是一个整型变量,如果这个值等于-1,说明打开文件出现错误,如果为大于0的值,那么这个值代表的就是文件描述符。一般的写法是if((fd=open("/dev/ttys0",O_RDWR | O_NOCTTY | O_NDELAY)<0){
perror("open");
}
这个事常用的一种用法fd是设备描述符,linux在操作硬件设备时,屏蔽了硬件的基本细节,只把硬件当做文件来进行操作,而所有的操作都是以open函数来开始,它用来获取fd,然后后期的其他操作全部控制fd来完成对硬件设备的实际操作。你要打开的/dev/ttyS0,代表的是串口1,也就是常说的com1,后面跟的是一些控制字。int open(const char *pathname, int oflag, …/*, mode_t mode * / ) ;这个就是open函数的公式。控制字可以有多种,我现在给你列出来:
O_RDONLY 只读打开。
O_WRONLY 只写打开。
O_RDWR 读、写打开。
O_APPEND 每次写时都加到文件的尾端。
O_CREAT 若此文件不存在则创建它。使用此选择项时,需同时说明第三个参数mode,用其说明该新文件的存取许可权位。
O_EXCL 如果同时指定了O_CREAT,而文件已经存在,则出错。这可测试一个文件是否存在,如果不存在则创建此文件成为一个原子操作。
O_TRUNC 如果此文件存在,而且为只读或只写成功打开,则将其长度截短为0。
O_NOCTTY 如果p a t h n a m e指的是终端设备,则不将此设备分配作为此进程的控制终端。
O_NONBLOCK 如果p a t h n a m e指的是一个F I F O、一个块特殊文件或一个字符特殊文件,则此选择项为此文件的本次打开操作和后续的I / O操作设置非阻塞方式。
O_SYNC 使每次w r i t e都等到物理I / O操作完成。
这些控制字都是通过“或”符号分开(|)
通过这些介绍,你的那段代码就不难解释了:是以读写方式、不把该文件作为终端设备、无延时模式打开串口1.
7. 嵌入式LINUX平台下,串口通讯时,串口初始化,设置属性失败
检查一下你的dev目录下是否有ttyAMA0这个文件,我怎么感觉常见的都是tty0之类的呢?
8. Linux下串口通信丢字节的问题是怎么样解决
int con=atoi(portstr);
unsigned char Port_file_name[30];
int fd0,rc;
struct termios ts0;
switch (con)
{ //选项O_NOCTTY 表示不能把本串口当成控制终端,否则用户的键盘输入信息将影响程序的执行
//O_NDELAY表示打开串口的时候,程序并不关心另一端的串口是否在使用中
case 1: fd0=open("/dev/ttyM0",O_RDWR | O_NOCTTY | O_NDELAY); break;
case 2: fd0=open("/dev/ttyM1",O_RDWR | O_NOCTTY | O_NDELAY); break;
case 3: fd0=open("/dev/ttyM2",O_RDWR | O_NOCTTY | O_NDELAY); break;
case 4: fd0=open("/dev/ttyM3",O_RDWR | O_NOCTTY | O_NDELAY); break;
case 5: fd0=open("/dev/ttyM4",O_RDWR | O_NOCTTY | O_NDELAY); break;
case 6: fd0=open("/dev/ttyM5",O_RDWR | O_NOCTTY | O_NDELAY); break;
case 7: fd0=open("/dev/ttyM6",O_RDWR | O_NOCTTY | O_NDELAY); break;
case 8: fd0=open("/dev/ttyM7",O_RDWR | O_NOCTTY | O_NDELAY); break;
default : fd0=open("/dev/ttyM0",O_RDWR | O_NOCTTY | O_NDELAY); break;
}
tcgetattr(fd0,&ts0);
bzero(&ts0,sizeof(struct termios));
switch (gytype)
{
case 1:{ts0.c_cflag |= B300 | CS7 | CLOCAL | CREAD | PARENB ;
ts0.c_cflag &= ~PARODD; // 转换为偶效验
ts0.c_iflag |= INPCK; // Disnable parity checking
break; }
case 2:{ts0.c_cflag |= B1200 | CS8 | CLOCAL | CREAD | PARENB ;
ts0.c_cflag &= ~PARODD; // 转换为偶效验
ts0.c_iflag |= INPCK; // Disnable parity checking
break;
}
case 3:{
ts0.c_cflag |= B9600 | CS8 | CLOCAL | CREAD ;
ts0.c_cflag &= ~PARENB; // Clear parity enable
ts0.c_iflag &= ~INPCK; // Enable parity checking
break;
}
case 4:{ts0.c_cflag |= B9600 | CS8 | CLOCAL | CREAD | PARENB ;
ts0.c_cflag &= ~PARODD; // 转换为偶效验
ts0.c_iflag |= INPCK; // Disnable parity checking
break;
}
}
ts0.c_lflag &= ~ECHO;
ts0.c_lflag &= ~ECHONL;
ts0.c_iflag &= ~IXOFF;
ts0.c_iflag &= ~IXON;
ts0.c_cflag &= ~CSIZE;
switch (gytype)
{
case 1:{ts0.c_cflag |= CS7 ; break;}
case 2:{ts0.c_cflag |= CS8 ; break;}
case 3:{ts0.c_cflag |= CS8 ; break;}
case 4:{ts0.c_cflag |= CS8 ; break;}
}
ts0.c_lflag &= ~ICANON; //如果设置使能规范输入,否则使用原始数据(本文使用)
ts0.c_oflag &= ~ONLCR; //如果设置将NL转换成CR-NL后输出
ts0.c_iflag &= ~INLCR; //如果设置将接收到的NL(换行)转换成CR(回车)。
ts0.c_cc[VMIN] = 0; //最少可读数据
ts0.c_cc[VTIME] = 0; //等待数据时间(10秒的倍数)
ts0.c_cflag &= ~CSTOPB; //如果设置则使用两个停止位 ,如果取消则使用一个停止位
ts0.c_iflag |= IGNBRK; //如果设置则忽略接收到的break信号
ts0.c_lflag &= ~IEXTEN; //如果设置则启用实现自定义的输入处理
ts0.c_lflag |= NOFLSH; //如果设置则禁止产生SIGINT,SIGQUIT和SIGSUSP信号时刷新输入和输出队列
switch (gytype)
{
case 1:{rc = cfsetospeed(&ts0,B300);break; }
case 2:{rc = cfsetospeed(&ts0,B1200);break; }
case 3:{rc = cfsetospeed(&ts0,B9600);break; }
case 4:{rc = cfsetospeed(&ts0,B9600);break; }
}
rc = tcsetattr(fd0,TCSAFLUSH,&ts0);
return fd0;
9. 如何查看linux串口cts
在Linux环境下,串口名从ttyS0开始依次是ttyS1、ttyS2等。在本程序中,使用ttyS0作为通信串口。在打开ttyS0的时候,选项 O_NOCTTY 表示不能把本串口当成控制终端,否则用户的键盘输入信息将影响程序的执行; O_NDELAY表示打开串口的时候,程序并不关心另一端 的串口是否在使用中。在Linux中,打开串口设备和打开普通文件一样,使用的是open()系统调用。比如我么打开串口设备1也就是COM1,只需要: fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY ); 打开的串口设备有很多设置选项。本文中使用int setup_com(int fd)设置。在系统头文件<termios.h>中 定义了终端控制结构struct termios,tcgetattr()和tcsetattr()两个系统函数获得和设置这些属性。结构 struct termios中的域描述的主要属性包括: c_cflag : 控制选项 c_lflag : 线选项 c_iflag : 输入选项 c_oflag :输出选项 c_cc :控制字符 c_ispeed :输入数据波特率 c_ospeed :输出数据波特率 如果要设置某个选项,那么就使用"|="运算,如果关闭某个选项就使用"&="和"~"运算。本文使用的各个选项的意义定义如下: c_cflag: CLOCAL 本地模式,不改变端口的所有者 CREAD 表示使能数据接收器 PARENB 表示偶校验 PARODD 表示奇校验 CSTOPB 使用两个停止位 CSIZE 对数据的bit使用掩码 CS8 数据宽度是8bit c_lflag: ICANON 使能规范输入,否则使用原始数据(本文使用) ECHO 回送(echo)输入数据 ECHOE 回送擦除字符 ISIG 使能SIGINTR,SIGSUSP, SIGDSUSP和 SIGQUIT 信号 c_iflag: IXON 使能输出软件控制 IXOFF 使能输入软件控制 IXANY 允许任何字符再次开启数据流 INLCR 把字符NL(0A)映射到CR(0D) IGNCR 忽略字符CR(0D) ICRNL 把CR(0D)映射成字符NR(0A) c_oflag: OPOST 输出后处理,如果不设置表示原始数据(本文使用原始数据) c_cc[VMIN]: 最少可读数据 c_cc[VTIME]: 等待数据时间(10秒的倍数) 根据以上设置的定义,串口端口设置函数setup_com()定义如下: int setup_com(int fd){ struct termios options; tcgetattr(fd, &options); /* Set the baud rates to 38400...*/ cfsetispeed(&options, B38400); cfsetospeed(&options, B38400); /* Enable the receiver and set local mode...*/ options.c_cflag |= (CLOCAL | CREAD); /* Set c_cflag options.*/ options.c_cflag |= PARENB; options.c_cflag &= ~PARODD; options.c_cflag &= ~CSTOPB; options.c_cflag &= ~CSIZE; options.c_cflag |= CS8; /* Set c_iflag input options */ options.c_iflag &=~(IXON | IXOFF | IXANY); options.c_iflag &=~(INLCR | IGNCR | ICRNL); options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /* Set c_oflag output options */ options.c_oflag &= ~OPOST; /* Set the timeout options */ options.c_cc[VMIN] = 0; options.c_cc[VTIME] = 10; tcsetattr(fd, TCSANOW, &options); return 1; } 6.7.2 设置串口通信参数 串口通信参数指的是波特率、数据位、奇偶校验位和停止位。对串口实现控制的时候同样要用到termio结构体。下面将结合具体的代码说明如何设置这些参数。 1.波特率设置 获得端口波特率信息是通过cfgetispeed函数和cfgetospeed函数来实现的。cfgetispeed函数用于获得结构体 termios_p中的输入波特率信息,而cfgetospeed函数用于获得结构体termios_p 中的输出波特率信息。这两个函数的具体信息如表 6.9所示。 表6.9 cfgetispeed函数和cfgetospeed函数 头文件 <termios.h> <unistd.h> 函数形式 speed_t cfgetispeed(const struct termios *termios_p); speed_t cfgetospeed(const struct termios *termios_p); 返回值 成功 失败 是否设置errno 返回termios_p结构中的输入/输出端口的波特率 ?1 是 cfsetispeed函数和cfsetospeed函数用于设置端口的输入/输出波特率。一般情况下,输入和输出波特率是相等的。cfsetispeed函数和cfsetospeed函数的函数声明信息如表6.10所示。 表6.10 cfsetispeed函数和cfsetospeed函数 头文件 <termios.h> <unistd.h> 函数形式 int cfsetispeed(struct termios *termios_p, speed_t speed); int cfsetospeed(struct termios *termios_p, speed_t speed); 返回值 成功 失败 是否设置errno 返回termios_p结构中的输入/输出端口的波特率 ?1 是 cfsetispeed函数和cfsetospeed函数会修改结构体termios_p中的波特率信息,其中参数speed可以使用表6.11中所列出的宏。 表6.11 speed参数常用波特率信息 宏 定 义 波特率(单位:bit/s) 宏 定 义 波特率(单位:bit/s) B0 0 B1800 1800 B50 50 B2400 2400 B75 75 B4800 4800 B110 110 B9600 9600 B134 134 B19200 19200 B150 150 B38400 38400 B200 200 B57600 57600 B300 300