短信猫的短信简单操作

好好静下心分析问题,怎么方法简洁,怎么快就用哪一个,

 

 

#include     <stdio.h>     
#include     <stdlib.h>    
#include     <unistd.h>    
#include     <sys/types.h>
#include     <sys/stat.h> 
#include     <fcntl.h>    
#include     <termios.h>   
#include     <errno.h>  
 
char ipdata[17];
char phone[12];
int  fd1;
long speed_arr[] = { B115200,B38400, B19200, B9600, B4800, B2400, B1200, B300,
     B38400, B19200, B9600, B4800, B2400, B1200, B300, };
long name_arr[] = {115200,38400, 19200, 9600, 4800, 2400, 1200, 300,
     38400, 19200, 9600, 4800, 2400, 1200, 300, };

 

void set_speed(int fd, long speed)
{
 int   i;
 int   status;
 struct termios   Opt;
 tcgetattr(fd, &Opt);
 for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++)
    {
    if (speed == name_arr[i])
    {  
 printf("speed:%ld/n",name_arr[i]);
  
        tcflush(fd, TCIOFLUSH);
      cfsetispeed(&Opt, speed_arr[i]);
  cfsetospeed(&Opt, speed_arr[i]);
  status = tcsetattr(fd, TCSANOW, &Opt);
  if (status != 0)
            perror("tcsetattr fd1");
  return;
 }
   tcflush(fd,TCIOFLUSH);
   }
}

int set_Parity(int fd,int databits,int stopbits,int parity)
{
 struct termios options;
 if ( tcgetattr( fd,&options) != 0)
 {
  perror("SetupSerial 1");
  return 0;
 }
 options.c_cflag &= ~CSIZE;
 switch (databits)
 {
 case 7:
     options.c_cflag |= CS7;
     break;
 case 8:
     options.c_cflag |= CS8;
     break;
 default:
     fprintf(stderr,"Unsupported data size/n");
     return 0;
}
switch (parity)
{
case 'n':
case 'N':
   options.c_cflag &= ~PARENB;  
   options.c_iflag &= ~INPCK;    
   break;
case 'o':
case 'O':
   options.c_cflag |= (PARODD | PARENB);
   options.c_iflag |= INPCK;           
   break;
case 'e':
case 'E':
   options.c_cflag |= PARENB;     /* Enable parity */
   options.c_cflag &= ~PARODD; 
   options.c_iflag |= INPCK;       /* Disnable parity checking */
   break;
case 'S':
case 's': /*as no parity*/
   options.c_cflag &= ~PARENB;
   options.c_cflag &= ~CSTOPB;
   break;
default:
   fprintf(stderr,"Unsupported parity/n");
   return 0;
   }
 
switch (stopbits)
{
case 1:
   options.c_cflag &= ~CSTOPB;
   break;
case 2:
   options.c_cflag |= CSTOPB;
   break;
default:
   fprintf(stderr,"Unsupported stop bits/n");
   return 0;
}

if (parity != 'n')
   options.c_iflag |= INPCK;
    options.c_cc[VTIME] = 150; // 15 seconds
    options.c_cc[VMIN] = 0;

tcflush(fd,TCIFLUSH);
if (tcsetattr(fd,TCSANOW,&options) != 0)
{
 perror("SetupSerial 3");
 return 0;
}
 return 1;
}


int OpenDev(char *Dev)
{
 int fd = open( Dev, O_RDWR| O_NOCTTY | O_NDELAY);    
 if (-1 == fd)
 {
  printf("open erron /n");
     perror("Can't Open Serial Port");
     return -1;
 }
 else printf("open ok/n");
 return fd;
}

int send_msg(int fd,char * msg1)
{
 char b=26;
 char  number[1024]={0};
 char  msg[1024]={0};

 sprintf(number,"at+cmgs=/"%s/"/r",phone);
 printf("number:%s/n",number);
 sprintf(msg,"%s",msg1);
 write(fd,"atz/r",4);
 sleep(1);
 write(fd,"at+cmgf=1/r",12);
 sleep(1);
 write(fd,"at+csca=/"+8613800270500/"/r",26);
 sleep(1);
 write(fd,number,strlen(number));
 sleep(2);
 printf("msg:%s/n",msg);
 write(fd,msg,strlen(msg));
 sleep(1);
 write(fd,&b,1);
 printf("send ok/n");
 return 1;
}

int my_ping(char *ip,int cnt)
{
 char tmp[2048];
 char cmd [36];
 int  count=0;
 int  fd3;
 char *ptr;
 char iptmp[16];
 memcpy(iptmp,ip,16);

  if(cnt<1) return 0;
 sprintf(cmd,"ping -c %d %s >ping_tmp.dat",cnt,ip); 
 system(cmd);
 
 if((fd3=open("ping_tmp.dat",O_RDONLY))==-1)
 {
  printf("open ping_tmp.dat erron/n");
  return 0; 
 }else printf("ping ok/n");

 read(fd3,tmp,2047);
 ptr=tmp;
 while((ptr=strstr(ptr,"ttl"))!=NULL)
 {
  count++;
  ptr+=3; 
 }
 printf("count:%d/n",count);

 if(count>cnt-1)
 {
  printf("connect ok/n");
  close(fd3);
  return 1;
 }
 else
 {
  printf("connect erron/n");
     send_msg(fd1,"take over erron");
  close(fd3);
  return 0;
 }
}

int main(int argc, char **argv)
{
 int fd2;
 int nread;
 char buff[512];
 int i=0,j=0,k=0;
 char tmp_ip[36]; 
 char *ptr;
 char *dev ="/dev/ttyS0";
 fd1 = OpenDev(dev);
 if (fd1>0)
 {
  set_speed(fd1,115200);
  printf("set speed ok/n");
 }
 else
    {
     printf("Can't Open Serial Port!/n");
     exit(0);
    }

 if (set_Parity(fd1,8,1,'N')== 0)
 {
     printf("Set Parity Error/n");
     exit(1);
 }
 else printf("set parity ok/n");

 if((fd2=open("ping.conf",O_RDONLY))==-1)
  printf("open errn/n");
 read(fd2,tmp_ip,2047);

 while(tmp_ip[i++]!='/n');
 memcpy(ipdata,&tmp_ip[3],i-4);
 ipdata[i-3]='/0';
 while(tmp_ip[i++]>57); 
 printf("i:%d/n",i);
 memcpy(phone,&tmp_ip[i-1],11);

 printf("phone :%s/n",phone);
 printf("IP    :%s/n",ipdata);

 my_ping(ipdata,5);

/*
nread=write(fd,"at/r",4);
if(nread>0) printf("connect /n");

 if((nread = read(fd,buff,36))>0)
      {
         printf("Len %d/n",nread);
         buff[nread+1]='/0';
         printf("BUFF:%s/n",buff);
   nread=0;
   }
*/
    close(fd1);
    exit(0);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值