mini2440串口控制GPRS模块程序

来源:互联网 发布:云主机 知乎 编辑:程序博客网 时间:2024/05/22 02:11
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/*******************************************
全局变量:
设置ip地址:IP_ADDR
设置短信地址:phone
设置短信内容:message
*******************************************/
int Gprs_fd;
char *IP_ADDR="219.231.146.1";
char Buff[20];
char message[20]="000800044F60597D";
char phone[12]="15264104927";
/*******************************************
File name:      void GTM900B_Init(void)   
Description:    gprs模块初始化函数
Author:          
Version:        版本  
Date:           2012.4.19
func:           GTM900B初始化,串口初始化
Output:        无
*******************************************/
void GTM900B_Init(void)
{   
    int nBytes=0;
    int len=0;
    char read_buf[1024];
    char *write_buf; 
    write_buf=(char *)malloc(sizeof(char)*200);
    bzero(read_buf,sizeof(read_buf));
    bzero(write_buf,sizeof(write_buf));
struct termios newcfg;
    Gprs_fd=open("/dev/ttySAC1",O_RDWR|O_NOCTTY);
if(Gprs_fd == -1)
    {
        perror("open error!\n");
        exit(0);
    }
    tcgetattr(Gprs_fd,&newcfg);
    cfsetispeed(&newcfg,B9600);
    cfsetospeed(&newcfg,B9600);
 
    newcfg.c_cflag &=~CSIZE;
    newcfg.c_cflag |=CS8;
    newcfg.c_cflag &=~PARENB;
    newcfg.c_cflag &=~PARODD;
    newcfg.c_cflag |=(CLOCAL | CREAD);
    newcfg.c_cflag &=~CRTSCTS;
    newcfg.c_cflag &=~CSTOPB;


    newcfg.c_lflag &=~( ICANON | ECHO | ECHOE | ISIG |IEXTEN|ECHOKE|ECHOCTL );
    
    newcfg.c_oflag &=0;
    newcfg.c_oflag &=~ONLCR;
    newcfg.c_iflag &=~IGNPAR;
    newcfg.c_lflag =0;
    newcfg.c_cc[VTIME] = 5 ;
    newcfg.c_cc[VMIN] = 0;


    tcflush(Gprs_fd,TCIOFLUSH);


    printf("serial configure complete\n");
    
    if(tcsetattr(Gprs_fd,TCSANOW, &newcfg) !=0)
    {
         perror("serial error!\n");
         return -1;
    }
/*激活gprs模块,进行初始化*/
    sprintf(write_buf,"AT\r");
    write(Gprs_fd,write_buf,strlen(write_buf));
    sleep(1);
    while((nBytes=read(Gprs_fd,read_buf,sizeof(read_buf)))>0)
    {
read_buf[nBytes]='\0';
        printf("string : %s \n",read_buf);


    }


    sprintf(write_buf,"ATE0\r");
    write(Gprs_fd,write_buf,strlen(write_buf));
    sleep(1);
    while((nBytes=read(Gprs_fd,read_buf,sizeof(read_buf)))>0)
    {
read_buf[nBytes]='\0';
        printf("string : %s \n",read_buf);


    }
}
/*******************************************
File name:      void Gprs_Init(void)   
Description:    gprs初始化函数
Author:          
Version:        版本  
Date:           2012.4.19
func:           Gprs初始化:
                1.AT+CGATT=1
2.AT+CGDCONT=1,"IP","CMNET"
3.AT%ETCPIP
4.AT%IPOPEN="TCP","ip_addr",port\r"
5.AT%IOMODE=0
Output:        无
*******************************************/
void Gprs_Init(void)
{
    int nBytes=0;
    int len=0;
    char read_buf[1024];
    char *write_buf; 
    write_buf=(char *)malloc(sizeof(char)*200);
    bzero(read_buf,sizeof(read_buf));
    bzero(write_buf,sizeof(write_buf));


    sprintf(write_buf,"AT+CGATT=1\r");
    write(Gprs_fd,write_buf,strlen(write_buf));
    sleep(1);
    while((nBytes=read(Gprs_fd,read_buf,sizeof(read_buf)))>0)
    {
read_buf[nBytes]='\0';
        printf("string : %s \n",read_buf);


    }


    sprintf(write_buf,"AT+CGDCONT=1,\"IP\",\"CMNET\"\r");
    write(Gprs_fd,write_buf,strlen(write_buf));
    sleep(1);
    while((nBytes=read(Gprs_fd,read_buf,sizeof(read_buf)))>0)
    {
read_buf[nBytes]='\0';
        printf("string : %s \n",read_buf);


    }
   
    sprintf(write_buf,"AT");
    strcat(write_buf,"%ETCPIP\r");
    write(Gprs_fd,write_buf,strlen(write_buf));
    sleep(1);
    while((nBytes=read(Gprs_fd,read_buf,sizeof(read_buf)))>0)
    {
read_buf[nBytes]='\0';
        printf("string : %s \n",read_buf);


    }
    
    sprintf(write_buf,"AT%IPOPEN=\"TCP\",\"%s\",6666\r",IP_ADDR);
    write(Gprs_fd,write_buf,strlen(write_buf));
    sleep(3);
    while((nBytes=read(Gprs_fd,read_buf,sizeof(read_buf)))>0)
    {
read_buf[nBytes]='\0';
        printf("string : %s \n",read_buf);


    }
    
    sprintf(write_buf,"AT");
    strcat(write_buf,"%IOMODE=0\r");
    write(Gprs_fd,write_buf,strlen(write_buf));
    sleep(1);
    while((nBytes=read(Gprs_fd,read_buf,sizeof(read_buf)))>0)
    {
read_buf[nBytes]='\0';
        printf("string : %s \n",read_buf);
    }
 
}
/*******************************************
File name:      void Send_Message(char *Msg)   
Description:    AT指令发短信
Author:          
Version:        版本  
Date:           2012.4.19
func:           GSM发短信:
                1.AT%IPCLOSE=1关闭tcp连接
2.AT+CMGF=0\r 发送PDU短信
3.AT+CMGS=%d  短信长度
4."%s\x1a", Msg 短信内容
5.Gprs_Init()启动tcp连接
Input:         *Msg,短信内容
Output:        无
*******************************************/
void Send_Message(char *Msg)
{
    int nBytes=0;
    int len=0;
    char read_buf[1024];
    char *write_buf; 
    //len=strlen(Msg);
    write_buf=(char *)malloc(sizeof(char)*200);
    bzero(read_buf,sizeof(read_buf));
    bzero(write_buf,sizeof(write_buf));
/*激活gprs模块*/
    sprintf(write_buf,"AT");
    strcat(write_buf,"%IPCLOSE=1\r");
    write(Gprs_fd,write_buf,strlen(write_buf));
    sleep(3);
    while((nBytes=read(Gprs_fd,read_buf,sizeof(read_buf)))>0)
    {
read_buf[nBytes]='\0';
        printf("string : %s \n",read_buf);


    }
    
    sprintf(write_buf,"AT+CMEE=1\r");
    write(Gprs_fd,write_buf,strlen(write_buf));
    sleep(1);
    while((nBytes=read(Gprs_fd,read_buf,sizeof(read_buf)))>0)
    {
read_buf[nBytes]='\0';
        printf("string : %s \n",read_buf);


    }
    


/*发送PDU编码短信*/
    sprintf(write_buf,"AT+CMGF=0\r");
    write(Gprs_fd,write_buf,strlen(write_buf));
    bzero(write_buf,sizeof(write_buf));
    sleep(1);
    while((nBytes=read(Gprs_fd,read_buf,sizeof(read_buf)))>0)
    {
read_buf[nBytes]='\0';
        printf("string : %s \n",read_buf);


    }
   


/*短信长度*/
    sprintf(write_buf,"AT+CMGS=%d\r",(strlen(Msg)-2)/2);
    write(Gprs_fd,write_buf,strlen(write_buf));
    bzero(write_buf,sizeof(write_buf));
    sleep(1);
    while((nBytes=read(Gprs_fd,read_buf,sizeof(read_buf)))>0)
    {
read_buf[nBytes]='\0';
        printf("string : %s \n",read_buf);


    }
    
/*短信内容*/
    sprintf(write_buf,"%s\x1a", Msg);
    write(Gprs_fd,write_buf,strlen(write_buf));
    sleep(1);
    while((nBytes=read(Gprs_fd,read_buf,sizeof(read_buf)))>0)
    {
read_buf[nBytes]='\0';
        printf("string : %s \n",read_buf);
    }
    
    Gprs_Init();
    return 0;    
}


