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

📄 gsmsmsdlg.cpp

📁 一个可以实现与SCADA系统实现数据访问,并且通过手机短消息的方式发送到指定手机用户的实现.
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// GsmSmsDlg.cpp : implementation file
//
#include "stdafx.h"
#include "GsmSms.h"
#include "GsmSmsDlg.h"
#include "Dlgsetcom.h"
#include "DlgTel.h"
#include "Dlghis.h"
#include <fstream.h>

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

/////////////////////////////////////////////////////////////////////////////
//from FjRlib
extern "C" __declspec(dllimport) void APIENTRY
FjLibGetLastMsg(FJS_MSG_STRUCT & Msg,int count);
extern "C" __declspec(dllimport) BOOL APIENTRY
FjLibMsgTrans(FJS_MSG_STRUCT & Msg,CString & sbuf,BOOL His);

HANDLE hCom;
const int  Port_Bate[7] = {2400,4800,9600,19200,38400,57600,115200};
const char Port_Com[4][5] = {"COM1","COM2","COM3","COM4"};

FJS_MSG_STRUCT	Msg;

//thread of send to TC35
BOOL   GetMsg(FJS_MSG_STRUCT & Msg);
BOOL   SearchMap(WORD rtu,WORD type,FJS_MSG_STRUCT Msg);
LPWSTR InitializeFromString(LPCSTR pstrString);
BOOL   WriteOut(CString name,CString code,CString SendString);
UINT   SendMsgThread(LPVOID pParm);

BOOL ThreadBusy = FALSE;
CString FjSysPath;
CString InitFile;

//from ini for map
CMap <DWORD, DWORD&, DWORD , DWORD &> m_LmtSendMap;
CMap <DWORD, DWORD&, DWORD , DWORD &> m_yxSendMap;
CMap <DWORD, DWORD&, DWORD , DWORD &> m_DevSendMap;
CMap <DWORD, DWORD&, DWORD , DWORD &> m_BhSendMap;

//save message
short			MsgPutPtr;				//存指针
short			MsgGetPtr;				//指针
FJS_MSG_STRUCT	MsgBuf[FJD_MSG_BUF_MAX];//消息缓冲

//person message
_PERSON_INFO_  m_person_info[FJD_PERSON_MAX];
int m_person_max;

//for Display List.
_DISPLAY_LIST_ m_display_list[FJD_MSG_BUF_MAX];
int GetListPtr = 0;
BOOL Repaint = FALSE;

void AddItem(_DISPLAY_LIST_  msg) 
{
	if (GetListPtr <= 0){
		GetListPtr = FJD_MSG_BUF_MAX-1;
	}
	else GetListPtr --;
    
	m_display_list[GetListPtr].m_date = msg.m_date;
    m_display_list[GetListPtr].m_time = msg.m_time;
    m_display_list[GetListPtr].m_person.name = msg.m_person.name;
    m_display_list[GetListPtr].m_person.number = msg.m_person.number;
    m_display_list[GetListPtr].SendedString = msg.SendedString;

	//write to file.
	CString Filename;
	CString tempstring;
	Filename = FjSysPath;
	Filename +="\\bin\\Smshis.txt";

	ofstream outs(Filename,ios::out|ios::app);
	if(outs)
	{
		tempstring = msg.m_person.name;
		tempstring += " ";
		tempstring += msg.m_person.number;
		tempstring += " ";
		tempstring += msg.m_date;
		tempstring += ":";
		tempstring += msg.m_time;
		tempstring += "--";
		tempstring += msg.SendedString;
		tempstring += "\n";
        outs.seekp(ios::beg);
		outs.write((LPCSTR)tempstring,tempstring.GetLength());
		outs.close();
	}
}

BOOL GetMsg(FJS_MSG_STRUCT & Msg)
{
	short	d;
	d = MsgGetPtr;
	if (d == MsgPutPtr) return FALSE;
	d++;
	if (d >= FJD_MSG_BUF_MAX) d=0;
	Msg=MsgBuf[d];
	MsgGetPtr=d;
	return TRUE;
}
//Form string to unicode.
LPWSTR InitializeFromString(LPCSTR pstrString)
{
    int nLen = MultiByteToWideChar(CP_ACP, 0,pstrString, -1, NULL, NULL);
    LPWSTR lpszW = new WCHAR[nLen];
    MultiByteToWideChar(CP_ACP, 0, pstrString, -1, lpszW, nLen);

    if(lpszW)
		return lpszW;
	return NULL;
}

