简体   繁体   English

使用 Linux 中的串口从 sim 中读取 SMS 消息,使用 C

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

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.我试图通过 linux 中的串行端口从我放在华为 3g usb 调制解调器中的 sim 卡读取短信。 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.我要做的就是使用 AT 命令、c 和串行端口从 sim 中读取短信。 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;
}

First of all, you need to be checking the return value of read .首先,您需要检查read的返回值。 Basically read is never guaranteed to give you what you want.基本上read永远不能保证给你你想要的。 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...如果你需要知道它真的来自你认为发送它的服务器,我会用公钥加密签署短信......

Appearently Gnokii project supports Huawei devices -- http://wiki.gnokii.org/index.php/Huawei显然 Gnokii 项目支持华为设备 -- http://wiki.gnokii.org/index.php/Huawei

I'd either go with gnokii, it's this simple:我要么 go 和 gnokii,就这么简单:

$ 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.或者至少查看 gnokii 源,因为您描述的问题看起来肯定像同步或等待输出问题,而且他们几乎肯定已经有一个很好的和经过测试的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM