LINUX 串口通訊源碼__LINUX

來源:互聯網
上載者:User

主要函數

int openport(char *Dev) //開啟串口

int setport(int fd, int baud,int databits,int stopbits,int parity)//設定串口,傳輸速率,資料位元,停止位,校正

int readport(int fd,char *buf,int len,int maxwaittime)//讀資料,參數為串口,BUF,長度,逾時時間

int writeport(int fd,char *buf,int len)  //發送資料

void clearport(int fd)      //如果出現資料與規約不符合,可以調用這個函數來重新整理串口讀寫資料

 如果有BUG,請大家及時回複給我,EMAIL:41063473@QQ.COM。

#include   <stdio.h>  
#include   <string.h>  
#include   <unistd.h>  
#include   <fcntl.h>  
#include   <errno.h>  
#include   <termios.h>  
#include   <sys/time.h>
int openport(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;

 }  
   
int setport(int fd, int baud,int databits,int stopbits,int parity)
{
 int baudrate;
 struct   termios   newtio;  
 switch(baud)
 {
 case 300:
  baudrate=B300;
  break;
 case 600:
  baudrate=B600;
  break;
 case 1200:
  baudrate=B1200;
  break;
 case 2400:
  baudrate=B2400;
  break;
 case 4800:
  baudrate=B4800;
  break;
 case 9600:
  baudrate=B9600;
  break;
 case 19200:
  baudrate=B19200;
  break;
 case 38400:
  baudrate=B38400;
  break;
 default :
  baudrate=B9600;  
  break;
 }
 tcgetattr(fd,&newtio);    
 bzero(&newtio,sizeof(newtio));  
   //setting   c_cflag
 newtio.c_cflag   &=~CSIZE;    
 switch (databits) /*設定資料位元數*/
 {  
 case 7:  
  newtio.c_cflag |= CS7; //7位元據位
  break;
 case 8:    
  newtio.c_cflag |= CS8; //8位元據位
  break;  
 default:   
  newtio.c_cflag |= CS8;
  break;    
 }
 switch (parity) //設定校正
 {  
 case 'n':
 case 'N':   
  newtio.c_cflag &= ~PARENB;   /* Clear parity enable */
  newtio.c_iflag &= ~INPCK;     /* Enable parity checking */
  break; 
 case 'o':  
 case 'O':    
  newtio.c_cflag |= (PARODD | PARENB); /* 設定為奇效驗*/ 
  newtio.c_iflag |= INPCK;             /* Disnable parity checking */
  break; 
 case 'e': 
 case 'E':  
  newtio.c_cflag |= PARENB;     /* Enable parity */   
  newtio.c_cflag &= ~PARODD;   /* 轉換為偶效驗*/    
  newtio.c_iflag |= INPCK;       /* Disnable parity checking */
  break;
 case 'S':
 case 's':  /*as no parity*/  
     newtio.c_cflag &= ~PARENB;
  newtio.c_cflag &= ~CSTOPB;break; 
 default:  
  newtio.c_cflag &= ~PARENB;   /* Clear parity enable */
  newtio.c_iflag &= ~INPCK;     /* Enable parity checking */
  break;   
 }
 switch (stopbits)//設定停止位
 {  
 case 1:   
  newtio.c_cflag &= ~CSTOPB;  //1
  break; 
 case 2:   
  newtio.c_cflag |= CSTOPB;  //2
    break;
 default: 
  newtio.c_cflag &= ~CSTOPB; 
  break; 
 }
 newtio.c_cc[VTIME] = 0;   
 newtio.c_cc[VMIN] = 0;
 newtio.c_cflag   |=   (CLOCAL|CREAD);
 newtio.c_oflag|=OPOST;
 newtio.c_iflag   &=~(IXON|IXOFF|IXANY);                    
    cfsetispeed(&newtio,baudrate);  
    cfsetospeed(&newtio,baudrate);  
    tcflush(fd,   TCIFLUSH);
 if (tcsetattr(fd,TCSANOW,&newtio) != 0)  
 {
  perror("SetupSerial 3"); 
  return -1; 
 } 
 return 0;
}
int readport(int fd,char *buf,int len,int maxwaittime)//讀資料,參數為串口,BUF,長度,逾時時間
{
 int no=0;int rc;int rcnum=len;
 struct timeval tv;
 fd_set readfd;
 tv.tv_sec=maxwaittime/1000;    //SECOND
 tv.tv_usec=maxwaittime%1000*1000;  //USECOND
 FD_ZERO(&readfd);
 FD_SET(fd,&readfd);
 rc=select(fd+1,&readfd,NULL,NULL,&tv);
 if(rc>0)
 {
  while(len)
  {
   rc=read(fd,&buf[no],1);
   if(rc>0)
    no=no+1;
   len=len-1;   
  }
  if(no!=rcnum)
   return -1;      //如果收到的長度與期望長度不一樣,返回-1
  return rcnum;      //收到長度與期望長度一樣,返回長度
 }
 else
 {
  return -1;
 }
 return -1;
}
int writeport(int fd,char *buf,int len)  //發送資料
{
 write(fd,buf,len);
}
void clearport(int fd)      //如果出現資料與規約不符合,可以調用這個函數來重新整理串口讀寫資料
{
 tcflush(fd,TCIOFLUSH);
}
main()  
{  
 int   fd,rc,i,ret;  
 unsigned char rbuf[256];
 unsigned char wbuf[256]="";
 for(i=0;i<256;i++)
  wbuf[i]=i;
 char *dev ="/dev/ttyS0";    //串口號 /dev/ttyS0  對應於串口1
    fd  =  openport(dev);     //開啟串口
 if(fd>0)
 {
  ret=setport(fd,4800,8,1,'o');  //設定串口,傳輸速率,資料位元,停止位,校正
  if(ret<0)
  {
   printf("Can't Set Serial Port!/n");
   exit(0);
  }
 }
 else
 {
  printf("Can't Open Serial Port!/n");
  exit(0);
 }
 while(1){ 
  rc=readport(fd,rbuf,5,500);   //讀取5個位元組,逾時時間為500毫秒
  if(rc>0)
  {
   writeport(fd,wbuf,rc);
   printf("recv:%d/n",rc);
   for(i=0;i<rc;i++)
   printf("%02x ",rbuf[i]);
   printf("/n");
  }
  else
   printf("recv none/n"); 
 }  
 close(fd);  
}  
 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.