开发者

Reading SMS messages from sim via serial port in Linux using C

开发者 https://www.devze.com 2023-03-16 23:40 出处:网络
am trying to read sms messages via serial port in linux a from a sim card which i have placed inside a huawei 3g usb modem. i have to execute the script a number of time before some of the sms message

am trying to read sms messages via serial port in linux a from a sim card which i have placed inside a huawei 3g usb modem. i have to execute the script a number of time before some of the sms messages are displayed on the screen. At times it displays unusual characters. All i want to do is read sms messages from the sim using AT commands, c and serial port. Below is the code i am using.

int 开发者_高级运维main(){
int fd;
struct termios options;

/* open the port */
fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{ /* Could not open the port */
fprintf(stderr, "open_port: Unable to open /dev/ttyS1 - %s\n",strerror(errno));
}else{
printf("port opened\n");
}
fcntl(fd, F_SETFL, 0);

/* get the current options */
tcgetattr(fd, &options);

/* set raw input, 1 second timeout */
options.c_cflag |= (CLOCAL | CREAD);
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_oflag &= ~OPOST;
options.c_cc[VMIN] = 0;
options.c_cc[VTIME] = 10;

/* set the options */
tcsetattr(fd, TCSANOW, &options);

char buffer[400]; /* Input buffer */
char *bufptr; /* Current char in buffer */
int nbytes; /* Number of bytes read */
int tries; /* Number of tries so far */

for (tries = 0; tries < 1; tries ++)
{
/* send an AT command*/
if (write(fd, "AT+CMGL=\"ALL\"\r", strlen("AT+CMGL=\"ALL\"\r")) < 3){
printf("command sent\n");
continue;
}

/* read characters into our string buffer*/
bufptr = buffer;

nbytes = read(fd, bufptr, buffer + sizeof(buffer) - bufptr - 1);
printf("%s\n",bufptr);

char *p;

p = strstr(buffer, "tin");
printf("%s",p);

p = strstr(buffer, "server");
if(p == NULL) printf("not from server\n");

*bufptr = '\0';

}
return 0;
}


Appearently Gnokii project supports Huawei devices -- http://wiki.gnokii.org/index.php/Huawei

I'd either go with gnokii, it's this simple:

$ gnokii --getsms

or at least look into gnokii sources, because the problem you're describing looks certainly like a synchronization or waiting-for-output problem and they almost certainly already have a nice and tested solution for that.


First of all, you need to be checking the return value of read. Basically read is never guaranteed to give you what you want. It might return early with nothing read due to your timeout or being interrupted by a signal (although you don't have any signal handlers so that's not an issue here) or give you just a partial read. You need to use the return value to advance your buffer pointer and read more until you determine you've read all the data you're looking for.

Aside from that, you should probably not just look at fixed text in an SMS to confirm the sender's id. I would sign the SMS with public key cryptography if you need to know it really came from the server you think sent it...

0

精彩评论

暂无评论...
验证码 换一张
取 消