Search code examples
cnetworkingat-commandgprs

How to send a UDP packet to a GPRS module


I use a GSM/GPRS module called SIM340DZ, and use AT commands to be able manage the module.

I am able to send a UDP packet to a remote computer at a specific port from the GPRS module. Now, I would like to transfer a UDP packet from computer to the GPRS unit. But, the GPRS unit has a private IP address (e.g. 10.46.123.25), and the access point name (APN) is internet.tele2.se

Could anyone please explain how I can send a UDP data from a (linux) computer to the GPRS unit? What information do I need to know and how can I find it out?

Additionally, if you have experience in AT commands, I would also appreciate if you could explain me what command sequence do I need to use to configure the module on UDP listening mode?


Solution

  • For the ones who needs to deal with similar system, I am posting the code that you can send UDP packets to a destinated port and IPaddress through the serial port by using AT commands. Explanations are included as comment on the code:

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

    I hope it helps to someone.