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

📄 smsdll.cpp

📁 PDU方式发送SMS的动态链接库
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// SMSDLL.cpp : Defines the initialization routines for the DLL.
//

#include "stdafx.h"
#include <afxdllx.h>
#include "SMSDLL.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

static AFX_EXTENSION_MODULE SMSDLLDLL = { NULL, NULL };

extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
	// Remove this if you use lpReserved
	UNREFERENCED_PARAMETER(lpReserved);

	if (dwReason == DLL_PROCESS_ATTACH)
	{
		TRACE0("SMSDLL.DLL Initializing!\n");
		
		// Extension DLL one-time initialization
		if (!AfxInitExtensionModule(SMSDLLDLL, hInstance))
			return 0;

		// Insert this DLL into the resource chain
		// NOTE: If this Extension DLL is being implicitly linked to by
		//  an MFC Regular DLL (such as an ActiveX Control)
		//  instead of an MFC application, then you will want to
		//  remove this line from DllMain and put it in a separate
		//  function exported from this Extension DLL.  The Regular DLL
		//  that uses this Extension DLL should then explicitly call that
		//  function to initialize this Extension DLL.  Otherwise,
		//  the CDynLinkLibrary object will not be attached to the
		//  Regular DLL's resource chain, and serious problems will
		//  result.

		new CDynLinkLibrary(SMSDLLDLL);
	}
	else if (dwReason == DLL_PROCESS_DETACH)
	{
		TRACE0("SMSDLL.DLL Terminating!\n");
		// Terminate the library before destructors are called
		AfxTermExtensionModule(SMSDLLDLL);
	}
	return 1;   // ok
}
COM_STR  m_com_data; 
//OVERLAPPED m_osRead,m_osWrite; //用于异步读/写
//CCommApp theApp; 

// 初始化全局变量 
BOOL Init_Com(COM_STR &m_data) 
{ 
	memset(&m_com_data,0,sizeof(COM_STR)); 
	memcpy(&m_com_data,&m_data,sizeof(COM_STR)); 
/*
	memset(&m_osRead,0,sizeof(OVERLAPPED));
	memset(&m_osWrite,0,sizeof(OVERLAPPED));

	if((m_osRead.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL))==NULL)
		return FALSE;

	if((m_osWrite.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL))==NULL)
		return FALSE;
*/
	return TRUE;} 

// 打开串口 
int Open_Port() 
{ 
	DCB dcb; 
	int ret = 0; 
	char m_port[5]=""; 
	sprintf(m_port,"COM%d",m_com_data.serial_port+1); 

	// 打开串口为异步方式 
	m_com_data.m_hComm=CreateFile( 
		m_port, 
		GENERIC_READ | GENERIC_WRITE, 
		0, 
		NULL, 
		OPEN_EXISTING, 
		FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,//异步方式
		//0,//同步方式
		NULL 
		); 

	if(m_com_data.m_hComm == (HANDLE) -1)
	{ 
		AfxMessageBox("OPEN_PORT_FAIL\n"); 
		return OPEN_PORT_FAIL; 
	} 

	// 得到当前串口默认配置 
	ret=GetCommState(m_com_data.m_hComm,&dcb); 
	if(!ret)
	{ 
		CloseHandle(m_com_data.m_hComm); 
		AfxMessageBox("GET_COM_STATA_FAIL\n"); 
		return GET_COM_STATA_FAIL; 
	} 

	// 设置串口波特率 
	int baud=CBR_9600; 
	switch(m_com_data.baud)
	{ 
		case 1 : baud=CBR_2400;   break; 
		case 2 : baud=CBR_4800;   break; 
		case 3 : baud=CBR_9600;   break; 
		case 4 : baud=CBR_14400;  break; 
		case 5 : baud=CBR_19200;  break; 
		case 6 : baud=CBR_38400;  break; 
		case 7 : baud=CBR_56000;  break; 
		case 8 : baud=CBR_57600;  break; 
		case 9 : baud=CBR_115200; break; 
		default : break; 
	} 

	// 设置当前串口参数 
	dcb.BaudRate=baud; 
	dcb.Parity=NOPARITY; 
	dcb.StopBits=ONESTOPBIT; 
	dcb.ByteSize=8; 
	ret=SetCommState(m_com_data.m_hComm,&dcb); 
	if(!ret)
	{ 
		CloseHandle(m_com_data.m_hComm); 
		AfxMessageBox("SET_COM_STATA_FAIL\n"); 
		return SET_COM_STATA_FAIL; 
	} 

	// 设置当前串口超时 
	COMMTIMEOUTS m_timeout; 
	m_timeout.ReadIntervalTimeout = 10000; 
	m_timeout.ReadTotalTimeoutConstant = m_com_data.timeout; 
	m_timeout.ReadTotalTimeoutMultiplier = 0; 
	m_timeout.WriteTotalTimeoutConstant = m_com_data.timeout; 
	m_timeout.WriteTotalTimeoutMultiplier = 0; 
	ret=SetCommTimeouts(m_com_data.m_hComm,&m_timeout); 
	if(!ret)
	{ 
		CloseHandle(m_com_data.m_hComm); 
		AfxMessageBox("SET_COM_TIMEOUT_FAIL\n"); 
		return SET_COM_TIMEOUT_FAIL; 
	} 

	// 清串口缓冲区 
	PurgeComm(m_com_data.m_hComm,PURGE_TXCLEAR); 
	PurgeComm(m_com_data.m_hComm,PURGE_RXCLEAR); 

	return 0; 
} 

// 发送数据 
int Send_Data(char * buf,int buf_len) 
{ 
	int ret=0; 
	DWORD len=0; 
	//ret=WriteFile(m_com_data.m_hComm,buf,buf_len,&len,&m_osWrite); 
	ret=WriteFile(m_com_data.m_hComm,buf,buf_len,&len,NULL); 
	if(!ret)
	{ 
		AfxMessageBox("SEND_DATA_FAIL\n"); 
		return SEND_DATA_FAIL; 
	} 
	if(len==0)
	{ 
		AfxMessageBox("SEND_DATA_TIMEOUT\n"); 
		return SEND_DATA_TIMEOUT; 
	} 
	return 0; 
} 

// 接收数据 
int Recv_Data(LPSTR buf,int buf_len) 
{ 
	COMSTAT ComStat ;
	DWORD dwErrorFlags;
	int ret=0; 

	ClearCommError( m_com_data.m_hComm, &dwErrorFlags, &ComStat ) ;
	buf_len = min( (DWORD) buf_len, ComStat.cbInQue ) ;

	DWORD len=0; 
	//ret=ReadFile(m_com_data.m_hComm,buf,buf_len,&len,&m_osRead); 
	ret=ReadFile(m_com_data.m_hComm,buf,buf_len,&len,NULL); 
	if(!ret)
	{ 
		AfxMessageBox("RECV_DATA_FAIL\n"); 
		return RECV_DATA_FAIL; 
	} 
	if(len==0)
	{ 
		AfxMessageBox("RECV_DATA_TIMEOUT\n");
		//AfxMessageBox("RECV_DATA_TIMEOUT\n"); 
	return RECV_DATA_TIMEOUT; 
	} 
	return 0; 
} 

// 完成一次发送接收任务 

int Trans_Data(char * sbuf,int send_len,char * rbuf,int recv_len) 
{ 
	int ret=0; 
	ret=Send_Data(sbuf,send_len); 
	if(ret)
	{ 
		return ret; 
	} 
	return (Recv_Data(rbuf,recv_len)); 
} 

// 关闭串口 

int Close_Port() 
{ 
	int ret=0; 
	ret=CloseHandle(m_com_data.m_hComm); 
	if(!ret)
	{ 
		AfxMessageBox("CLOSE_PORT_FAIL\n"); 
		return CLOSE_PORT_FAIL ; 
	} 
	return 0; 
} 

/**************** 以下为GSM SMSDLL专用函数 ****************/

// 可打印字符串转换为字节数据
// 如:"C8329BFD0E01" --> {0xC8, 0x32, 0x9B, 0xFD, 0x0E, 0x01}
// 输入: pSrc - 源字符串指针
//       nSrcLength - 源字符串长度
// 输出: pDst - 目标数据指针
// 返回: 目标数据长度
int GSMString2Bytes(const char* pSrc, unsigned char* pDst, int nSrcLength)
{
	for (int i = 0; i < nSrcLength; i += 2)
	{
		// 输出高4位
		if ((*pSrc >= '0') && (*pSrc <= '9'))
		{
			*pDst = (*pSrc - '0') << 4;
		}
		else
		{
			*pDst = (*pSrc - 'A' + 10) << 4;
		}

		pSrc++;

		// 输出低4位
		if ((*pSrc>='0') && (*pSrc<='9'))
		{
			*pDst |= *pSrc - '0';
		}
		else
		{
			*pDst |= *pSrc - 'A' + 10;
		}

		pSrc++;
		pDst++;
	}

	// 返回目标数据长度
	return (nSrcLength / 2);
}

