LINUX下串列埠通訊開發

2021-06-04 11:19:09 字數 4930 閱讀 6038

摘要:

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即是中斷裝置*/

2.配置串列埠引數函式set_opt()中要實現的函式:

(1)儲存原先有串列埠配置

tcgetattr(fd,&oldtio);

(2)先將新串列埠配置清0

bzore(&newtio,sizeof(newito));

(3)啟用選項clocal和cread 並設定資料位大小

newtio.c_cflag |=clocal | cread;

newtio.c_cflag &= ~csize;

newtio.c_cflag |=cs8;

(4)設定奇偶校驗

奇校驗:

newtio.c_cflag |= parenb;

newtio.c_cflag |= parodd;

newtio.c_iflag |= (inpck | istrip);

偶校驗:

newtio.c_iflag |= (inpck | istrip);

newtio.c_cflag |= parend;

newtio.c_cflag &= ~parodd;

無奇偶校驗:

newtio.c_cflag &= ~parenb;

(5) 設定停止位

newtio.c_cflag &= ~cstopb; /*停止位為1*/

newtio.c_cflag |= cstopb;/*停止位為0*/

(6)設定波特率:

cfsetispeed(&newtio,b115200);

cfsetospeed(&newtio,b115200);

(7)設定等待時間和最小接受字元:

newtio.c_cc[vtime] = 0;

newtio.c_cc[vmin] = 0;

(8)處理為接收字元:

tcflush(fd,tciflush);

(9)啟用新配置:

tcsetattr(fd,tcsanow,&newtio);

3.讀寫串列埠

write(fd,buff,8);

read(fd,buff,8);

例程:#include

#include

#include

#include

#include

#include

#include

#include

#include

int set_opt(int fd,int nspeed, int nbits, char nevent, int nstop)

bzero( &newtio, sizeof( newtio ) );

newtio.c_cflag |= clocal | cread;

newtio.c_cflag &= ~csize;

switch( nbits )

switch( nevent )

switch( nspeed )

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)

printf("set done!\n");

return 0;

}int open_port(int fd,int comport)

;long vdisable;

if (comport==1)

else

printf("open ttys0 .....\n");

}else if(comport==2)

else

printf("open ttys1 .....\n");

}else if (comport==3)

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)

if((i=set_opt(fd,115200,8,'n',1))<0)

printf("fd=%d\n",fd);

//    fd=3;

nread=read(fd,buff,8);

printf("nread=%d,%s\n",nread,buff);

close(fd);

return;

我自己的程式:

#ifndef _com_h

#define _com_h

#include

#include

#include

#include

#include

#include

#include

#include

#include

int set_opt(int fd,int nspeed, int nbits, char nevent, int nstop);

int open_port(int fd,int comport);

unsigned char uart_cmd(unsigned char *buffer,int tx, int rx);

#endif

/#include "com.h"

//使用開發板的con2(con0,con1,con2)

int set_opt(int fd,int nspeed, int nbits, char nevent, int nstop)

bzero( &newtio, sizeof( newtio ) );

newtio.c_cflag |= clocal | cread;

newtio.c_cflag &= ~csize;

newtio.c_oflag &= ~(onlcr | ocrnl); //

newtio.c_iflag &= ~(ixon | ixoff | ixany);    //

switch( nbits )

switch( nevent )

switch( nspeed )

if( nstop == 1 )

newtio.c_cflag &= ~cstopb;

else if ( nstop == 2 )

newtio.c_cflag |= cstopb;

newtio.c_cc[vtime] =5; //測試時該大一點

newtio.c_cc[vmin] = 0;//set min read byte!

tcflush(fd,tciflush);

if((tcsetattr(fd,tcsanow,&newtio))!=0)

printf("set done!\n");

return 0;

}int open_port(int fd,int comport)

;long vdisable;

if (comport==1)

else

printf("open ttysac0 .....\n");

}else if(comport==2)

else

printf("open ttysac1 .....\n");

}else if (comport==3)

else

printf("open ttysac2 .....\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;

}//我的應用函式

unsigned char uart_cmd(unsigned char *buffer,int tx, int rx)

if(( i=set_opt(fd,115200,8,'n',1) ) < 0)

if(( nwrite=write(fd,buffer,tx)) != tx)

printf("nwrite=%d\n",nwrite);

for(i=0;i

strncpy(buffer,mybuffer,rx+1);

close(fd);

return 0;

}

Linux下串列埠通訊

1.開啟串列埠 與其他的關於裝置程式設計的方法一樣,在linux下,操作 控制串列埠也是通過操作起裝置檔案進行的。在linux下,串列埠的裝置檔案是 dev ttys0或 dev ttys1等。因此要讀寫串列埠,我們首先要開啟串列埠 char dev dev ttys0 串列埠1 int fd op...

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然後在另一主機的輸出端檢視輸出...