使用C在Linux中通过串行端口从sim读取SMS消息 [英] Reading SMS messages from sim via serial port in Linux using C
本文介绍了使用C在Linux中通过串行端口从sim读取SMS消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图通过我已经放置在华为3G USB调制解调器内的SIM卡通过Linux A中的串行端口读取SMS消息.在屏幕上显示某些短信之前,我必须执行脚本多次.有时会显示不寻常的字符.我要做的就是使用AT指令,c和串行端口从sim读取短信.以下是我正在使用的代码.
Hi,
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.
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <termios.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <errno.h>
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;
}
推荐答案
您没有检查返回的消息以过滤出任何控制字符或验证收到的消息是否完整.查看此文档 [ ^ ],以及Google可能为您找到的任何其他资源.
You are not checking the message returned to filter out any control characters or verify that your received message is complete. Check this document[^], and any other resources that Google may find for you.
这篇关于使用C在Linux中通过串行端口从sim读取SMS消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文