-----------------------------------------------------------------------
Cross compiler:arm-linux-gcc-4.5.4
Linux kernel version:linux-3.0
Development board:fl2440
Author: Yuzhonghan <754270866@qq.com>
这里写的接收短信息是与串口连接,所以要不断读取串口的数据。这里可以接收到短信息的数字和英文字母,不支持中文。
Cross compiler:arm-linux-gcc-4.5.4
Linux kernel version:linux-3.0
Development board:fl2440
Author: Yuzhonghan <754270866@qq.com>
-----------------------------------------------------------------------
/*********************************************************************************
* Copyright: (C) 2016 Yuzhonghan<754270866@qq.com>
* All rights reserved.
*
* Filename: receive_message.c
* Description: This file
*
* Version: 1.0.0(08/04/2016)
* Author: Yuzhonghan <754270866@qq.com>
* ChangeLog: 1, Release initial version on "08/04/2016 09:44:09 PM"
*
********************************************************************************/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include<termios.h>
void serial_init(int fd)
{
struct termios options;
tcgetattr(fd, &options);
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~CSIZE;
options.c_cflag &= ~CRTSCTS;
options.c_cflag |= CS8;
options.c_cflag &= ~CSTOPB;
options.c_iflag |= IGNPAR;
options.c_oflag = 0;
options.c_lflag = 0;
cfsetispeed(&options, B115200);
cfsetospeed(&options, B115200);
tcsetattr(fd, TCSANOW, &options);
}
int main(int argc,char **argv)
{
int fd;
char reply[16];
char buff[16];
int nread;
int nwrite;
fd=open("/dev/ttyS1", O_RDWR|O_NOCTTY|O_NDELAY);
if(fd<0)
{
perror("Can't open the serial port!\n");
}
serial_init(fd);
while(1)
{
memset(buff,0,16);
strcpy(buff,"at+cnmi=3,2");<span style="white-space:pre"> </span>//新短信息的提示接收
strcat(buff,"\r");
nwrite=write(fd,buff,strlen(buff));
printf("nwrite=%d,%s\n",nwrite,buff);
memset(reply,0,16);
sleep(1);
nread=read(fd,reply,sizeof(reply));
printf("nread=%d,%s\n",nread,reply);
}
}
这里写的接收短信息是与串口连接,所以要不断读取串口的数据。这里可以接收到短信息的数字和英文字母,不支持中文。
如有更多了解请去搜索AT指令集相关消息!