Overview and environment
Using socket communication mode, establish a connection to the serial port at the server the acquired data calls
Server for the Linux, Windows or Linux clients to
During testing, please: use iptables -F, so as not to make people feel bad.
In addition, the first part of files in Linux can not run on a Windows system, it may cause some inconvenience.
socket communication
See: https://www.cnblogs.com/wuyepeng/p/9717236.html
To really achieve proper communication is still required in addition to modify. In the present embodiment the testing process, the output data sender at the receiving end will be more than two bytes in length. It believed to be caused at the sending end.
The client C is substantially the same test code:
1 #include <sys/types.h> 2 #include <sys/socket.h> 3 #include <stdio.h> 4 #include <netinet/in.h> 5 #include <arpa/inet.h> 6 #include <unistd.h> 7 #include <string.h> 8 #include <stdlib.h> 9 #include <fcntl.h> 10 #include <sys/shm.h> 11 12 #define MYPORT 8887 13 #define BUFFER_SIZE 1024 14 15 int main() 16 { 17 ///定义sockfd 18 is int sock_cli = Socket (AF_INET, SOCK_STREAM, 0 ); . 19 20 is /// defines the sockaddr_in 21 is struct the sockaddr_in servaddr; 22 is Memset (& servaddr, 0 , the sizeof (servaddr)); 23 is servaddr.sin_family = AF_INET; 24 servaddr.sin_port = the htons (MyPort); /// server port 25 servaddr.sin_addr.s_addr the inet_addr = ( " xxxxxx " ); /// server IP 26 is 27 /// connected to the server, a successful returns 0, an error is returned -1 28 IF (connect(sock_cli, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) 29 { 30 perror("connect"); 31 exit(1); 32 } 33 34 char sendbuf[BUFFER_SIZE]; 35 char recvbuf[BUFFER_SIZE]; 36 while (fgets(sendbuf, sizeof(sendbuf), stdin) != NULL) 37 { 38 // int len = strlen(sendbuf); 39 // if(sendbuf[len-1] == '\r'){sendbuf[len--]=0;} 40 printf("send:%s",sendbuf); 41 send(sock_cli, sendbuf, strlen(sendbuf),0); ///发送 42 recv(sock_cli, recvbuf, strlen(recvbuf),0); ///接收 43 fputs(recvbuf, stdout); 44 memset(sendbuf, 0, sizeof(sendbuf)); 45 memset(recvbuf, 0, sizeof(recvbuf)); 46 } 47 close(sock_cli); 48 return 0; 49 }
C server test code: there is a fatal flaw that the code is not corrected. After running on the client end of the knock several times Enter to display.
1 #include <sys/types.h> 2 #include <sys/socket.h> 3 #include <stdio.h> 4 #include <netinet/in.h> 5 #include <arpa/inet.h> 6 #include <unistd.h> 7 #include <string.h> 8 #include <stdlib.h> 9 #include <fcntl.h> 10 #include <sys/shm.h> 11 #include <asm/termios.h> 12 #define DEV_NAME "/dev/pts/1" 13 #define MYPORT 8887 14 #define QUEUE 20 15 #define BUFFER_SIZE 1024 16 17 int mainFunction() 18 { 19 int fd;//serial port 20 ///定义sockfd 21 int server_sockfd = socket(AF_INET,SOCK_STREAM, 0); 22 23 ///定义sockaddr_in 24 struct sockaddr_in server_sockaddr; 25 server_sockaddr.sin_family = AF_INET; 26 server_sockaddr.sin_port = htons(MYPORT); 27 server_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY); 28 29 fd = open(DEV_NAME, O_RDWR | O_NOCTTY); 30 if(fd < 0) 31 { 32 perror(DEV_NAME); 33 return -1; 34 } 35 printf("%d port is ready\n"); 36 ///bind,成功返回0,出错返回-1 37 while(bind(server_sockfd,(struct sockaddr *)&server_sockaddr,sizeof(server_sockaddr))==-1) 38 {; 39 } 40 41 ///the listen, successful return 0, an error return -1 42 is IF (the listen (server_sockfd, QUEUE) == - . 1 ) 43 is { 44 is perror ( " the listen error \ n- " ); 45 return ; 46 is } 47 48 /// client sets Sockets 49 char Buffer [BUFFER_SIZE]; 50 struct the sockaddr_in client_addr; 51 is the socklen_t length = the sizeof (client_addr); 52 is 53 is /// successful return non-negative descriptor, error return -1 54 is int Conn = Accept (server_sockfd, ( struct sockaddr*)&client_addr, &length); 55 if(conn<0) 56 { 57 perror("connect error\n"); 58 return; 59 } 60 61 while(1) 62 { 63 memset(buffer,0,sizeof(buffer)); 64 int len = recv(conn, buffer, sizeof(buffer),0); 65 if(len<=0) 66 { 67 printf("maybe disconnected... restart:\n"); 68 break; 69 } 70 fputs(buffer, stdout); 71 send(conn, strcat("send message success:",buffer), len, 0); 72 } 73 close(conn); 74 close(server_sockfd); 75 76 } 77 void main() 78 { 79 while(1) 80 { 81 mainFunction(); 82 } 83 }
python communicate See: https://blog.csdn.net/su_bao/article/details/80380465
python achieve communication more convenient. Link to read the information. The python client program, modify the parameters may communicate directly with the server socket. Communication process, the transmitting end and receiving the same number of bytes.
The code may need to enter data in python2 double quotes,
Serial Port Control
Serial section can be found in: ZLG [Tutorial] M6G2C & A6G2C; Software Development Guide Series core board.
The Guide, in Chapter 16, the serial port to send a detailed case. Which also detailed some of the functions of writing custom parameters, are available.
The following sample code is basically the same:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <unistd.h> 4 #include <fcntl.h> 5 #include <asm/termios.h> 6 #define DEV_NAME "/dev/pts/1" 7 int main (int argc, char *argv[]) 8 { 9 int fd; 10 int len, i,ret; 11 char buf[] = "hello ZLG!"; 12 13 fd = open(DEV_NAME, O_RDWR | O_NOCTTY); 14 if(fd < 0) { 15 perror(DEV_NAME); 16 return -1; 17 } 18 printf("%d",fd); 19 len = write(fd, buf, sizeof(buf)); 20 if (len < 0) { 21 printf("write data error \n"); 22 } 23 printf("%s", buf); 24 return(0); 25 }
Additional testing is required serial control, you need to use a virtual serial port.
Here a virtual serial port can be used to write the script python others, the establishment of interoperable two serial ports? : Https://blog.csdn.net/qq_42973043/article/details/82888075
Here some of the deletions may be made to the code, is substantially the same as follows:
1 import pty 2 import os 3 import select 4 5 def mkpty(): 6 # 7 master1, slave = pty.openpty() 8 slaveName1 = os.ttyname(slave) 9 master2, slave = pty.openpty() 10 slaveName2 = os.ttyname(slave) 11 print '/nslave device names: ', slaveName1, slaveName2 12 return master1, master2 13 14 if __name__ == "__main__": 15 16 master1, master2 = mkpty() 17 while True: 18 rl, wl, el = select.select([master1,master2], [], [], 1) 19 for master in rl: 20 data = os.read(master, 128) 21 if master==master1: 22 print "master1 receive data: %d data:" % len(data) 23 print data 24 else: 25 print "master2 receive data: %d data:" % len(data) 26 print data
The same testing methods.
Complex
Correcting section below, the portion removed some errors.
1 while(bind(server_sockfd,(struct sockaddr *)&server_sockaddr,sizeof(server_sockaddr))==-1) 2 { 3 4 ; 5 }
C language server complete code is as follows
#include <sys/types.h> #include <sys/socket.h> #include <stdio.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <string.h> #include <stdlib.h> #include <fcntl.h> #include <sys/shm.h> #include <asm/termios.h> #define DEV_NAME "/dev/pts/1" #define MYPORT 8887 #define QUEUE 20 #define BUFFER_SIZE 1024 int mainFunction() { int fd;//serial port ///定义sockfd int server_sockfd = socket(AF_INET,SOCK_STREAM, 0); ///定义sockaddr_in struct sockaddr_in server_sockaddr; server_sockaddr.sin_family = AF_INET; = the htons server_sockaddr.sin_port (MyPort); server_sockaddr.sin_addr.s_addr = htonl (INADDR_ANY); FD = Open (DEV_NAME, O_RDWR | the O_NOCTTY); IF (FD <0) { perror (DEV_NAME); return -1; } the printf ( "Port IS READY \ n-"); cfsetispeed (FD, 115200); cfsetospeed (FD, 115200); // the bind, the successful return 0, an error return -1 the while (the bind (server_sockfd, (struct the sockaddr *) & server_sockaddr, the sizeof ( server_sockaddr)) == -. 1) { ; } /// the listen, successful return 0, an error return -1 IF (the listen (server_sockfd, QUEUE) == -1) { perror ( "the listen error \ n-"); return; } /// client socket char buffer[BUFFER_SIZE]; struct sockaddr_in client_addr; socklen_t length = sizeof(client_addr); ///成功返回非负描述字,出错返回-1 int conn = accept(server_sockfd, (struct sockaddr*)&client_addr, &length); if(conn<0) { perror("connect error\n"); return; } while(1) { memset(buffer,0,sizeof(buffer)); int len = recv(conn, buffer, sizeof(buffer),0); if(len<=0) { printf("maybe disconnected... restart:\n"); break; } // if(buffer[0]=='\n') // { // printf("end by user"); // break; // } len = write(fd, buffer,len); if (len < 0) { printf("write data error \n"); break; } send(conn,buffer, len, 0); } close(conn); close(server_sockfd); } void main() { while(1) { mainFunction(); } }