Search code examples
androidcgalaxyat-commandmodem

Dealing with GSM modem by JNI in android


I'm developing a program that send at-command to GSM modem and I want to read the response.

Some programmers advice me to use atinout program in this link.

so I ported to android and simplify the code to this C code, because I don't want to use files (input and output):

JNIEXPORT jstring JNICALL Java_com_marakana_NativeLib_hello(JNIEnv * env, jobject obj)
{
FILE *modem;
char *line="AT\r";
bool success;
int res;

modem = fopen("/dev/smd0", "r+b");
if (modem == NULL) 
    return (*env)->NewStringUTF(env, "2fopen() failed: /dev/smd0\n");

res = fputs(line, modem);
if (res < 0)
return (*env)->NewStringUTF(env, ("5failed to send '%s' to modem res = " + res,line));  

line = fgets(buf, (int)sizeof(buf), modem);
if (line == NULL)
    return (*env)->NewStringUTF(env, "6EOF from modem\n");
strcpy(buf2, line);
strip_cr(buf2);

res = fclose(modem);
if (res != 0) 
    return (*env)->NewStringUTF(env,  ("9closing modem failed: %s\n", strerror(errno)));

return (*env)->NewStringUTF(env, buf2);
}

static void strip_cr(char *s)
{
char *from, *to;
from = to = s;
while (*from != '\0') {
    if (*from == '\r') {
        from++;
        continue;
    }
    *to++ = *from++;
}
*to = '\0';
}

The problem is: The modem response is the same request. That mean I received AT from the function.

note: my mobile is Samsung Galaxy Win Duos.

Is the problem in the modem of my mobile. or in my code? how can I get the response of at command?


Solution

  • You have simplified too much; you MUST keep the logic that reads and parses everything that the modem sends back. After sending any AT command line you should continue processing responses (and do nothing else!) until you get a Final result code, e.g. OK, ERROR, +CME ERROR: ... etc.

    As for the getting echo of the command line this is normal modem behaviour, although you can turn off echo with ATE0, see "5.2.3 Command line echo" in V.250. That specification is the most fundamental AT command document that anyone working with AT commands benefits from reading. Read all of chapter 5 and then look into specific command if applicable in chapter 6.