串口备份
来源:互联网 发布:linux 线程数过多 编辑:程序博客网 时间:2024/05/23 13:22
串口备份
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <dlfcn.h>#include <mysql/mysql.h>#include "uart.h"#define BUF_SIZE 1024 int main(void) { int fd,res; char buf[BUF_SIZE]; char buff[BUF_SIZE]; fd_set inset,tempset; struct timeval tv; FD_ZERO(&inset); //1. 打开串口 if((fd = open_uart(1)) < 0) { perror("open port"); return -1; } if(set_uart_config(fd,115200,8,'N',1) < 0) { perror("set com_config"); return -1; } FD_SET(fd,&inset); tv.tv_sec = 5; tv.tv_usec = 0; do{ tempset = inset; #if 0 printf("Please input some data(quit to exit)\n"); memset(buf,0,BUF_SIZE); if(fgets(buf,BUF_SIZE,stdin) == NULL) { perror("input error"); break; } write(fd,buf,strlen(buf)); #endif memset(buff,0,BUF_SIZE); tv.tv_sec = 5; tv.tv_usec = 0; res = select(fd + 1,&tempset,NULL,NULL,&tv); if(res == 0) write(fd,"是收到水电费水电费\r\n",strlen("是收到水电费水电费\r\n")); //printf("time out 怎么办 \n"); else if (res < 0) printf("select error\n"); else { if(FD_ISSET(fd,&inset)) //if(1) { int rx_length = read(fd,buff,BUF_SIZE); if(rx_length > 0){ buff[rx_length] = '\0'; //write(fd,buff,BUF_SIZE); printf("%d from uart:%s\r\n",rx_length,buff); } } } }while(1 ); close(fd); return 0; } #if 0#include <stdio.h>#include <unistd.h>#include <fcntl.h>#include <termios.h>int main(){ int fd = -1; fd = open("/dev/ttyS1", O_RDWR | O_NOCTTY | O_NDELAY); if (fd == -1) { perror("Open Serial Port Error!\n"); return -1; } struct termios options; tcgetattr(fd, &options); //115200, 8N1 options.c_cflag = B115200 | CS8 | CLOCAL | CREAD; options.c_iflag = IGNPAR; options.c_oflag = 0; options.c_lflag = 0; options.c_cc[VTIME]=0; options.c_cc[VMIN]=1; tcflush(fd, TCIFLUSH); tcsetattr(fd, TCSANOW, &options); unsigned char rx_buffer[256]; while(1){ int rx_length = read(fd, (void*)rx_buffer, 255); if (rx_length > 0) { //Bytes received rx_buffer[rx_length] = '\0'; printf("%i bytes read : %s\n", rx_length, rx_buffer); write(fd,"是收到水电费水电费\r\n",strlen("是收到水电费水电费\r\n")); //tcflush(fd, TCIFLUSH); } } close(fd); return 0;}#endif
0 0