基於OK6410的串口數據收發

轉自http://blog.csdn.net/pzhsunxu/article/details/8257939

***************************
 *基於OK6410的串口程序
 *2012.12.4
 *孫旭
 ***************************/

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<termios.h>
#include<string.h>

/*串口設置,"串口,波特率,數據位,奇偶校驗,停止位"*/
int set_Baudrate(int fd,int nSpeed, int nBits, char nEvent, int 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;
 case 460800:
  cfsetispeed(&newtio, B460800);
  cfsetospeed(&newtio, B460800);
  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;
 }

 return 0;
}

/*數據解析*/
int analysis_data(char buf[100])
{
 printf("recv data %s \n",buf);
}

/*波特率爲115200串口0*/
int serial_port0(void)
{ 
 int fd_port;
 int return_value=0;
 int length_data=0;
 char buffer[100];    //從串口讀取數據的buffer
 memset(buf,0,sizeof(buf));

 //打開串口0
 fd_port = open("/dev/ttySAC0",O_RDWR);
 if(-1 == fd_port)
 {
  exit(1);
 }
  
 //串口,波特率,數據位,奇偶校驗,停止位
 return_value = set_Baudrate(fd_port,115200,8,'N',1);
 if(-1 == return_value)
 {
  exit(1);
 }

 //接收串口0的數據
 while(1)
 {
  length_data = read(fd_port,buf,1);  //數據的發送使用write(fd_port,buf,1); buf爲要發送的字符串
  if(-1 == length_data)
  {
   exit(1);
  }
  
  if(0 > analysis_data(buf))
   return -1;

  memset(buf,0,100);
 }

 close(fd_port);
 return 1;
}

/*波特率爲4800串口1*/
int serial_port1(void)
{ 
 int fd_port;
 int return_value=0;
 int length_data=0;
 char buffer[100];    //從串口讀取數據的buffer
 memset(buf,0,sizeof(buf));

 //打開串口1
 fd_port = open("/dev/ttySAC1",O_RDWR);
 if(-1 == fd_port)
 {
  return -1;
 }
  
 //串口,波特率,數據位,奇偶校驗,停止位
 return_value = set_Baudrate(fd_port,4800,8,'N',1);
 if(-1 == return_value)
 {
  return -1;
 }

 //接收串口1的數據
 while(1)
 {
  length_data = read(fd_port,buf,1); //數據的發送使用write(fd_port,buf,1);
  if(-1 == length_data)
  {
   exit(1);
  }
  
  if(0 > analysis_data(buf))
   return -1;

  memset(buf,0,100);
 }
 close(fd_port);
 return 1;
}

/*主函數*/
int main(void)
{
 if(0 > serial_port0())
 {
  exit(1);
 }

 if(0 > serial_port1())
 {
  exit(1);
 }
 return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章