⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 net.cpp

📁 采用cb平台开以
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include <vcl.h>
#pragma hdrstop
//*----------------------------------------------------------------------------
//*        NANTIAN DONGHUA TECHNOLOGY CO.,Ltd  COPYRIGHT 2006
//*----------------------------------------------------------------------------
//* The software is delivered by henry huang for driver develop.
//* Write in 2007-10-28
//* Update   2007-11-23 by zhanghw
//*    增加WNetInterfaceInit()调用方式;
//*----------------------------------------------------------------------------
#include "GprsNet.h"
#include "s_net.h"
#include "endebug.h"
#include <ctype.h>
#include "UartPort.h"
//-----------------------------------------------------------------------
#define	GPRSSCAN	0		//关闭扫描方式,使用中断方式

#define GPRSPORT	4		//GPRS的端口号
#define GPRSBAUND	115200	//GPRS口的波特率
////////////////////////////////////////////////////////////////////////// 
unsigned char cmd[128],rsp[200];
unsigned char NetCheckType(void);
//////////////////////////////////////////////////////////////////////////
//by henry
#define LEN_GPRS_DATA	3500
extern unsigned char GprsBuff[];
extern unsigned short iGprsRead;
extern unsigned short iGprsWrite;
#define  StTick GetTickCount()
/*******************************************************************************
** name: CdmaRcvPck
** function:  从串口接受数据
** parameter:len 预接受数据大小  ms 时间超时  mode 1 打开通信串口 
** ret: 	  数据的个数
** modify:
** comment:
********************************************************************************/
unsigned char ClearGprsBuf()
{
	iGprsRead =0;
	iGprsWrite =0;
	memset(GprsBuff,0,LEN_GPRS_DATA);
}

//以毫秒为单位
//如果收到数据,返回实际收到的数据长度;
unsigned int CdmaRcvPck(unsigned char *buf,unsigned short len,unsigned int ms,unsigned char mode)
{
	unsigned short i=0;
	unsigned short ret;
	unsigned int	lTmpStic,nstic,ntime;
	unsigned int	itime;
	unsigned char	cdata,nextchar;

    lTmpStic =StTick;
    nextchar=0;
    i=0;

	while((StTick - lTmpStic) < ms)
	{
        if (PortRecv(mode,buf+i,1)==0)
        {
            nextchar=1;
            i++;
        }
        else
        {
            if (nextchar)
                break;
        }
    }

    buf[i] = 0;


	#ifdef NET_DEBUG
		if (i==0)
			DebugPrint("\r\n time out: %d",ms);

		DebugPrint("\r\n GprsBuff:%64s len=%d receivelen=%d\r\n",GprsBuff,len,i);
		DebugPrint("\r\n   >>>%s[", GprsBuff);
		DebugData(0,(i+2)%64,10,GprsBuff);
		DebugPrint("]\r\n");
		//DebugPrint("\r\n receive buf:%64s",buf);
	#endif

	return i;
}
/*******************************************************************************
** name:	 CdmaSendPck
** function:  通讯串口发送数据
** parameter:	buf 待发送数据 len发送长度
** ret: 	 0 -- 成功
** modify:
** comment:
********************************************************************************/
unsigned char CdmaSendPck(unsigned char *buf,int len)
{
	int i, j=0;

	ClearGprsBuf();

	#ifdef NET_DEBUG
	DebugPrint("\r\n cmd=%s", buf);
	#endif

	//PortOpen(GPRSPORT, GPRSBAUND);
	//PortOpen(4,19200);
	//PortOpen(4,9600);	

	for (i=0; i<len; i++) {	
		PortSend(GPRSPORT, buf[i]);
		for (j=0; j<100; j++);	
	}
	return 0;
}


/*******************************************************************************
** name:  MsgRcvPck
** function: 接受短信的内容
** parameter: buf 短信内容 len 预接受短信长度
** ret: 	   短信字节     
** modify:
** comment:	   只用来接受短信内容
********************************************************************************/
unsigned short MsgRcvPck(unsigned char *buf,unsigned short len)
{
	int i;
	unsigned char ret;

	char *Rs;

	ret = PortRecv(3,buf,3000);
	if (ret != 0) {
		return 0;
	}

	i=1;
	buf[i]=0;

	while (i < len)
	{
	 	ret = PortRecv(3,buf+i,30);
		if (ret != 0)
			continue;	  //等待比较处理后再返回,由于不同的卡读取的时间							
//			return 0;     //不一样,所以比较后才返回值

		i++; 
		buf[i] = 0;

		if (i < 15) {
			Rs = strstr((char *)buf,"ERROR");  //对出现异常进行处理
			if (Rs)	{
				return 0x00;
			}
		} 

		Rs = strstr((char *)(buf+i-2),"OK");
		if (Rs == GPRS_NULL)
			continue;
		else {
			return i;
		}
												
	}
	return 0;
}



//////////////////////////////////////////////////////////////////////////
//初始化函数
//返回值  0 成功  其他值:对应错误
void ConvAscii(unsigned char *Hex,unsigned char *Ascii,unsigned int len)
{
	unsigned int i;
	unsigned char ch;
	for(i=0;i<len;i++){
		ch=Hex[i]/16;
		if(ch>=10){
			ch=ch-10+'A';
		}
		else{
			ch=ch+'0';
		}
		Ascii[2*i]=ch;
		ch=Hex[i]%16;
		if(ch>=10){
			ch=ch-10+'A';
		}
		else{
			ch=ch+'0';
		}
		Ascii[2*i+1]=ch;
	}
	Ascii[2*len]=0;	
}

void AsciiToHex(unsigned char *Ascii,unsigned char *Hex,int len)
{
	int i;
	unsigned char ch;
	for(i=0;i<len;i=i+2){
		if(Ascii[i]>'9'){
			ch=toupper(Ascii[i])-'A'+0xa;		
		}else{
			ch=Ascii[i]-'0';
		}
		Hex[i/2]=ch<<4;
		if(Ascii[i+1]>'9'){
			ch=toupper(Ascii[i+1])-'A'+0xa;		
		}else{
			ch=Ascii[i+1]-'0';
		}		
		Hex[i/2]+=ch;	
	}
	return;
}
  

//////////////////////////////////////////////////////////////////////////////
//*********************        add short message 	************************//
//////////////////////////////////////////////////////////////////////////////


extern unsigned short uc2gbc( unsigned short uc );

/*
unsigned char NumAlPDU(char *Tage,unsigned char *Src,unsigned char Len)
{
	unsigned char i,temp;
	unsigned char pos;
	pos=0;
	for(i=0;i<Len;i++)
	{
		temp=(Src[i]&0x0f);
		if(temp!=0x0f)
		{
			Tage[pos]=temp+'0';
			pos++;
		}
		temp=(Src[i]&0xf0)>>4;
		if(temp!=0x0f)
		{
			Tage[pos]=temp+'0';
			pos++;
		}
	}
	return pos;
}

*/
int getGbLenOfUniStr( const unsigned short *p )
{
	int len = 0;

	while ( *p ) {
		if ( *p < 0x80 ) {
			len += 1;
		}
		else {
			len += 2;	/* convert unsupport char to ?? */
		}
		p++;
	}
	return len;
}


unsigned int ReadUnicodeData(unsigned long HzAdr)
{
#if 0
	unsigned char temp;
	unsigned int dataTemp;
	union dd
	{
		unsigned char charbuffer[2];
		unsigned int intbuffer;
	};
	union dd inttemp;
	temp=(unsigned char)(HzAdr>>12);
	MCON=4;
	XBYTE[FZK_HIGH_ADDR]=temp;
	MCON=0;
	dataTemp=FZF_ADDR+(unsigned int)(HzAdr&0x00000fff);
	MCON=4;
	temp=XBYTE[dataTemp];
	MCON=0;
	
	inttemp.charbuffer[0]=temp;
	MCON=4;
	temp=XBYTE[dataTemp+1];
	MCON=0;
	
	inttemp.charbuffer[1]=temp;
	
	return inttemp.intbuffer;
#endif
    return 0;
}


/****************************************************************************
* NAME:		uni2gb
* PURPOSR:	Convert unicode string to gbk string
*			the byte order of unicode MUST be consistent with underlying CPU
*			the two-byte code in GBK is always big-endian
* ENTRY:	unistr	unicode string
*			gbbuf	output buffer
*			buflen	length of buffer
* EXIT:		length of gbk string
****************************************************************************/
int uni2gb( unsigned short *unistr, unsigned char *gbbuf, int buflen )
{
#if 0
	int gblen, i;
	f_cs2=0;
	rf_cs=0;
	TDA8007cs=0;
	f_cs=1;	
	gblen = getGbLenOfUniStr(unistr);
	if ( !gbbuf || ( buflen <= 0 ) ) {
		return gblen;
	}

	if ( gblen >= buflen ) 
	{
		gblen = buflen - 1;
	}
	gbbuf[gblen] = 0;

	i = 0;
	while ( i < gblen ) 
	{
		//if ( *unistr < 0x80 ) 
		if ( *unistr < 0xa1 ) 
		{
			gbbuf[i] = (char)(*unistr);
			i++;
		}
		else 
		{
			/* gbk-code is big-endian */
			
			unsigned short t = uc2gbc(*unistr);
			
			gbbuf[i++] = (unsigned char)( t >> 8 );
			gbbuf[i++] = (unsigned char)( t & 0xff );
	
	
		}
		unistr++;
	}

	return gblen;
#endif
}

