1. 程式人生 > >linux下的串列埠通訊

linux下的串列埠通訊

2> 把串列埠線的一端短接(用金屬把2,3號腳連通),用萬用表測另一端的2,3號如果正常的話會有嘀嘀的短接報警聲。


二、linux下串列埠的基本操作

1、串列埠的操作

1.1開啟:fd = open("/dev/ttySAC1", O_RDWR | O_NOCTTY | O_NDELAY);
              O_RDWR 讀寫方式開啟;
              O_NOCTTY 不允許程序管理串列埠(不太理解,一般都選上);
              O_NDELAY 非阻塞(預設為阻塞,開啟後也可以使用fcntl()重新設定)

1.2寫入:n = write(fd, "linux", 5);
                n實際寫入位元組數;

1.3讀取:res = read(fd,buf,len);
                 res 讀取的位元組數;

1.4設定:fcntl(fd, F_SETFL, FNDELAY); //非阻塞
                 fcntl(fd, F_SETFL, 0); // 阻塞

1.5關閉:close(fd);

2、串列埠配置

struct termios options;  // 串列埠配置結構體
tcgetattr(fd,&options); //獲取當前設定
bzero(&options,sizeof(options));
options.c_cflag  |= B115200 | CLOCAL | CREAD; // 設定波特率,本地連線,接收使能
options.c_cflag &= ~CSIZE; //遮蔽資料位
options.c_cflag  |= CS8; // 資料位為 8 ,CS7 for 7
options.c_cflag &= ~CSTOPB; // 一位停止位, 兩位停止為 |= CSTOPB
options.c_cflag &= ~PARENB; // 無校驗
 //options.c_cflag |= PARENB; //有校驗
//options.c_cflag &= ~PARODD // 偶校驗
//options.c_cflag |=  PARODD    // 奇校驗
options.c_cc[VTIME] = 0; // 等待時間,單位百毫秒 (讀)。後有詳細說明
options.c_cc[VMIN] = 0; // 最小位元組數 (讀)。後有詳細說明
tcflush(fd, TCIOFLUSH); // TCIFLUSH刷清輸入佇列。
                                       TCOFLUSH刷清輸出佇列。 
                                       TCIOFLUSH刷清輸入、輸出佇列。
tcsetattr(fd, TCSANOW, &options); // TCSANOW立即生效;
                                                        TCSADRAIN:Wait until everything has been transmitted;
                                                        TCSAFLUSH:Flush input and output buffers and make the change

3、VTIME 和  VMIN

VTIME  定義要求等待的零到幾百毫秒的值(通常是一個8位的unsigned char變數)。
VMIN 定義了要求等待的最小位元組數, 這個位元組數可能是0。
只有設定為阻塞時這兩個引數才有效,僅針對於讀操作。
說起來比較複雜,舉個例子吧,設定為阻塞狀態,寫操作未進行實驗,這裡僅討論讀操作,
read(fd,&buf,8); // 讀串列埠

3.1
options.c_cc[VTIME] = 0;
options.c_cc[VMIN] = 0;
VMIN = 0,當緩衝區位元組數 >= 0 時進行讀操作,實際上這時讀串列埠操作並未被阻塞,因為條件始終被滿足。

3.2
options.c_cc[VTIME] = 0;
options.c_cc[VMIN] = 1;
VMIN = 1,當緩衝區位元組數 >= 1 時進行讀操作,當沒有資料時讀串列埠操作被阻塞。

3.3
options.c_cc[VTIME] = 0;
options.c_cc[VMIN] = 4;
VMIN = 4,當緩衝區位元組數 >= 4 時進行讀操作,否則讀串列埠操作被阻塞。每次讀出的最大位元組數由read函式中第三個引數決定。直到緩衝區剩下的資料< read 第三個引數 並且< 4 (如果這時read第三引數為 1 則進行4次讀操作直至讀完緩衝區,如read第三引數為2,連續進行讀操作,直至緩衝區空或還剩一個字元)。沒有設定VTIME,剩下的字元沒有確定的期限,直到下次滿足讀條件的時候才被讀出。

----------------------------------考慮VTIME-----------------------------

3.4
options.c_cc[VTIME] = 10; //單位百毫秒
options.c_cc[VMIN] = 4;
同3.3的區別就是,沒滿足條件或讀緩衝區中剩下的資料會在1秒(10百毫秒)後讀出。另外特別注意的是當設定VTIME後,如果read第三個引數小於VMIN ,將會將VMIN 修改為read的第三個引數,即使用read(fd,&buf,2);,以上設定變為:
options.c_cc[VTIME] = 10;
options.c_cc[VMIN] = 2;

==================================================================================================================================

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 <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開啟檔案 speed設定波特率 bit資料位設定   neent奇偶校驗位 stop停止位 */
    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)
{
/* fd 開啟串列埠 comport表示第幾個串列埠 */
    char *dev[]={"/dev/ttyS0","/dev/ttyS1","/dev/ttyS2"};
    long vdisable;
    if (comport==1)
    {    fd = open( "/dev/ttyS0", 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);
//    fd=3;
    nread=read(fd,buff,8);
    printf("nread=%d,%s\n",nread,buff);
    close(fd);
    return;

}