while (1) { nread = read(fd, buff, 20); nsend = write(sock,buff,nread); usleep(10000); } |
void *uart_send(void *data) { struct termios newtio; printf("enter send\n\r"); while (STOP == FALSE) { fd = open("/dev/ttyS0", O_RDWR); //| O_NOCTTY |O_NONBLOCK); if (fd < 0) { printf("cannot open ttyS0\n\r"); exit(-1); } tcgetattr(fd, &oldtio); /* save current modem settings */ newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;/*ctrol flag*/ newtio.c_iflag = IGNPAR | ICRNL; /*input flag*/ newtio.c_oflag &= ~OPOST; /*output flag*/ newtio.c_lflag = 0; newtio.c_cc[VMIN] = 1; newtio.c_cc[VTIME] = 0; /* now clean the modem line and activate the settings for modem */ tcflush(fd, TCIFLUSH); tcsetattr(fd, TCSANOW, &newtio);/*set attrib*/ char *c = "helloworld"; write(fd, c, 10); usleep(1000000); printf("send\n\r"); tcsetattr(fd, TCSANOW, &oldtio); close(fd); } return NULL; } |
void* uart_to_net(void* data) { int nread; int nsend; int file; char buff[512]; struct sockaddr_in remote_addr; printf("enter uart_to_net\n\r"); if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) { printf("error:cannot obtain socket descriptor\n"); return 0; } bzero(&remote_addr, sizeof(remote_addr)); remote_addr.sin_family = AF_INET; remote_addr.sin_port = htons(5000); remote_addr.sin_addr.s_addr = inet_addr("192.168.0.55"); if (connect(sock, (struct sockaddr *) &remote_addr, sizeof(struct sockaddr_in)) == -1) { printf("connect to remote addr failed\n\r"); exit(0); } else printf("connect to server ...\n\r"); while (STOP==FALSE) { nread = read(fd, buff, 20); nsend = write(sock,buff,nread); } close(sock); printf("end uart to net\n\r"); return NULL; } |
int main() { pthread_t th_kb,th_send; void *retval; char *dev = "/dev/ttyS0"; fd = OpenDev(dev); usleep(200000); tcgetattr(fd, &oldtio); /* save current modem settings */ set_speed(fd, 115200); if (set_Parity(fd, 8, 1, 'N') == FALSE) { printf("Set Parity Error\n"); exit(0); } pthread_create(&th_send,NULL,uart_to_net,0); pthread_create(&th_kb,NULL,kb,0); pthread_join(th_send,&retval); pthread_join(th_kb,&retval); return 0; } |
欢迎光临 Chinaunix (http://bbs.chinaunix.net/) | Powered by Discuz! X3.2 |