嵌入式开发记录-day55 嵌入式智能网关

1、智能网关概念

  在智能网关产品中搜到的一句话:

智能网关具备智能家居控制枢纽及无线路由两大功能,一侧负责具体的安防报警,家电控制,用电信息采集。通过无线方式与智能交互终端等产品进行数据交互。它还具备有无线路由功能,优良的无线性能,网络安全和覆盖面积,智能网关是您无线家庭网络的理想选择
View Code

  在这里再做一些补充:

   1、智能网关与传统的网关不一样;

   2、嵌入式中的网关:将一种协议转换为另一种协议,实现数据转发而不作控制;

    协议转换,一般是由于智能终端通过以太网控制智能设备,智能网关需要将TCP/IP协议转换为智能设备对应的通信协议;实现数据转换:对传过来的数据包

    进行分组转发;

    3、智能网关与普通网关:智能网关实现数据处理:提取数据包中有效的数据信息;

    1、将一种协议转换为另外一种协议、实现数据转发、不做控制、能够实现数据处理的设备。

2、智能网关的软件框架

  1、模拟智能网关的组成结构:

  

  在Ubuntu上搭建一个服务器,通过以太网给4412发送数据,在串口助手上显示数据;同样的,Ubuntu上只做数据显示;不做数据处理

  由于使用socket,所以Ubuntu与开发板之间网络需要能够ping通;串口使用另一个;

  2、智能网关= 网络通信socket + 串口通信 + 多进程fork()

  3、客户端伪代码

pid = fork();
    if(pid){
        while(1){
            if(接收到网口数据)
                通过串口讲数据发送
        }
    }
    else if(pid){
        while(1){
            if(接收到串口数据)
                通过网口将数据送出
        }
    }
View Code

  4、服务端伪代码:

pid = fork()
    if(pid){
        while(1){
            if(接收到网口数据)
                打印
        }
    }
    else if(pid){
        while(1){
            通过网口将数据发送到开发板
            delay 1S
        }
    }
View Code

3、程序分析

  客户端

// client.c 客户端代码
#include <stdlib.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>

extern int net_int(char *client_ip,unsigned short portvalue);
extern int set_opt(int,int,int,char,int);
extern int uart_check(char *uart,char *uart_buffer);

int main(int argc, char **argv)
{
    int cfd;
    int recbyte,nByte;
    char buffer[1024] = {0};
    pid_t pid;
    unsigned short portnum = 0x8888;
    int fd,wr_static,i=10;
    char *uart3 = "/dev/ttySAC3";
    char *uart_buffer = "hello world!\r\n";
    
    printf("Hello,welcome to client!\r\n");

    if(argc == 3){
        portnum = atoi(argv[2]);
        printf("portnum is 0x%x !\n",portnum);
    }
    
    if(argc < 2){
        printf("usage: echo ip\n");
        return -1;
    }
    if((cfd = net_int(argv[1],portnum))==0){
        return -1;
    }
    if((fd = uart_check(uart3,uart_buffer))==-1){
        return -1;
    }
    
    pid = fork();
    if(pid == -1){
        printf("fork failed\n");
        return 1;
    }
    else if(pid){
        while(1){
            if(-1 == (recbyte = read(cfd, buffer, 1024))){
                printf("read data fail !\r\n");
                return -1;
            }
            buffer[recbyte]='\0';
            printf("%s\r\n",buffer);
            
            if(strlen(buffer)>0){
                wr_static = write(fd,buffer, strlen(buffer));
                memset(buffer, 0, 1024);
            }
            usleep(50);
        }
    }
    else{
        while(1){
            while((nByte = read(fd, buffer, 512))>0){
                write(fd,buffer,nByte);
                printf("read uart1 data %s!\n",buffer);
                
                buffer[nByte]='\0';
                printf("%s\r\n",buffer);
            
                send(cfd, buffer, nByte, 0);
                nByte = 0;    
            }
            usleep(50);
        }
    }
    
    close(cfd);
    close(fd);
    
    return 0;
}
View Code

  服务端

// server,c 服务端代码配置
#include <stdlib.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>

int main(int argc, char **argv)
{
    pid_t pid;
    int sfp, nfp, num = 0;
    struct sockaddr_in s_add,c_add;
    int sin_size,recbyte;
    unsigned short portnum=0x8888;
    char buffer[100] = {0};  
    
    printf("Hello,welcome to my server !\r\n");
    
    if(argc == 2){
        portnum = atoi(argv[1]);
        printf("portnum is 0x%x !\n",portnum);
    }
    
    sfp = socket(AF_INET, SOCK_STREAM, 0);
    if(-1 == sfp){
        printf("socket fail ! \r\n");
        return -1;
    }
    
    int val = 1;  
    setsockopt(sfp,SOL_SOCKET,SO_REUSEADDR,(char *)&val,sizeof(val));
    
    printf("socket ok !\r\n");

    bzero(&s_add,sizeof(struct sockaddr_in));
    s_add.sin_family=AF_INET;
    s_add.sin_addr.s_addr=htonl(INADDR_ANY);
    s_add.sin_port=htons(portnum);

    if(-1 == bind(sfp,(struct sockaddr *)(&s_add), sizeof(struct sockaddr))){
        printf("bind fail !\r\n");
        return -1;
    }

    printf("bind ok !\r\n");

    if(-1 == listen(sfp,5)){
        printf("listen fail !\r\n");
        return -1;
    }
    printf("listen ok\r\n");

    sin_size = sizeof(struct sockaddr_in);
    nfp = accept(sfp, (struct sockaddr *)(&c_add), &sin_size);
    if(-1 == nfp){
        printf("accept fail !\r\n");
        return -1;
    }

    printf("accept ok!\r\nServer start get connect from %#x : %#x\r\n",ntohl(c_add.sin_addr.s_addr), ntohs(c_add.sin_port));
    
    pid = fork();
    if(pid == -1){
        printf("fork failed\n");
        return 1;
    }
    else if(pid){
        while(1){
            scanf("%s",buffer);
            send(nfp, buffer, strlen(buffer), 0);
            usleep(5);
        }
    }
    else{
        while(1){
            if(-1 == (recbyte = read(nfp, buffer, 1024))){
                printf("read data fail !\r\n");
                return -1;
            }
            if(recbyte > 0){
                buffer[recbyte]='\0';
                printf("socket data is %s!\n",buffer);            
            }        
            memset(buffer, 0, recbyte);    
            usleep(5);
        }
    }
    
over:
    close(nfp);
    close(sfp);
    return 0;            
}
View Code

  串口初始化

