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