BOOL SearchMap(WORD rtu,WORD type,FJS_MSG_STRUCT Msg)
{
	DWORD key, value;
	POSITION pos;

	switch (type){
	    case SEND_TYPE_YX:{
			pos=m_yxSendMap.GetStartPosition();
			while (pos){
				m_yxSendMap.GetNextAssoc(pos, key, value);//key=rtu,value=dot
				if ((Msg.MsgDate.Yx.Rtu==value)&&(Msg.MsgDate.Yx.Dot==key)&&(rtu = Msg.MsgDate.Yx.Rtu)){
					return TRUE;
				}
			}
		}
	    case SEND_TYPE_DEV:{
			pos=m_DevSendMap.GetStartPosition();
			while (pos){
				m_DevSendMap.GetNextAssoc(pos, key, value);//key=rtu,value=dot
				if ((Msg.MsgDate.Dev.Rtu==value)&&(Msg.MsgDate.Dev.Dot==key)&&(Msg.MsgDate.Dev.Rtu==rtu)){
					return TRUE;
				}
			}
		}
	    case SEND_TYPE_LMT:{
			pos=m_LmtSendMap.GetStartPosition();
			while (pos){
				m_LmtSendMap.GetNextAssoc(pos, key, value);//key=dot,value=dot
				if ((Msg.MsgDate.Lmt.Dot==key)&&(Msg.MsgDate.Lmt.Dot==value)&&(Msg.MsgDate.Lmt.Rtu==rtu)){
					return TRUE;
				}
			}
		}
	    case SEND_TYPE_BHWJ:{
			pos=m_BhSendMap.GetStartPosition();
			while (pos){
				m_BhSendMap.GetNextAssoc(pos, key, value);//key=rtu,value=act
				if ((Msg.MsgDate.Bh.Rtu==value)&&(Msg.MsgDate.Bh.SndBhAct=key)&&(Msg.MsgDate.Bh.Rtu==rtu)){
					return TRUE;
				}
			}
		}
	}
	return FALSE;
}

BOOL WriteOut(CString name,CString code,CString SendString)
{
    char  rec[255];
	DWORD wer;
    DWORD lpNumber;
 	CString result = "";
	CString Code = code;
    
	memset(rec,0, 255);
    
    CString sendhead = "0011000d9168";//head
	CString sendcontrol = "000810";//control
    //Pro Phone Number
	wer = Code.GetLength();
	CString TelCode="";
	char temp11,temp12;
	if(wer&1){
		Code += "f";
		wer++;
	}
	for(int i=0;i<wer;i++){
		if(!(i&1)){
			temp11 = Code[i+1];
		    temp12 =  Code[i];
		    TelCode += temp11;
		    TelCode += temp12;
		}
	}

    CString sendmessage = SendString;

	char* tempw = (char*)(InitializeFromString(sendmessage.GetBuffer(0)));
	//LPCTSTR tempw;
	//tempw = LPCTSTR(InitializeFromString(sendmessage.GetBuffer(0)));
	BYTE templength = MultiByteToWideChar(CP_ACP, 0,sendmessage, -1, NULL, NULL);
    BYTE len = (templength-1) * sizeof(WCHAR);

    BYTE eeee[255];
    for(int j=0;j<len/2;j++)
	{
		eeee[j*2+1] = tempw[j*2];
		eeee[j*2] = tempw[(j*2)+1];
	}

    CString temp="";
	CString temp1;
    for(j=0;j<len;j++)
	{
		temp1.Format("%02x",eeee[j]);
		temp +=temp1;
	}

	delete tempw;

	CString temptemp[16]; //split a message to max 16
    int     lenlen[16]; 
	int r = len*2/MESSAGE_MAX;
	int f = len*2%MESSAGE_MAX;
	int c;
	CString Inv;
	Inv = temp;
	for(i=0;i<r;i++)
	{
		temptemp[i] = Inv.Left(MESSAGE_MAX);
	    Inv = temp.Right(len-MESSAGE_MAX*i);
		lenlen[i] = MESSAGE_MAX/2;
	}
	temptemp[i] = temp.Right(f);
	lenlen[i] = f/2;
	c = i+1;
    BOOL SendSuccess = FALSE;
	for(i=0;i<c;i++)
	{
        SendSuccess = FALSE;
		int sendlength = 14+lenlen[i]+1;
        CString messagelength;

	    messagelength.Format("%02x",lenlen[i]);
	    CString sendcode = sendhead+TelCode;
	    sendcode += sendcontrol;
	    sendcode += messagelength;
	    sendcode += temptemp[i];
	    sendcode += 0x1a;//ctrl+z

	    CString command = "";
        PurgeComm(hCom,PURGE_TXABORT|PURGE_TXCLEAR);

        CString cmgf = "at+cmgf=0\r\n";
	    WriteFile(hCom,cmgf.GetBuffer(0),cmgf.GetLength(),&lpNumber,NULL);
        cmgf.ReleaseBuffer();
		    
        ReadFile(hCom,result.GetBuffer(32),32,&wer,NULL);
	    result.ReleaseBuffer();
			
		_DISPLAY_LIST_  dsp;
	    dsp.m_person.name   = name;
        dsp.m_person.number = code;
        dsp.SendedString    = SendString;
	    CTime time = CTime::GetCurrentTime();
		int year = time.GetYear();
	    int month = time.GetMonth();
	    int day = time.GetDay();
	    int hour = time.GetHour();
	    int min = time.GetMinute();
	    int second = time.GetSecond();
	    dsp.m_date.Format("%4d:%02d:%02d",year,month,day);
	    dsp.m_time.Format("%02d:%02d:%02d",hour,min,second);
        AddItem(dsp);
        Repaint = TRUE;
	
	    if(result.Find("OK")!=-1){
            command.Format("at+cmgs=%d\r\n",sendlength);
	        WriteFile(hCom,command.GetBuffer(0),command.GetLength(),&lpNumber,NULL);
            command.ReleaseBuffer();

	        ReadFile(hCom,result.GetBuffer(32),32,&wer,NULL);
	    
		    if(result.Find(">")!=-1){
			    WriteFile(hCom,sendcode.GetBuffer(0),sendcode.GetLength(),&lpNumber,NULL);
	            sendcode.ReleaseBuffer();
		    } 
            result.ReleaseBuffer();
		
	        ReadFile(hCom,result.GetBuffer(255),255,&wer,NULL);
		
		    if((result.Find("CMGS")!=-1)&&(result.Find("OK")!=-1))
		    {
			    SendSuccess = TRUE;
		    }
	        result.ReleaseBuffer();
			
	    }
	}
	//if(SendSuccess){
	//	return TRUE;
	//}
	
	if(SendSuccess)
	{
		_DISPLAY_LIST_  dsp;
	    dsp.m_person.name   = name;
        dsp.m_person.number = code;
        dsp.SendedString    = SendString;
	    CTime time = CTime::GetCurrentTime();
		int year = time.GetYear();
	    int month = time.GetMonth();
	    int day = time.GetDay();
	    int hour = time.GetHour();
	    int min = time.GetMinute();
	    int second = time.GetSecond();
	    dsp.m_date.Format("%4d:%02d:%02d",year,month,day);
	    dsp.m_time.Format("%02d:%02d:%02d",hour,min,second);
        AddItem(dsp);
        Repaint = TRUE;
	}


/*
    CString messagelength;
	messagelength.Format("%02x",len);
	CString sendcode = sendhead+TelCode;
	sendcode += sendcontrol;
	sendcode += messagelength;
	sendcode += temp;
	sendcode += 0x1a;//ctrl+z

	CString command = "";
    PurgeComm(hCom,PURGE_TXABORT|PURGE_TXCLEAR);

    CString cmgf = "at+cmgf=0\r\n";
	WriteFile(hCom,cmgf.GetBuffer(16),cmgf.GetLength(),&lpNumber,NULL);
    cmgf.ReleaseBuffer();
		    
    ReadFile(hCom,result.GetBuffer(32),32,&wer,NULL);
	result.ReleaseBuffer();

	if(result.Find("OK")!=-1){

	    command.Format("at+cmgs=%d\r\n",sendlength);
	    WriteFile(hCom,command.GetBuffer(0),command.GetLength(),&lpNumber,NULL);
        command.ReleaseBuffer();

	    ReadFile(hCom,result.GetBuffer(32),32,&wer,NULL);
	    
		if(result.Find(">")!=-1){
			WriteFile(hCom,sendcode.GetBuffer(0),sendcode.GetLength(),&lpNumber,NULL);
	        sendcode.ReleaseBuffer();
		}
        result.ReleaseBuffer();
		
	    ReadFile(hCom,result.GetBuffer(255),255,&wer,NULL);
		
		if((result.Find("CMGS")!=-1)&&(result.Find("OK")!=-1))
		{
	        _DISPLAY_LIST_  dsp;
		    dsp.m_person.name   = name;
            dsp.m_person.number = code;
            dsp.SendedString    = SendString;
	
			CTime time = CTime::GetCurrentTime();
			int year = time.GetYear();
	        int month = time.GetMonth();
	        int day = time.GetDay();
	        int hour = time.GetHour();
	        int min = time.GetMinute();
	        int second = time.GetSecond();
	        dsp.m_date.Format("%4d:%02d:%02d",year,month,day);
	        dsp.m_time.Format("%02d:%02d:%02d",hour,min,second);
	        AddItem(dsp);
            Repaint = TRUE;

			return TRUE;
		}
	    result.ReleaseBuffer();
	}*/
	return FALSE;
}

UINT SendMsgThread(LPVOID pParm)
{
    CString code;
	CString name;
	WORD rtu;
    int type;
    int count = 0;
	CString sendstring=_T("");
    FJS_MSG_STRUCT	Msg;

	while(TRUE){
		if(!GetMsg(Msg)){
	        ThreadBusy = FALSE;
			return 0;

⌨️ 快捷键说明

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