#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <string.h>
#include <stdlib.h>
#include <linux/types.h>
#include <stddef.h>
#include <time.h>
#include <errno.h>
#define GPRS_RESET 0 //模块硬复位
#define GPRS_POWER_ON 1 //模块硬件上电
#define GPRS_RING_STATUS 2 //电话呼入状态
#define no_driver 0x01 //驱动错误
#define no_module 0x02 //模块错误
#define no_signal 0x04 //无信号
#define ring_into 0x08 //电话呼入状态
#define no_sim_card 0x10 //sim卡不正常
#define no_cgatt 0x20 //是否开通GPRS业务
#define no_GPRS 0x40 //gprs掉线,打不开zpppopen
#define no_tcp 0x80 //tcp连接断
unsigned char Com_err=0; //串口错误字
unsigned char Gprs_err=0; //GPRS模块错误字
#define Rec_send_sign 0x01 //接收到发送标识符
#define send_data_ok 0x02 //发送错误
#define Gprs_run 0x04 //gprs已正常启动
unsigned char Gprs_status=0; //GPRS模块错误字
static struct GPRS_COM
{
unsigned char Gprs_rssi[2]; //信号强度rssi+误码率ber
unsigned char Gprs_ip2[4]; //主站备用IP
unsigned char Gprs_port2[2]; //备用IP的端口号
unsigned char Gprs_port2_bcd[5]; //bcd格式的端口号
unsigned char Gprs_creg; //SIM卡注册情况
unsigned char GPRS_heartbeat_time; //心跳时间,单位:分钟
}Gprs_state;
int fd1,fd2; //fd1:串口 fd2:gprs模块
unsigned char Gprs_CSCA[9]={0x13,0x80,0x03,0x71,0x50,0x0f,0x0ff,0xff}; //短信中心号码
unsigned char Gprs_apn[17]="CMNET";
unsigned char Gprs_user[33];
unsigned char Gprs_pwd[33]; //按376.1规定
unsigned char Gprs_ip1[5]={0xda,0x1d,0x76,0x1b}; //主站IP
unsigned char Gprs_port1[3]={0x75,0x83};//0x35}; //端口号30083
unsigned char Gprs_port_bcd[6];
int Gprs_send_376_update; //已更新为#0x33,没有更新为0x0
int Gprs_rec_376_update; //已更新为#0x33,没有更新为0x0
int Gprs_rec_376_len; //376.1完整帧长度
int Gprs_rec_buff_len; //65536字节接收数据长度
int Gprs_send_pointer; //发送指针
int AT_rev_len; //AT接收字符长度
unsigned char Gprs_send_farme; //发送帧
unsigned char SendData[550]; //AT通道发送缓存
unsigned char Gprs_send_376[16500]; //376.1帧备份缓冲区,长度:16500
unsigned char Gprs_rec_buff[16500]; //帧接收缓冲区,长度:16500
unsigned char Gprs_rec_376[16500]; //376.1完整帧
long int Gprs_recive_send_number=0; //gprs通讯流量,包括收发字节
int Recive_send_sign=0; //接收到<标识
int Recive_OK_sign=0; //接收到OK标识符
struct tm *system_time; //系统时间,system_time
int min_status; //分状态
int min_acc; //分累加器
//376.1封装的心跳包,行政区码:4500,终端地址:0010,带时间标签
unsigned char JZQ_addr_h=0x00;
unsigned char JZQ_addr_l=0x10;
unsigned char JZQ_postcode[2]={0x00,0x45};
//行政区划 终端地址
unsigned char GPRS_heartbeat[26]={0x68,0x32,0x00,0x32,0x00,0x68,0xC9,0x00,0x00,0x00,0x00,0x00,0x02,0x71,0x00,0x00,0x01,0x00,
0xB2,0x16};
/**********设置串口参数**************/
static int speed_arr[] = {B230400, B115200, B57600, B38400, B19200, B9600, B4800, B2400, B1800, B1200, B600, B300};
static int name_arr[] = {230400, 115200, 57600, 38400, 19200, 9600, 4800, 2400, 1800, 1200, 600, 300};
/****************************************
* *
* 串口参数设置,速度设置 *
* *
*****************************************/
int set_speed(int Com, int speed)
{
int i;
struct termios Opt;
tcgetattr(Com, &Opt); //获取参数,修改
for (i=0;i<sizeof(speed_arr)/sizeof(int);i++)
{
if(speed==name_arr[i])
{
tcflush(Com, TCIOFLUSH);
cfsetispeed(&Opt, speed_arr[i]);
cfsetospeed(&Opt, speed_arr[i]);
if((tcsetattr(Com,TCSANOW,&Opt))<0) //执行新参数
{
perror("tcsetattr fd");
return -1;
}
tcflush(Com,TCIOFLUSH); //清空缓冲区
return 1;
}
}
return -1;
}
/****************************************
* *
* 串口参数设置,数据,停止,校验位 *
* *
*****************************************/
int set_other_attribute(int Com, int databits, int stopbits, int parity)
{
struct termios options;
if(tcgetattr(Com,&options)<0)
{
perror("SetupSerial 1");
return -1;
}
options.c_cflag &= ~CSIZE; //屏蔽字符大小位
switch(databits) /*é?ê?Y?ê*/
{
case 7:
options.c_cflag |= CS7;
break;
case 8:
options.c_cflag |= CS8;
break;
default:
fprintf(stderr,"Unsupported data size\n");
return -1;
}
switch(parity)
{
case 'n':
case 'N':
case 0:
options.c_cflag &= ~PARENB; /* 清除校验使能 */
options.c_iflag &= ~INPCK; /* 使能校验检查 */
break;
case 'o':
case 'O':
case 1:
options.c_cflag |= (PARODD | PARENB); /* 奇校验 */
options.c_iflag |= INPCK; /* 清除校验检查 */
break;
case 'e':
case 'E':
case 2:
options.c_cflag |= PARENB; /* Enable parity */
options.c_cflag &= ~PARODD; /* 偶校验 */
options.c_iflag |= INPCK; /* Disnable parity checking */
break;
default:
fprintf(stderr,"Unsupported parity\n");
return -1;
}
switch (stopbits) /*停止位*/
{
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
printf("Unsupported stop bits\n");
return -1;
}
options.c_cflag |= CLOCAL; //CLOCAL:忽略DCD信号,避免一直占用端口.
options.c_cflag |= CREAD; //CREAD:启动接收装置,能够读入数据
options.c_cflag &= ~CRTSCTS; //屏蔽硬件流控制
/* Set input parity option */
tcflush(Com,TCIFLUSH);
options.c_oflag=0;
options.c_lflag=0;
options.c_cc[VTIME]=0; /* read时不延时 */
options.c_cc[VMIN]=0; /* 若有数据,则read()读取指定数量的数据后返回;若没有数据,则read()返回0 */
if(tcsetattr(Com,TCSANOW,&options)<0)
{
perror("SetupSerial 3");
return -1;
}
return 1;
}
void clr_Gprs_rec_buff(int pointer)
{
int i;
for(i=0;i<pointer+10;i++)
{
Gprs_rec_buff[i]=0;
}
}
void clr_Gprs_rec_376(void)
{
int i;
for(i=0;i<16500;i++)
{
Gprs_rec_376[i]=0x0;
}
}
/****************************************************
* *
* *
* GPRS子程序 *
* *
* *
****************************************************/
int AT_return_OK(int delays)
{
int i,j,len=0,k=0,ret=0;
unsigned char Re_data[100]; //模块返回数据
unsigned char Re_data_ok[100]; //模块返回数据
for(i=0;i<100;i++)
{
Re_data_ok[i]=0;
}
for(i=0;i<delays;i++)
{
usleep(1000); //1ms为单位
AT_rev_len=read(fd1,Re_data,100);
if(AT_rev_len>0)
{
for(j=0;j<AT_rev_len;j++)
{ //传递接收到的数据
k=Re_data[j];
Re_data_ok[len+j]=k;
}
len=len+j;
if(len>4) i=delays-5; //再留出5毫秒接收时间
}
}
// printf("%s\n",Re_data_ok);
for(j=0;j<len;j++)
{
if(Re_data_ok[j