• 网口转串口


    client.py

    from socket import *
    
    HOST = '192.168.1.14' # or 'localhost'
    PORT = 8887
    BUFSIZ =1024
    ADDR = (HOST,PORT)
    
    tcpCliSock = socket(AF_INET,SOCK_STREAM)
    tcpCliSock.connect(ADDR)
    while True:
         data1 = input('>')
         #data = str(data)
         if not data1:
             break
         tcpCliSock.send(data1.encode())
         data1 = tcpCliSock.recv(BUFSIZ)
         if not data1:
             break
         print(data1.decode('utf-8'))
    tcpCliSock.close()
    

    com.py

    #! /usr/bin/env python
    
    #coding=utf-8
    
    import pty
    import os
    import select
    
    def mkpty():
        #
        master1, slave = pty.openpty()
        slaveName1 = os.ttyname(slave)
        master2, slave = pty.openpty()
        slaveName2 = os.ttyname(slave)
        print '/nslave device names: ', slaveName1, slaveName2
        return master1, master2
    
    if __name__ == "__main__":
    
        master1, master2 = mkpty()
        while True:        
            rl, wl, el = select.select([master1,master2], [], [], 1)
            for master in rl:
                data = os.read(master, 128)
                if master==master1:
                    print "master1 receive data: %d data:" % len(data)
                    print data
                else:
                    print "master2 receive data: %d data:" % len(data)
                    print data
    

    client.c

    #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>
     
    #define MYPORT  8887
    #define BUFFER_SIZE 1024
     
    int main()
    {
        ///定义sockfd
        int sock_cli = socket(AF_INET,SOCK_STREAM, 0);
     
        ///定义sockaddr_in
        struct sockaddr_in servaddr;
        memset(&servaddr, 0, sizeof(servaddr));
        servaddr.sin_family = AF_INET;
        servaddr.sin_port = htons(MYPORT);  ///服务器端口
        servaddr.sin_addr.s_addr = inet_addr("xxxxxx");  ///服务器ip
     
        ///连接服务器,成功返回0,错误返回-1
        if (connect(sock_cli, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
        {
            perror("connect");
            exit(1);
        }
     
        char sendbuf[BUFFER_SIZE];
        char recvbuf[BUFFER_SIZE];
        while (fgets(sendbuf, sizeof(sendbuf), stdin) != NULL)
        {
    //	int len = strlen(sendbuf);
    //	if(sendbuf[len-1] == '
    '){sendbuf[len--]=0;}
    	printf("send:%s",sendbuf);
            send(sock_cli, sendbuf, strlen(sendbuf),0); ///发送
            recv(sock_cli, recvbuf, strlen(recvbuf),0); ///接收
            fputs(recvbuf, stdout);
            memset(sendbuf, 0, sizeof(sendbuf));
            memset(recvbuf, 0, sizeof(recvbuf));
        }
     
        close(sock_cli);
        return 0;
    }
    

    server.c

    #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;
        server_sockaddr.sin_port = htons(MYPORT);
        server_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    
        fd = open(DEV_NAME, O_RDWR | O_NOCTTY);
    	if(fd < 0) 
    	{
    			perror(DEV_NAME);
    			return -1;
    	}
        printf("port is ready
    ");
        cfsetispeed(fd,115200);  
        cfsetospeed(fd,115200); 
        //bind,成功返回0,出错返回-1
        while(bind(server_sockfd,(struct sockaddr *)&server_sockaddr,sizeof(server_sockaddr))==-1)
        {
            
    	;
        }
    
        ///listen,成功返回0,出错返回-1
        if(listen(server_sockfd,QUEUE) == -1)
        {
            perror("listen error
    ");
    	return;
        }
    
        ///客户端套接字
        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
    ");
    	return;
        }
    
        while(1)
        {
            memset(buffer,0,sizeof(buffer));
            int len = recv(conn, buffer, sizeof(buffer),0);
    	if(len<=0)
            {
                printf("maybe disconnected... restart:
    ");
                break;
            }
    //	if(buffer[0]=='
    ')
    //	{
    //	    printf("end by user");
    //	    break;
    //	}
    	len = write(fd, buffer,len);
    	if (len < 0) {
    		printf("write data error 
    ");
    		break;
    	}
            send(conn,buffer, len, 0);
        }
        close(conn);
        close(server_sockfd);
    
    }
    void main()
    {
        while(1)
        {
            mainFunction();
        }
    }
    
    



    Le vent se lève! . . . il faut tenter de vivre!


    Le vent se lève! . . . il faut tenter de vivre!
  • 相关阅读:
    LeetCode
    LeetCode
    LeetCode
    LeetCode
    LeetCode
    LeetCode
    LeetCode
    LeetCode
    LeetCode
    LeetCode
  • 原文地址:https://www.cnblogs.com/bai2018/p/15308842.html
Copyright © 2020-2023  润新知