⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tcpclient_serial.c

📁 The source code example of ARM9 development board from Artila (M-501 starter kit). The source code c
💻 C
字号:
/* *	Description:  *		Serial-to-Ethernet example. * *		A TCP client is ready to connect remote server *		TTY Port is set to 19200, N81, no flow control. * *		After connection is established,  *		TCP client will send the received package to TTY port,  *		and TTY port will send the received serial data to ethernet. *		 *		If remote server close the connection, example will terminate. * *		Usage: ./test ttyPort hostIP tcpPort *		e.g. ./tcpclient_serial /dev/ttyS1 192.168.2.230 4000 *		 *		matrix's tty port: *		ttyS1, ttyS2, ttyS3, ttyS4  * *	History: *	Create 2006/02/25 */#include <stdio.h> #include <string.h> #include <unistd.h> #include <fcntl.h>#include <errno.h>#include <termios.h>#include <sys/types.h>#include <sys/socket.h>#include <arpa/inet.h>#include <netinet/in.h>#include <fcntl.h>struct termios T_old;struct sockaddr_in dest;int open_ttyPort(char *portName); 	/*open serial port*/int init_ttyPort(int sfd);		/*initialize serial port*/	int init_tcpclient(char *destIP, int port);		/*initialize tcp client*/int main(int argc, char *argv[]){	int	fd_tty, fd_client;	int	tcpPort, maxfd, ret, connection;	fd_set	rfd, wfd, efd; 	struct timeval	tm;		char buf[1024];		if ( argc != 4 ) {		printf("Usage: ./test ttyPort hostIP tcpPort\n");        	return -1;        }         tcpPort = atoi(argv[3]);	/*get tcp client remote port*/        if (tcpPort < 0) {        	printf("invalid tcp port");        	return -1;	}                fd_tty = open_ttyPort(argv[1]);		/*argv[1] contains tty port name that user input*/        if (fd_tty == -1)        	return -1;        	        if (init_ttyPort(fd_tty) == -1)        	return -1;        	                fd_client = init_tcpclient(argv[2], tcpPort); 	/*argv[2] contains remote host IP that user input*/        if (fd_client == -1)        	return -1;         		connection = -1;        /*set select time out*/        tm.tv_sec = 3;	tm.tv_usec = 0;        	printf("connect...\r\n");        while (1) {        	/*if connection is not established, call connect*/ 	        	if (connection == -1) {			connection = connect(fd_client, (struct sockaddr*)&dest, sizeof(dest));			if (connection == 0) {				printf("Connect OK!\r\n");			}        	}        	if (connection == -1)  /*connection is not established, continue to connect*/        		continue;        				FD_ZERO(&rfd);		FD_ZERO(&wfd);		FD_ZERO(&efd); 		FD_SET(fd_tty, &rfd);		FD_SET(fd_client, &rfd);		maxfd = (fd_tty > fd_client ? fd_tty : fd_client) + 1;		/*connection is established, call select to see if any data arrived*/				ret = select(maxfd, &rfd, &wfd, &efd, &tm);		if (ret < 0) {			printf("select fail\r\n");			break;		} else if (ret == 0) {			continue;	/*no data arrived, continue to call select*/		}		if (FD_ISSET(fd_tty, &rfd)) {	/*tty port has data to be read*/			ret = read(fd_tty, buf, sizeof(buf));			if (ret <= 0) {				printf("read tty port fail\r\n");				break;			}			if (send(fd_client, buf, ret, 0) < 0) {	/*send data to ethernet*/				printf("send to ethernet fail\r\n");				break;							}		}		if (FD_ISSET(fd_client, &rfd)) { /*tcp client has data to be read*/			ret = recv(fd_client, buf, sizeof(buf), 0);			if (ret < 0) {				printf("read from ethernet fail\r\n");				break;			} else if (ret == 0) {				printf("disconnect by remote\r\n");				break; //exit program			}			if (write(fd_tty, buf, ret) < 0) { /*send data to tty port*/				printf("write to tty fail\r\n");				break;							}					}	}	close(fd_client);	tcsetattr(fd_tty,TCSANOW,&T_old);	close(fd_tty);	printf("bye\r\n");	return -1;}intopen_ttyPort(char *portName){	int fd;	fd = open(portName, O_RDWR | O_NOCTTY | O_NDELAY);	if (fd == -1) {		perror("open_port: Unable to open - ");		return (fd);	}	fcntl(fd, F_SETFL, 0);	return (fd);}intinit_ttyPort(int sfd){	struct termios T_new;        if (tcgetattr(sfd,&T_old) != 0) {		printf("tcgetattr failed\n");		close(sfd);  		return -1;        }         T_new = T_old;        T_new.c_cflag = (B19200 | CS8 | CREAD | CLOCAL | HUPCL);        T_new.c_oflag = 0;        T_new.c_iflag = 0;        T_new.c_lflag = 0;        if (tcsetattr(sfd,TCSANOW,&T_new) != 0) {		printf("tcsetattr failed\n");		tcsetattr(sfd,TCSANOW,&T_old);		close(sfd);		return -1;         }         return 0; }intinit_tcpclient(char *destIP, int port){	int sockfd;		sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);	if (sockfd < 0) {  		printf("socket failed\n");  		return (-1);			}	bzero(&dest, sizeof(dest));	dest.sin_family = PF_INET;	dest.sin_port = htons(port);	inet_aton(destIP, &dest.sin_addr);	return (sockfd);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -