串口通讯

来源:互联网 发布:加入网络水军 编辑:程序博客网 时间:2024/04/30 13:08

源程序:

#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     <time.h>#include     <memory.h> #define BAUDRATE B9600#define DEVICE "/dev/ttyS0"/*打开串口*/int OpenDev(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 set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop){struct termios newtio,oldtio;        /*保存原先配置,以测试配置是否正确、该串口是否可用等。调试成功,函数返回0,失败,函数返回-1*/if  ( tcgetattr( fd,&oldtio)  !=  0) { perror("SetupSerial 1");return -1;}//bzero( &newtio, sizeof( newtio ) );        memset(&newtio,0,sizeof(newtio));        /*忽略一切调制解调器状态行,激活字符接受回执功能,CLOCAL用于本地链接,CREAD用于接受使能*/newtio.c_cflag  |=  CLOCAL | CREAD;        /*设置字符大小,没有现成可用函数,需要位掩码。一般先去除数据位中的位掩码,再重新按要求设置。*/newtio.c_cflag &= ~CSIZE;switch( nBits ){case 7:  /*在发送和接收字符时使用7个二进制位*/newtio.c_cflag |= CS7;break;case 8: /*在发送和接收字符时使用8个二进制位*/newtio.c_cflag |= CS8;break;}        /*设置奇偶校验位*/switch( nEvent ){case 'O':/*使能奇校验*/newtio.c_cflag |= PARENB;//激活校验码的生成和检测功能newtio.c_cflag |= PARODD;//使用奇校验而不是偶校验newtio.c_iflag |= (INPCK | ISTRIP);//INPCK对接收到的字符进行奇偶校验,ISTRIP把所有接收到的字符都设为7位二进制位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:/*成功时返回0,失败-1*,进出波特率一样*/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;}        /*设置停止位,CSTOPB每个字符使用两个停止位而不是一个*/if( nStop == 1 )newtio.c_cflag &=  ~CSTOPB;//使用一个停止位else if ( nStop == 2 )newtio.c_cflag |=  CSTOPB;        /*设置等待时间和最小接收字符*/newtio.c_cc[VTIME] =0;newtio.c_cc[VMIN] =100;        /*处理要写入的引用对象,刷新收到的数据不读取*/tcflush(fd,TCIFLUSH);        /*激活配置,激活的配置立即生效*/if((tcsetattr(fd,TCSANOW,&newtio))!=0){perror("com set error");return -1;}printf("set done!\n");return 0;}int main(){  int fd1,fd2,nread,nset;  char buf[100];  char buf1[1];  fd1=open("/dev/ttyUSB0",O_RDWR);  if(fd1==-1)  {     printf("Open serial fasiled!\n");     exit(1);   }  else   {   printf("Open serial successful!\n");                }  fd2=open("1.txt",O_RDWR);        if(fd2==-1)               {                printf("Open 1.txt failed!\n");                exit(1);                }  nset=set_opt(fd1,9600,8,'N',1);if(nset==-1)exit(1);   while(1){   nread=read(fd1,buf,100);   printf("字节个数为%d \n",nread);   printf("读取字节为%s \n",buf);      if(nread>0)write(fd2,buf,nread);    }    close(fd1);   close(fd2);   return 0;}



原创粉丝点击