c - 如何向GPRS模块发送UDP数据包

标签 c networking at-command gprs

我使用名为 SIM340DZ 的 GSM/GPRS 模块,并使用 AT 命令 来管理该模块。

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

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

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

最佳答案

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

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;
}

我希望它对某人有帮助。

关于c - 如何向GPRS模块发送UDP数据包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22048489/

相关文章:

c - c中的多线程生活游戏

ios - iOS下的网状网络

windows - Linux 到 WinXP over UDP 滞后

c# - 玩家移动时与子弹碰撞 - Unity UNET

java - 如何使用 java comm 从 gsm 读取短信?

c - 尝试将 AT 命令写入 USB 转 OBDii 电缆,使用 FT232R 读取 ECU 数据 (ISO 9141-2)

c - 为什么 fwrite 和 fread 打印垃圾?

c - 在 connect 调用后使用 select

gsm - AT 命令 - USSD 仅返回 OK

编译时 sizeof 条件