c - 使用C在Linux中通过串口从SIM读取短信

标签 c linux

我试图通过linux中的串口从我放置在华为3g USB调制解调器中的SIM卡读取短信。在屏幕上显示一些短信之前,我必须多次执行该脚本。有时它会表现出不寻常的特征。我想做的就是使用 AT 命令、c 和串行端口从 sim 读取短信。下面是我正在使用的代码。

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

最佳答案

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

我要么选择 gnokii,就这么简单:

$ gnokii --getsms

或者至少查看 gnokii 源代码,因为您所描述的问题看起来肯定像同步或等待输出问题,并且他们几乎肯定已经为此提供了一个很好的且经过测试的解决方案。

关于c - 使用C在Linux中通过串口从SIM读取短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6572116/

相关文章:

c - 为什么导出的网络设备 xmit 调用导致内核崩溃?

c++ - Linux 上的 QT、VTK 项目未运行

php - 使用 PHP 从基于 Linux 的文件服务器上传/检索文件

php - 如何检查是否有正在运行的 wget 实例

c - 是否有使用像素着色滤镜的跨平台 C 库?

c - C 中 bool 表达式的解析和计算

c - .h 文件中声明的函数在哪里找到它的定义?

我可以在空字符串中复制一个字符串吗?

linux - 我在这个 lex 程序中遇到无法识别的规则错误

C - 三角程序在调用 main 时不打印任何输出