// 字节数据转换为可打印字符串
// 如:{0xC8, 0x32, 0x9B, 0xFD, 0x0E, 0x01} --> "C8329BFD0E01" 
// 输入: pSrc - 源数据指针
//       nSrcLength - 源数据长度
// 输出: pDst - 目标字符串指针
// 返回: 目标字符串长度
int GSMBytes2String(const unsigned char* pSrc, char* pDst, int nSrcLength)
{
	const char tab[]="0123456789ABCDEF";	// 0x0-0xf的字符查找表

	for (int i = 0; i < nSrcLength; i++)
	{
		*pDst++ = tab[*pSrc >> 4];		// 输出高4位
		*pDst++ = tab[*pSrc & 0x0f];	// 输出低4位
		pSrc++;
	}

	// 输出字符串加个结束符
	*pDst = '\0';

	// 返回目标字符串长度
	return (nSrcLength * 2);
}

// 7bit编码
// 输入: pSrc - 源字符串指针
//       nSrcLength - 源字符串长度
// 输出: pDst - 目标编码串指针
// 返回: 目标编码串长度
int GSMEncode7bit(const char* pSrc, unsigned char* pDst, int nSrcLength)
{
	int nSrc;		// 源字符串的计数值
	int nDst;		// 目标编码串的计数值
	int nChar;		// 当前正在处理的组内字符字节的序号,范围是0-7
	unsigned char nLeft;	// 上一字节残余的数据

	// 计数值初始化
	nSrc = 0;
	nDst = 0;

	// 将源串每8个字节分为一组,压缩成7个字节
	// 循环该处理过程,直至源串被处理完
	// 如果分组不到8字节,也能正确处理
	while (nSrc < nSrcLength)
	{
		// 取源字符串的计数值的最低3位
		nChar = nSrc & 7;

		// 处理源串的每个字节
		if(nChar == 0)
		{
			// 组内第一个字节,只是保存起来,待处理下一个字节时使用
			nLeft = *pSrc;
		}
		else
		{
			// 组内其它字节,将其右边部分与残余数据相加,得到一个目标编码字节
			*pDst = (*pSrc << (8-nChar)) | nLeft;

			// 将该字节剩下的左边部分,作为残余数据保存起来
			nLeft = *pSrc >> nChar;

			// 修改目标串的指针和计数值
			pDst++;
			nDst++;
		}

		// 修改源串的指针和计数值
		pSrc++;
		nSrc++;
	}

	// 返回目标串长度
	return nDst;
}

// 7bit解码
// 输入: pSrc - 源编码串指针
//       nSrcLength - 源编码串长度
// 输出: pDst - 目标字符串指针
// 返回: 目标字符串长度
int GSMDecode7bit(const unsigned char* pSrc, char* pDst, int nSrcLength)
{
	int nSrc;		// 源字符串的计数值
	int nDst;		// 目标解码串的计数值
	int nByte;		// 当前正在处理的组内字节的序号,范围是0-6
	unsigned char nLeft;	// 上一字节残余的数据

	// 计数值初始化
	nSrc = 0;
	nDst = 0;
	
	// 组内字节序号和残余数据初始化
	nByte = 0;
	nLeft = 0;

	// 将源数据每7个字节分为一组,解压缩成8个字节
	// 循环该处理过程,直至源数据被处理完
	// 如果分组不到7字节,也能正确处理
	while(nSrc<nSrcLength)
	{
		// 将源字节右边部分与残余数据相加,去掉最高位,得到一个目标解码字节
		*pDst = ((*pSrc << nByte) | nLeft) & 0x7f;

		// 将该字节剩下的左边部分,作为残余数据保存起来
		nLeft = *pSrc >> (7-nByte);

		// 修改目标串的指针和计数值
		pDst++;
		nDst++;

		// 修改字节计数值
		nByte++;

		// 到了一组的最后一个字节
		if(nByte == 7)
		{
			// 额外得到一个目标解码字节
			*pDst = nLeft;

			// 修改目标串的指针和计数值
			pDst++;
			nDst++;

			// 组内字节序号和残余数据初始化
			nByte = 0;
			nLeft = 0;
		}

		// 修改源串的指针和计数值
		pSrc++;
		nSrc++;
	}

	// 输出字符串加个结束符

⌨️ 快捷键说明

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