/****************************************************************************
* NAME:		binarySearch
* PURPOSR:	perform binary search in uint16 array
* ENTRY:	table	- point to uint16 array
*		tablen	- length of array
*		code	- word to search
* EXIT:		int16 - index of object, -1 for no match
* AUTHOR: 	lvjie November 18, 2003
****************************************************************************/
short binarySearch(unsigned short icode)
{
	unsigned short head,tail,middle;
	head = 0;
	tail=0;
	middle=0;
	tail = CODE_NUM-1;
	if ((icode < ReadUnicodeData(unionaddress+head*2))||(icode > ReadUnicodeData(unionaddress+tail*2)))
		return (-1);

	while (head <= tail)
	{
		middle = (head+tail)/2;
		if (icode == ReadUnicodeData(unionaddress+middle*2))
		{
			return (middle);
		}
		else if (icode > ReadUnicodeData(unionaddress+middle*2))
			head = middle+1;
		else
			tail = middle-1;
	}

	return (NOT_SUPPORTED);
}

unsigned short uc2gbc( unsigned short uc )
{
	short index;
	index = binarySearch(uc);
	
	if ( index == NOT_SUPPORTED ) 
	{
		return 0x3f3f;	/* ?? */
	}
	else 
	{ 
		return ReadUnicodeData(uniAGbkcode+index*2);
	}
}



//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////


#if 1

/*******************************************************************************
** name:  		WNetInterfaceInit
** function: 	对GPRS相关的参数和函数进行初始化
** parameter: 	none
** ret:    		
** modify:
** comment:	    需要在开机会使用GPRS功能前至少调用一次.
********************************************************************************/
void WNetInterfaceInit(void)
{
	#ifdef MOTO_GPRS		//motorola	
	{
		gNet.WNetInit		= M_NetInit;
		gNet.WNetLink		= M_NetLink;
		gNet.WNetConn		= M_NetConn;
		gNet.WNetTxd		= M_NetSend;
		gNet.WNetRxd		= M_NetRcv;
		gNet.WNetCheckConn	= M_NetCheckConn;
		gNet.WNetCheckLink	= M_NetCheckLink;
		gNet.WNetCloseConn	= M_NetCloseConn;
		gNet.WNetCloseLink	= M_NetCloseLink;
		
		gNet.WNetCheckSim	= M_NetCheckSim;
		gNet.WNetCheckSignal= M_NetCheckSignal;	
		gNet.WNetGetVer     = M_NetGetVer;
		
		gNet.WNetCheckType	= NetCheckType;	
		//gNet.WNetUidPwd		= M_NetUidPwd;
	}
	#endif
	
	
	#ifdef HUAWEI_GPRS		//huawei
	{
		gNet.WNetInit		= H_NetInit;
		gNet.WNetLink		= H_NetDial;
		gNet.WNetConn		= H_TcpConn;
		gNet.WNetTxd		= H_NetTcpSend;
		gNet.WNetRxd		= H_NetTcpRcv;
		gNet.WNetCloseConn	= H_NetTCPClose;
		gNet.WNetCloseLink	= H_NetPPPClose;
		gNet.WNetCheckSim	= H_NetCheckSim;
		gNet.WNetCheckSignal= H_NetCheckSignal;	
		gNet.WNetGetVer     = H_NetGetVer;	
		
		gNet.WNetCheckType	= NetCheckType;
	}
	#endif
	
}



/*******************************************************************************
** name: CheckWNetType
** function:  检测网络类型
** parameter:none
** ret:    0--CDMA  1--Mortorola  2--GTM900 3--异常 
** modify:
** comment:
********************************************************************************/
unsigned char NetCheckType(void)
{
	unsigned char ret;
	unsigned char *p;
	
	//设置工作模式处于内置TCPIP方式
	
	if (gNet.WNetType==0x01 || gNet.WNetType==0x02)
		return gNet.WNetType;	

	strcpy(cmd,"AT+CGMI\r");
	CdmaSendPck(cmd,8);
	ret = CdmaRcvPck(rsp,100,9000,1);
	if (!ret)
		return 0x03;
	else{
		p = (unsigned char*)strstr((char*)rsp,"HUAWEI");
		if (p != NULL){
			gNet.WNetType = 0x01;
			return 0x02;
		}
		else{
			p = (unsigned char *)strstr((char *)rsp, "Motorola");
			gNet.WNetType = 0x02;
			return 0x01;
		}
	}
}





/*******************************************************************************
** name:  		WNetInit
** function: 	对GPRS初始化
** parameter: 	none
** ret:    		1--成功  其他值--异常
** modify:
** comment:	 	注意,使用该函数,仅对模块起初始化的作用,对于SIM卡,不做处理,考虑
			 	原因为,初始化卡的处理速度远慢于模块的初始化
********************************************************************************/
unsigned char WNetInit(void)
{
	return gNet.WNetInit();
}

/*******************************************************************************

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -