一个串口通信程序,适用于3g模块发送简单AT指令

源码如下:

tty_fd;int max(int a,int b){return a>b?a:b;}int set_opt(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;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;};}int open_port(int fd,int comport){(comport==0){//printf(“open /dev/ttyUSB0”);fd = open( “/dev/ttyUSB0”, O_RDWR|O_NOCTTY|O_NDELAY);if (-1 == fd){perror(“Can’t Open Serial Port”);return(-1);}else{// printf(“open ttyUSB0 …..\n”);}}else if(comport==1){ fd = open( “/dev/ttyUSB1”, O_RDWR|O_NOCTTY|O_NDELAY);if (-1 == fd){perror(“Can’t Open Serial Port”);return(-1);}else{// printf(“open ttyUSB1 …..\n”);}}else if (comport==2){fd = open( “/dev/ttyUSB2”, O_RDWR|O_NOCTTY|O_NDELAY);if (-1 == fd){perror(“Can’t Open Serial Port”);return(-1);}else{// printf(“open ttyUSB2 …..\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(int argc, char *argv[]){int maxfd;fd_set rd;int nread,i;char tmp_buff[8];char buff[1024];char send_buf[1024];int offset;char recvbuf[1024];struct timeval timeout={0,50000};int timeout_flg = 0;int recv_flg = 0;((tty_fd=open_port(tty_fd,1))<0){perror(“open_port error”);return 0;}if((i=set_opt(tty_fd,115200,8,’N’,1))<0){perror(“set_opt error”);return 0;}// printf(“fd=%d\n”,tty_fd);FD_ZERO(&rd);strcpy(send_buf,argv[1]);// printf(“uart >> “);send_buf[strlen(send_buf)] = ‘\r’;send_buf[strlen(send_buf) + 1] = ‘\n’;send_buf[strlen(send_buf) + 2] = 0;write(tty_fd, send_buf, strlen(send_buf));memset(send_buf,0,sizeof(send_buf));FD_SET(tty_fd, &rd);FD_SET(fileno(stdin), &rd);maxfd = max(fileno(stdin), tty_fd) + 1;if(select(maxfd, &rd, NULL,NULL,NULL) < 0) {perror(“select error\n”);}if(FD_ISSET(tty_fd, &rd)){while((nread = read(tty_fd, buff, sizeof(buff))) > 0){printf(“%s”, buff);memset(buff, 1 , sizeof(buff));}exit(1);}close (tty_fd);return 0; }

,每一发奋努力的背后,必有加倍的赏赐。

一个串口通信程序,适用于3g模块发送简单AT指令

相关文章:

你感兴趣的文章:

标签云: