sim900芯片—GPRS模块接收短信应用程序

-----------------------------------------------------------------------
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指令集相关消息!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值