基本功练习_3_2_之(GPRS.c)

/*************************************************************************
 *程序功能:
 *        发送中英文短信,拨打报警电话
 *
 *参数:   主程序向GPRS.c中的gprs(int) 传入因为什么而报警(火警或陌生人闯入)
 *        以决定发送火灾报警短信还是陌生人闯入时的报警短信
 *
 * ***********************************************************************/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <termio.h>
#include <errno.h>
#include "GPRS.h"

#define THIEF_INTO 1    //小偷进入
#define FIRE 2          //火灾发生
 /*
  *函数功能:设置串口属性
  *参数:  
        @fd:   串口文件描述符
  @nSpeed:   比特率
  @nBit:     有效位个数
  @nEvent:   有无校验位
  @nStop:    几位停止位
  返回值:设置成功与否:
        secuss:1
  error: 0
  * */
int set_opt(int fd,int nSpeed,int nBits,char nEvent,int nStop)
{
    struct termios newtio,oldtio;

 if(tcgetattr(fd,&oldtio) < 0)
    {
     perror("SetupSerial 1");
  return -1;
 }

 bzero(&newtio,sizeof(newtio));
 newtio.c_cflag |= CLOCAL|CREAD;
 newtio.c_cflag &= ~CSIZE;
 //设置有效数据位个数
 switch(nBits)
 {
 case 7:
  newtio.c_cflag |= CS7;
  break;
 case 8:
  newtio.c_cflag |= CS8;
  break;
 }
 switch(nEvent)
 {
    case 'O':
     newtio.c_cflag |= PARENB;
     newtio.c_cflag |= PARODD;
     newtio.c_iflag |= (INPCK|ISTRIP);
     break;
    case 'E':
     newtio.c_iflag |= (INPCK|ISTRIP);
     newtio.c_cflag |= PARENB;
     newtio.c_cflag &= ~PARODD;
     break;
    case 'N'://无奇偶校验
     newtio.c_cflag &= ~PARENB;
     break;

 }
 switch(nSpeed)
 {
 case 2400:
  cfsetispeed(&newtio,B2400);
  cfsetospeed(&newtio,B2400);
  break;
 case 4800:
  cfsetispeed(&newtio,B4800);
  cfsetospeed(&newtio,B4800);
  break;
 case 9600:
  cfsetispeed(&newtio,B9600);
  cfsetospeed(&newtio,B9600);
  break;
 case 115200:
  cfsetispeed(&newtio,B115200);
  cfsetospeed(&newtio,B115200);
  break;
 default:
  cfsetispeed(&newtio,B115200);
  cfsetospeed(&newtio,B115200);
  break;
 }
 if(nStop == 1)
 {
     newtio.c_cflag &= ~CSTOPB;
 }
 else if(nStop == 2)
 {
     newtio.c_cflag |= CSTOPB;
 }
 newtio.c_cc[VTIME] = 0;
 newtio.c_cc[VMIN] = 0;
    tcflush(fd,TCIFLUSH);//刷新设置,使设置立即生效

 if((tcsetattr(fd,TCSANOW,&newtio)) != 0)
 {
     perror("com set error");
  return -1;
 }
 printf("set done\n");
 return 0;
}

/*
 * 函数功能:打开串口
 * 参数:
 *      @comport:打开的串口的号码
 *      @fd:     打开的文件描述符
 * 返回值:打开成功与否
 *      secuess: 返回打开的串口的文件描述符
 *      error:   -1
 * 最后修改时间:2011.12.22
 * */
int open_port(int fd,int comport)
{
 long vdisable;
 if(0 == comport)
 {
     fd = open("/dev/ttyS0",O_RDWR|O_NOCTTY|O_NDELAY);
  if(-1 == fd)
  {
     perror("can not open the port");
     return -1;
  }
 }
 if(1 == comport)
 {
     fd = open("/dev/s3c2410_serial0",O_RDWR|O_NOCTTY|O_NDELAY);
  if(-1 == fd)
  {
     perror("can not open the port");
     return -1;
  }
 }
 if(2 == comport)
 {
     fd = open("/dev/s3c2410_serial1",O_RDWR|O_NOCTTY|O_NDELAY);
  if(-1 == fd)
  {
     perror("can not open the port");
     return -1;
  }
 }
 if(3 == comport)
 {
     fd = open("/dev/s3c2410_serial2",O_RDWR|O_NOCTTY|O_NDELAY);
  if(-1 == fd)
  {
     perror("can not open the port");
     return -1;
  }
 }

 if(fcntl(fd,F_SETFL,0) < 0)
  printf("fcntl error\n");
 else
  printf("fcntl=%d\n",fcntl(fd,F_SETFL,0));
 if(isatty(STDIN_FILENO) == 0)
  printf("standard input is not a terminal device\n");
 else
  printf("isatty sucess \n");
 printf("fd-open=%d\n",fd);

 return fd;
}

int gprs(int type)
{
 int fd;
 int pid;
 char string[100];
 int n = 0;
 /*小偷进屋*/
 char send_thief[100] = "0891683110102105F011000D91685115607830F70008000A5C0F50778FDB5C4BFF01";
 /*家里着火*/
 char send_fire[100] = "0891683110102105F011000D91685115607830F70008000A5bb691cc7740706bff01";


 /*打开串口,并设置属性(115200,8位有效数据位,无奇偶校验,1位)*/
 //extern pthread_mutex_t GPRS_lock;
  // pthread_mutex_lock(&GPRS_lock);
// fd=open_port(fd,2);
 set_opt(fd,9600,8,'N',1);
 n = write(fd,"at\r",strlen("at\r"));
 printf("write=%d\n",n);
 printf(".................\n");
    /*发送AT,返回ok*/
 sleep(2);
 bzero(string,sizeof(string));
 n = read(fd,string,100);
 printf("read AT=%d\n",n);
 printf("read AT=%s\n",string);
 
 


 
    /*短信中心号码*/
 //write(fd,"AT+CSCA=\"+8613010112500\"\r",strlen("AT+CSCS=\"+8613010112500\"\r"));
 /*write(fd,"AT+CSCA=+8613010112500\r",strlen("AT+CSCS=+8613010112500\r"));
 sleep(2);
 bzero(string,sizeof(string));
 n = read(fd,string,100);
 printf("read AT+CSCA = %d\n",n);
 printf("read AT+CSCA =%s\n",string);
    */
    /*
 write(fd,"AT+CCID\r",strlen("AT+CCID\r"));
 sleep(2);
 bzero(string,sizeof(string));
 n = read(fd,string,100);
 printf("read AT+CCID %d\n",n);
 printf("read AT+CCID =%s\n",string);
 */
    /*发送目标号码*/
 //n = write(fd,"AT+CMGS=+8615510687037\r",strlen("AT+CMGS=+8615510687037\r"));
 
 /*发送设置的短信模式*/
 
 write(fd,"AT+CMGF=0\r",strlen("AT+CMGF=0\r"));
 sleep(2);
 bzero(string,sizeof(string));
 n = read(fd,string,100);
 printf("read AT+CMGF = %d\n",n);
 printf("read AT+CMGF =%s\n",string);

 

 write(fd,"AT+CMGS=25\r",strlen("AT+CMGS=25\r"));
 sleep(2);
 bzero(string,sizeof(string));
 n = read(fd,string,50);
 printf("read AT+CMGS = %d\n",n);
 printf("read AT+CMGS =%s\n",string);

 


 char cmd[100];
 bzero(cmd,100);
   
 
 /*火灾时发出的短信*/
    if(THIEF_INTO == type)
 {
  strcpy(cmd,send_thief);
 }
 
 /*盗贼进入时发出的短信*/
 
    if(FIRE == type)
 {
  strcpy(cmd,send_fire);
 }
 

 strcat(cmd,"\x1a");
    n = write(fd,cmd,strlen(cmd));
 sleep(2);
 /*查看网络注册*/
 printf("search the internet...............\n");
 write(fd,"AT+CREG=?\r",strlen("AT+CRGE=?\r"));
 sleep(2);
 bzero(string,sizeof(string));
 n = read(fd,string,50);
 printf("read AT+CRGE = %d\n",n);
 printf("read AT+CRGE =%s\n",string);

 /*拨号  ATD + 电话号码(别忘了加";")*/
 printf("call..................\n");
 write(fd,"ATD +8615510687037;\r",strlen("ATD +8615510687037;\r"));
 sleep(2);
 bzero(string,sizeof(string));
 n = read(fd,string,50);
 printf("read ATD = %d\n",n);
 printf("read ATD =%s\n",string);
/* if(n < 0)
 {
  printf("send message error\n");
  return -1;
 }
 printf("write message = %d\n",n);
 printf("send end\n");
 sleep(1);
    close(fd);
*/


   // pthread_mutex_unlock(&GPRS_lock);
 /*sleep(2);
 bzero(string,sizeof(string));
 n = read(fd,string,100);
 printf("read message %d\n",n);
 printf("read message =%s\n",string);
    */
 return 1;
}
 FILE *fd;

/* int main(void)
 {
  //FILE *fd;
  printf("11111111111\n");
     fd = open("/dev/ttyS0",O_RDWR|O_NOCTTY|O_NDELAY);
  printf("22222222222\n");
  set_opt(fd,9600,8,0,0);
  gprs(THIEF_INTO);
  return 0;
 }
*///测试代码 
 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

doublewei1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值