/*******************************************
File name:      void Gprs_Send(char *DATA)
Description:    Gprs发送信息
Author:         
Version:        版本  
Date:           2012.4.19
func:           GPRS发送:
                AT%IPSEND="DATA"\r"
Input:         *DATA,信息内容
Output:        无
*******************************************/
void Gprs_Send(char *DATA)
{
    int nBytes=0;
    int len=0;
    char read_buf[1024];
    char *write_buf; 
    //len=strlen(Msg);
    write_buf=(char *)malloc(sizeof(char)*200);
    bzero(read_buf,sizeof(read_buf));
    bzero(write_buf,sizeof(write_buf));
    
    sprintf(write_buf,"AT%IPSEND=\"%s\"\r",DATA);
    write(Gprs_fd,write_buf,strlen(write_buf));
    sleep(20);
    
}
/*******************************************
File name:      void *Gprs_Read(void *gprs_read)
Description:    Gprs_Read线程,阻塞接收上位机数据
Author:         
Version:        版本  
Date:           2012.4.19
Input:         无
Output:        无
func:          在程序中查询Buff中是否有值即可,用完清空
*******************************************/
void *Gprs_Read(void *gprs_read)
{
    int nBytes=0;
    int n=0;
    char read_buf[1024];
    char *Buff=(char *)gprs_read;
    char *Test="%IPDATA:";
    char DATA[9];
    sleep(1);
    Gprs_Init();
    bzero(Buff,sizeof(Buff));
    while(1)
    {
sleep(2);
   nBytes=read(Gprs_fd,read_buf,sizeof(read_buf));
        read_buf[nBytes]='\0';
        strncpy(DATA,read_buf,8);
        DATA[8]='\0';
        if(strcmp(DATA,Test)==0)
{
for(n=11;n<19;n++)
            Buff[n-11]=read_buf[n];
            Buff[19]='\0';
}      
        if(Buff[0]!='\0')
printf("%s \n",Buff);
        bzero(Buff,sizeof(Buff));
    }   
}
int main()
{
int i;
    char temp;
    pthread_t Read_Gprs;


    char Gprs_Read_Str[100];
GTM900B_Init();
/*连接要发送的短信内容,在发短信前使用*/
    strcat(phone,"F");
    for(i=0;i<11;i++,i++)
    {
temp=phone[i];
        phone[i]=phone[i+1];
        phone[i+1]=temp;
    }
    char Message[400];
    bzero(Message,sizeof(Message));
    strcat(Message,"0011000D9168");
strcat(Message,phone);
strcat(Message,message); 
    pthread_create(&Read_Gprs,NULL,(void *)Gprs_Read,(void *)Gprs_Read_Str);
    while(1)
    {
           
    }


}





原创粉丝点击