1. 程式人生 > >PC機與FL2440的串列埠通訊程式設計

PC機與FL2440的串列埠通訊程式設計

#include     <stdio.h>      /*標準輸入輸出定義*/
#include     <stdlib.h>     /*標準函式庫定義*/
#include     <unistd.h>     /*Unix標準函式定義*/
#include     <sys/types.h>  /**/
#include     <sys/stat.h>   /**/
#include     <fcntl.h>      /*檔案控制定義*/
#include     <termios.h>    /*PPSIX終端控制定義*/
#include     <errno.h>      /*錯誤號定義*/
#include     <string.h>
#define TRUE    0
#define FALSE   -1

#define BUFFSIZE 1024

int open_dev(char *dev_name );
void set_serial_port_speed(int fd, int speed);
void Set_parityofport(int fd,int databits,int stopbits,char parity);

int main(void)
{  
    int  fd;
    int  read_size;
    struct timeval  timeout;
    char   buff[BUFFSIZE];
    pid_t   pid;
        

    fd = open_dev("/dev/ttyS1");
    set_serial_port_speed(fd, 115200);
    Set_ParityOfPort(fd, 8, 1, 'n');
	
/*子程序執行讀操作*/
if (0 == (pid = fork()))
    {
        char w_buff[BUFFSIZE];
        while (1)
        {
            scanf("%s",w_buff);
           /*write為不帶緩衝的IO函式*/
            if (0 > write(fd, w_buff, strlen(w_buff)))
            {
                perror("Write Serial Fail:");
                exit(-1);
            }
            memset(w_buff, 0x00, BUFFSIZE);
        }

    }

	/*父程序執行寫操作*/
    while (1)
    {
        memset(buff, 0x00, sizeof(buff));
        if (0 > (read_size = read(fd, buff, BUFFSIZE)))
        {
            perror("Read Serial Fail:");
            exit(-1);
        }
        if (read_size > 0)
            printf("%s", buff);
        fflush(stdout);  
    }
   
	close(fd);
	return 0;
}

以上是標頭檔案,巨集定義,函式宣告以及主函式

以下是各個子函式的實現

/********************************************************
 *                開啟串列埠
 *******************************************************/
 int open_dev(char *dev_name)
 {   
	 int fd;
	 if((fd = open(dev_name, O_RDWR | O_NOCTTY | O_NONBLOCK)) < 0)
	 {
		 perror("open error");
		 return FALSE;
	 }
	 return fd;
 }


/***********************************************************
 *              設定波特率
 **********************************************************/
void Set_Serial_Port_Speed(int fd, int speed)
{
        struct termios  term;

	if(tcgetattr(fd, &term) != 0)
	{
	   perror("tcgetattr error");

	}
   
    tcflush(fd, TCIOFLUSH);
    switch(speed)
{ 
    
    case 2400:
          cfsetispeed(&term, B2400);//輸入波特率
          cfsetospeed(&term, B2400);//輸出波特率
          break;
      case 4800:
          cfsetispeed(&term, B4800);
          cfsetospeed(&term, B4800);
          break;
      case 9600:
          cfsetispeed(&term, B9600);
          cfsetospeed(&term, B9600);
          break;
      case 57600:
          cfsetispeed(&term, B57600);
          cfsetospeed(&term, B57600);
          break;
      case 115200:
          cfsetispeed(&term, B115200);
          cfsetospeed(&term, B115200);
      default:
          cfsetispeed(&term, B115200);
          cfsetospeed(&term, B115200);
          break;
}
	if(tcsetattr(fd, TCSANOW, &term) != 0)
	{
		perror("tcsetattr error");
	}
}


/******************************************************************
 *           設定奇偶校驗

*****************************************************************/           
void Set_ParityOfPort(int fd,int databits,int stopbits,char parity)
{
    struct termios options;
    if(tcgetattr(fd,&options) != 0)
	{
		perror("tcgetattr error");
	
	}
        // 不遮蔽字元大小
	options.c_cflag &= ~CSIZE;
        //非規範輸入,不進行回送,非可見擦除符,不啟用終端產生的訊號
	options.c_lflag  &= ~(ICANON | ECHO | ECHOE | ISIG);
        //不進行輸出處理
	options.c_oflag  &= ~OPOST;

	switch (databits) /*設定資料位數*/
	{
	  case 7:  
		options.c_cflag |= CS7;
   		break;
           case 8:
		options.c_cflag |= CS8;
	        break;
	   default: 
		fprintf(stderr,"Unsupported data size/n");
	

	}

   
   switch (parity)
   {
	   case 'n':
	   case 'N':
	                  options.c_cflag &= ~PARENB; 
			  options.c_iflag &= ~INPCK; 
			  break;
           case 'o':
	   case 'O':
	                  options.c_cflag |= (PARODD | PARENB);
			  options.c_iflag |= INPCK;
			  break;
	   case 'e':
           case 'E':
		          options.c_cflag |= PARENB; 
			  options.c_cflag &= ~PARODD;
			  options.c_iflag |= INPCK; 
			  break;
	    case 's': 
	    case 'S':
		           options.c_cflag &= ~PARENB;
			   options.c_cflag &= ~CSTOPB; 
			   break; 
	    default:
		           fprintf(stderr,"Unsupported parity/n"); 
	
   }


   switch (stopbits)
   {
	   case 1: 
	                  options.c_cflag &= ~CSTOPB; 
			  break;
	   case 2:
	                  options.c_cflag |= CSTOPB; 
			  break;
	   default:
	                  fprintf(stderr,"Unsupported stop bits/n"); 
	
   }
   /*開啟奇偶校驗*/
   if (parity != 'n')
	   options.c_iflag |= INPCK;
	
   tcflush(fd,TCIFLUSH);
   /*read 立即返回[0,nbytes]*/
   options.c_cc[VTIME] = 0;
   options.c_cc[VMIN] = 0;

   if (tcsetattr(fd,TCSANOW,&options) != 0) 
   {
      perror("tcsetattr error"); 
	 
   }
   
}

將c程式碼在交叉編譯器以編譯,生成a.out檔案

然後將a.out檔案下載到FL2440開發板上執行就可以了!