1. 開啟串列埠
與其他的關於裝置程式設計的方法一樣,在linux下,操作、控制串列埠也是通過操作起裝置檔案進行的。在linux下,串列埠的裝置檔案是/dev/ttys0或/dev/ttys1等。因此要讀寫串列埠,我們首先要開啟串列埠:
char *dev = "/dev/ttys0"; //串列埠1
int fd = open( dev, o_rdwr );
//| o_noctty | o_ndelay
if (-1 == fd)
perror("can't open serial port");
return -1;
else
return fd;
2. 設定串列埠速度
開啟串列埠成功後,我們就可以對其進行讀寫了。首先要設定串列埠的波特率:
int speed_arr = ;
int name_arr = ;
void set_speed(int fd, int speed);
int name_arr = ;
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 fd");
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;
if ( tcgetattr( fd,&options) != 0) {
perror("setupserial 1");
return(false);
options.c_cflag &= ~csize;
options.c_lflag &= ~(icanon | echo | echoe | isig); /*input*/
options.c_oflag &= ~opost; /*output*/
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] = 0; /* 設定超時15 seconds*/
options.c_cc[vmin] = 13; /* define the minimum bytes data to be readed*/
if (tcsetattr(fd,tcsanow,&options) != 0)
perror("setupserial 3");
return (false);
return (true);
**說明:使用串列埠一測試的,傳送的資料是字元,
但是沒有傳送字串結束符號,所以接收到後,後面加上了結束符號
int opendev(char *dev)
int fd = open( dev, o_rdwr );
//| o_noctty | o_ndelay
if (-1 == fd)
perror("can't open serial port");
return -1;
else
return fd;
void getcardinfo(char *buff){
int fd;
int nread,count=0;
char tempbuff[13];
char *dev = "/dev/ttys0"; //串列埠1
fd = opendev(dev);
set_speed(fd,9600);
if (set_parity(fd,8,1,'n') == false) {
printf("set parity error/n");
//return -1;
while (1) //迴圈讀取資料
count=0;
//sleep(5000);
while(1)
if((nread = read(fd, tempbuff, 13))>0)
//printf("/nlen %d/n",nread);
memcpy(&buff[count],tempbuff,nread);
count+=nread;
if(count==13)
buff[count+1] = '/0';
//printf( "/n%s", buff);
break;
//break;
//return buff;
close(fd);
pthread_exit(null);
//close(fd);
// exit (0);
linux 下串列埠通訊
include include include include include include include include include define baudrate b115200 baud rate 115200 define device dev ttyama0 define size...
linux下串列埠通訊
配置串列埠 sudo stty f dev ttys1 115200 raw echo echoe echok crtscts 更改許可權 sudo chmod 777 dev ttys1 向串列埠輸出資料 sudo echo hello,world dev ttys1然後在另一主機的輸出端檢視輸出...
LINUX下串列埠通訊開發
摘要 1.開啟串列埠函式open port 中要實現的函式 1 open dev ttys0 o rdwr o noctty o ndelay 開啟串列埠0 2 fcntl fd,f setfl,0 恢復串列埠為阻塞狀態 3 isatty stdin fileno 測試是否為中斷裝置 非0即是中斷裝...