// uart_int.c  串口初始化代码

#include <stdlib.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>

//串口初始化函数
int set_opt(int,int,int,char,int);
//串口测试函数
int uart_check(char *uart,char *uart_buffer);

//串口部分初始化
int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop)
{
    struct termios newtio,oldtio;
    if  ( tcgetattr( fd,&oldtio)  !=  0) { 
        perror("SetupSerial 1");
        return -1;
    }
    bzero( &newtio, sizeof( newtio ) );
    newtio.c_cflag  |=  CLOCAL | CREAD;
    newtio.c_cflag &= ~CSIZE;

    switch( nBits )
    {
    case 7:
        newtio.c_cflag |= CS7;
        break;
    case 8:
        newtio.c_cflag |= CS8;
        break;
    }

    switch( nEvent )
    {
    case 'O':
        newtio.c_cflag |= PARENB;
        newtio.c_cflag |= PARODD;
        newtio.c_iflag |= (INPCK | ISTRIP);
        break;
    case 'E': 
        newtio.c_iflag |= (INPCK | ISTRIP);
        newtio.c_cflag |= PARENB;
        newtio.c_cflag &= ~PARODD;
        break;
    case 'N':  
        newtio.c_cflag &= ~PARENB;
        break;
    }

    switch( nSpeed )
    {
    case 2400:
        cfsetispeed(&newtio, B2400);
        cfsetospeed(&newtio, B2400);
        break;
    case 4800:
        cfsetispeed(&newtio, B4800);
        cfsetospeed(&newtio, B4800);
        break;
    case 9600:
        cfsetispeed(&newtio, B9600);
        cfsetospeed(&newtio, B9600);
        break;
    case 115200:
        cfsetispeed(&newtio, B115200);
        cfsetospeed(&newtio, B115200);
        break;
    case 460800:
        cfsetispeed(&newtio, B460800);
        cfsetospeed(&newtio, B460800);
        break;
    default:
        cfsetispeed(&newtio, B9600);
        cfsetospeed(&newtio, B9600);
        break;
    }
    if( nStop == 1 )
        newtio.c_cflag &=  ~CSTOPB;
    else if ( nStop == 2 )
    newtio.c_cflag |=  CSTOPB;
    newtio.c_cc[VTIME]  = 0;
    newtio.c_cc[VMIN] = 0;
    tcflush(fd,TCIFLUSH);
    if((tcsetattr(fd,TCSANOW,&newtio))!=0)
    {
        perror("com set error");
        return -1;
    }
//    printf("set done!\n\r");
    return 0;
}

int uart_check(char *uart,char *uart_buffer){
    int fd;
    int wr_static;
    if((fd = open(uart, O_RDWR|O_NOCTTY|O_NDELAY))<0){
        printf("open %s is failed",uart);
        return -1;
    }
    else{
        set_opt(fd, 115200, 8, 'N', 1); 
        wr_static = write(fd,uart_buffer, strlen(uart_buffer));
    }
    return fd;
}
View Code

  网络初始化

// net_int()  网络初始化
#include <stdlib.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>

//网络初始化
int net_int(char *client_ip,unsigned short portvalue){
    int cfd;
    struct sockaddr_in s_add, c_add;
    unsigned short portnum = portvalue;
    
    cfd = socket(AF_INET, SOCK_STREAM, 0);
    if(-1 == cfd)
    {
        printf("socket fail ! \r\n");
        return -1;
    }
    //fcntl(cfd,F_SETFL,O_NONBLOCK);
    
    printf("socket ok !\r\n");

    bzero(&s_add,sizeof(struct sockaddr_in));
    s_add.sin_family=AF_INET;
    s_add.sin_addr.s_addr= inet_addr(client_ip);
    s_add.sin_port=htons(portnum);
    printf("s_addr = %#x ,port : %#x\r\n",s_add.sin_addr.s_addr,s_add.sin_port);

    if(-1 == connect(cfd,(struct sockaddr *)(&s_add), sizeof(struct sockaddr)))
    {
        printf("connect fail !\r\n");
        return -1;
    }

    printf("connect ok !\r\n");
    return cfd;
}
View Code

 

posted @ 2020-11-11 00:39  笑不出花的旦旦  阅读(405)  评论(0)    收藏  举报