【问题标题】:How to send a UDP packet to a GPRS module如何向 GPRS 模块发送 UDP 数据包
【发布时间】:2014-08-05 05:24:38
【问题描述】:

我使用一个叫 SIM340DZ 的 GSM/GPRS 模块,并使用AT commands 来管理模块。

我能够从 GPRS 模块的特定端口向远程计算机发送 UDP 数据包。现在,我想将 UDP 数据包从计算机传输到 GPRS 单元。但是,GPRS 单元有一个私有 IP 地址(例如 10.46.123.25),接入点名称(APN)是 internet.tele2.se

谁能解释我如何将 UDP 数据从(linux)计算机发送到 GPRS 单元?我需要知道哪些信息以及如何找到?

另外,如果你有AT commands的经验,如果你能解释一下我需要使用什么命令序列来配置模块在UDP监听模式下,我将不胜感激?

【问题讨论】:

    标签: c networking at-command gprs


    【解决方案1】:

    对于那些需要处理类似系统的人,我发布了您可以使用AT commands通过串口将UDP数据包发送到目标portIPaddress的代码。解释包含在代码的注释中:

    int Serial_Close(int *fd);
    int Serial_Send(int *fd, char *string);
    int Serial_Open(int* fd, char *serial_Name, speed_t baudrate);
    
    
    
    int main(int argc, char** argv)
    {
        int fd;
    
        Serial_Open(&fd, "/dev/ttyAPP0", B115200); //open the UART interface with 115200 boundrate, 8 1 N
        if(tcflush(fd, TCIOFLUSH) != 0)
        {
            exit(1);   //error
            fprintf(stderr, "tcflush error\n");
        }
    
        Serial_Send(&fd, "ATE0\r\n");   //ATE0 = echo mode(ATE) is off (0)
        sleep(1);
        Serial_Send(&fd, "AT+CGATT=1\r\n");
        sleep(1);
        Serial_Send(&fd, "AT+AT+CSTT=\"internet.tele2.se\"\r\n");  //here you define the name the APN
        sleep(1);
        Serial_Send(&fd, "AT+CIICR\r\n");
        sleep(1);
        Serial_Send(&fd, "AT+cipstart=\"UDP\",\"85.1.2.3\",\"20000\"\r\n"); //85.1.2.3 is the destination IP address, 20000 is the destination Port number
        sleep(1);
        Serial_Send(&fd, "AT+CIPSEND=5\r\n");
        sleep(1);
        Serial_Send(&fd, "12345\r\n"); //12345 is the message
        sleep(1);
        Serial_Send(&fd, "AT+CIPSHUT\r\n");
        sleep(1);
        Serial_Close(&fd);
    
        return 0;
    }
    
    
    int Serial_Open(int* fd, char *serial_Name, speed_t baudrate)
    {
    
        struct termios serCfg;
        memset(&serCfg, 0, sizeof(serCfg));
        if((*fd = open(serial_Name, O_RDWR)) < 0)
            return -1;
        else
            if(tcgetattr(*fd, &serCfg) != 0)
                return -1;
    
        cfsetispeed(&serCfg, baudrate);
        cfsetospeed(&serCfg, baudrate);
        cfmakeraw(&serCfg);
    
        if(tcsetattr(*fd, TCSANOW, &serCfg) != 0)
            return -1;
        return 0;
    }
    
    
    int Serial_Send(int *fd, char *string)
    {
        int len;
        char *buffer;
        int bytes_sent;
    
    
        len = strlen(string);
        if(len > 255)
            return -1;
        buffer = (char*)malloc((len+1)*sizeof(char));
        if(buffer == NULL)
            return -1;
        strcpy(buffer, string);
        buffer[len] = 0;
    
        bytes_sent = write(*fd, buffer, strlen(buffer));
        if(bytes_sent < 0)
        {
            close (*fd);
            return -1;
        }
        else
        {
            free(buffer);
            return 0;
        }
    }
    
    
    
    int Serial_Close(int *fd)
    {
            if(close (*fd) < 0)
                return -1;
            else
                return 0;
    }
    

    我希望它对某人有所帮助。

    【讨论】:

    • 一个小修正:AT+AT+CSTT 应该是AT+CSTT。另外,对于 SIM800L,直到我在 AT+CIICR 命令之后添加了一个 AT+CIFSR 命令之后它才起作用。
    猜你喜欢
    • 2014-03-22
    • 2011-12-16
    • 2020-01-20
    • 1970-01-01
    • 2013-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多