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

📄 mobilesyncdlg.cpp

📁 A program that communicates with mobile, but need data line
💻 CPP
📖 第 1 页 / 共 4 页
字号:
// MobileSyncDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MobileSync.h"
#include "MobileSyncDlg.h"


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

const OP_UINT8 base64_asc[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

//#define _DISPLAY_DATA_ 

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMobileSyncDlg dialog

CMobileSyncDlg::CMobileSyncDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMobileSyncDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMobileSyncDlg)
	m_nMelodyCmd = -1;
	m_nRadioInbox = -1;
	m_nRadioSim = -1;
	m_nRadioSms = -1;
	m_nComboPort = -1;
	m_nEditIndex = 0;
	m_nComboCount = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_pfBuff = OP_NULL;
	m_nfCount = 0;
}

void CMobileSyncDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMobileSyncDlg)
	DDX_Control(pDX, IDC_RESULT, m_EditCtrl);
	DDX_Control(pDX, IDC_HEADER_LIST, m_HeaderList);
	DDX_Control(pDX, IDC_MSCOMM1, m_comm);
	DDX_Radio(pDX, IDC_MELODY_CMD, m_nMelodyCmd);
	DDX_Radio(pDX, IDC_INBOX, m_nRadioInbox);
	DDX_Radio(pDX, IDC_SIM, m_nRadioSim);
	DDX_Radio(pDX, IDC_SMS, m_nRadioSms);
	DDX_CBIndex(pDX, IDC_COMBO1, m_nComboPort);
	DDX_Text(pDX, IDC_EDIT_INDEX, m_nEditIndex);
	DDX_CBIndex(pDX, IDC_COMBO_COUNT, m_nComboCount);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMobileSyncDlg, CDialog)
	//{{AFX_MSG_MAP(CMobileSyncDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_OPEN_PORT, OnOpenPort)
	ON_BN_CLICKED(IDC_DOWNLOAD, OnDownload)
	ON_BN_CLICKED(IDC_COUNT, OnCount)
	ON_BN_CLICKED(IDC_INQUIRE, OnInquire)
	ON_BN_CLICKED(IDC_DELETE, OnDelete)
	ON_BN_CLICKED(IDC_UPLOAD, OnUpload)
	ON_BN_CLICKED(IDC_PB_STATUS, OnPbStatus)
	ON_BN_CLICKED(IDC_PB_SAVE, OnPbSave)
	ON_BN_CLICKED(IDC_PB_READ, OnPbRead)
	ON_BN_CLICKED(IDC_PB_DELETE, OnPbDelete)
	ON_BN_CLICKED(IDC_PB_UPDATE, OnPbUpdate)
	ON_BN_CLICKED(IDC_PB_DELETEALL, OnPbDeleteall)
	ON_BN_CLICKED(IDC_SMS_SEND, OnSmsSend)
	ON_BN_CLICKED(IDC_SMS_STORE, OnSmsStore)
	ON_BN_CLICKED(IDC_SMS_STATUS, OnSmsStatus)
	ON_BN_CLICKED(IDC_SMS_READ, OnSmsRead)
	ON_BN_CLICKED(IDC_SMS_DELETE, OnSmsDelete)
	ON_BN_CLICKED(IDC_COMM_REQUEST, OnCommRequest)
	ON_BN_CLICKED(IDC_COMM_TERMINATE, OnCommTerminate)
	ON_BN_CLICKED(IDC_COMM_MODEL, OnCommModel)
	ON_BN_CLICKED(IDC_BUTTON_PORTRAIT, OnButtonPortrait)
	ON_BN_CLICKED(IDC_CLOSE_PORT, OnClosePort)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMobileSyncDlg message handlers

BOOL CMobileSyncDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}


	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	GetDlgItem(IDC_OPEN_PORT)->EnableWindow(TRUE);

//	GetDlgItem(IDC_SEND_CMD)->EnableWindow(FALSE);
//	GetDlgItem(IDC_CHANGE_MODE)->EnableWindow(FALSE);
	GetDlgItem(IDC_COUNT)->EnableWindow(FALSE);
	
	GetDlgItem(IDC_INQUIRE)->EnableWindow(FALSE);
	GetDlgItem(IDC_DELETE)->EnableWindow(FALSE);
	GetDlgItem(IDC_DOWNLOAD)->EnableWindow(FALSE);
	GetDlgItem(IDC_UPLOAD)->EnableWindow(FALSE);

	m_HeaderList.InsertColumn(0, "FileName", LVCFMT_LEFT, 140);
	m_HeaderList.InsertColumn(1, "FileType", LVCFMT_LEFT, 70);
	m_HeaderList.InsertColumn(2, "FileSize", LVCFMT_LEFT, 70);

	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMobileSyncDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMobileSyncDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMobileSyncDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}


BEGIN_EVENTSINK_MAP(CMobileSyncDlg, CDialog)
//{{AFX_EVENTSINK_MAP(CMobileSyncDlg)
ON_EVENT(CMobileSyncDlg, IDC_MSCOMM1, 1 /* OnComm */, OnOnCommMscomm1, VTS_NONE)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()


void CMobileSyncDlg::OnOnCommMscomm1() 
{
	VARIANT variant_inp;
    COleSafeArray safearray_inp;
    LONG len,k;
    BYTE rxdata[2048]; //设置BYTE数组 An 8-bit integerthat is not signed.
    CString strtemp;
	CString strData;
	
    if (m_comm.GetCommEvent() == 2) //事件值为2表示接收缓冲区内有字符
    {             
		//以下你可以根据自己的通信协议加入处理代码
        variant_inp = m_comm.GetInput(); //读缓冲区
        safearray_inp = variant_inp; //VARIANT型变量转换为ColeSafeArray型变量
        len = safearray_inp.GetOneDimSize(); //得到有效数据长度

        for(k=0; k<len; k++)
		{
            safearray_inp.GetElement(&k,rxdata+k);//转换为BYTE型数组
		}
		
		OutText("Received Data: ");
        for(k=0; k<len; k++) //将数组转换为Cstring型变量
        {
            BYTE bt = *(char*)(rxdata+k); //字符型
            strtemp.Format("%c",bt); //将字符送入临时变量strtemp存放
            //m_strRXData += strtemp; //加入接收编辑框对应字符串 
			strData += strtemp;
			
		//	OutText(bt);
		//	OutText(" ");
			
        }
		OutText("\"");
		//OutText(strData);
		//OutText("\"");
		//OutText("\n");
		
		
		//if (m_nMode > 0)
		//{
			m_strRecvData += strData;
			comm_process_package(m_strRecvData);
		//}
    }
    //UpdateData(FALSE); //更新编辑框内容
	
	
}

void CMobileSyncDlg::comm_process_package(CString &strRecvData) 
{
	char chStart;
	char chEnd;
	OP_UINT8 RxBuffer[2048];
	OP_UINT8 tempBuffer[2048];
	OP_UINT16 DataLen=2048;
	OP_UINT16 sum_recv;
	OP_UINT16 sum_comp;

	//int len;
	chStart = strRecvData.GetAt(0);
	//len = strRecvData.GetLength();
	chEnd = strRecvData.GetAt(strRecvData.GetLength()-1);
	if ((chStart == 0x3a) && (chEnd == 0x3b))
	{
		OutText("Receive a Package: ");
		//OutText(strRecvData);
		OutText("\n");
		for (int i=0; i<strRecvData.GetLength(); i++)
		{
			RxBuffer[i] = strRecvData.GetAt(i);
		}
		if (comm_base64_decode(RxBuffer+1, i-2, tempBuffer, &DataLen))
		{
			memcpy(&sum_recv, tempBuffer+DataLen-2, 2);
			sum_comp = comm_checksum(tempBuffer, DataLen-2);
			if (sum_recv == sum_comp)
			{
				OutText("Original Data: ");
#ifdef _DISPLAY_DATA_
				if (DataLen < 100)
				{
					for (int i=0; i<DataLen-2; i++)
					{
						OutText(tempBuffer[i]);
						OutText(" ");
					}
				}
				else
				{
					for (int i=0; i<100; i++)
					{
						OutText(tempBuffer[i]);
						OutText(" ");
					}
				}
#endif				
				
				
				OutText("\n");
				strRecvData = "";
				//////////////////////////////////////////////////////
				//phone model
				if ((tempBuffer[0] == 0x00) && (tempBuffer[1] == 0x23) && (tempBuffer[2] == 0x00))
				{
					char ch[30];
					CString strText;
					memcpy(ch, tempBuffer+3, 30);
					strText.Format("Manufacturer = %s\n", ch);
					OutText(strText);

					memcpy(ch, tempBuffer+33, 30);
					strText.Format("Model = %s\n", ch);
					OutText(strText);

					memcpy(ch, tempBuffer+63, 30);
					strText.Format("Version = %s\n", ch);
					OutText(strText);
				}
				//////////////////////////////////////////////////////
				//get the file count
				//melody
				if ((tempBuffer[0] == 0x01) && (tempBuffer[1] == 0x21) && (tempBuffer[2] == 0x00))
				{
					memcpy(&m_nfCount, tempBuffer+3, 2);
					CString strTxt;
					COleDateTime afterTime;
					strTxt.Format("Get File Count: %d .\n", m_nfCount);
					afterTime = COleDateTime::GetCurrentTime();
					COleDateTimeSpan ts = afterTime - m_preTime;
					OutText(strTxt);
				}
				
				
				//dispaly the header information
				//melody header
				if ((tempBuffer[0] == 0x01) &&(tempBuffer[1] == 0x22) && (tempBuffer[2] == 0x00))
				{
					CString strType;
					CString strName;
					CString strSize;
					int nCount;
					int nSize;
					char name[44];
					WCHAR uniname[22];
					
					nCount = (DataLen - 5)/52;
					m_HeaderList.DeleteAllItems();
					for (int i=0; i<nCount; i++)
					{
						//for(int j=0; j<44; j++)
						//						{
						//							strName += tempBuffer[3+52*i+j];
						//						}
						
						memcpy(uniname, tempBuffer+3+52*i, 44);
						WideCharToMultiByte(CP_ACP, 0 , uniname, -1,
										name, 44, 0, 0);
						strName = name;

						for(int j=0; j<4; j++)
						{
							strType += tempBuffer[3+52*i+44+j];
						}
						memcpy(&nSize, tempBuffer+3+52*i+48, 4);
						strSize.Format("%d", nSize);

						
						m_HeaderList.InsertItem(i, strName);
						m_HeaderList.SetItemText(i, 1, strType);
						m_HeaderList.SetItemText(i, 2, strSize);
						strName = "";
						strType = "";
						strSize = "";
						nSize = 0;
					}
					
				}
				
				///////////////////////////////////////////////////////

				//receive upload header rsp
				//melody
				if ((tempBuffer[0] == 0x01) && (tempBuffer[1] == 0x26) && (tempBuffer[2] == 0x00))
				{
					OP_UINT32 length;
					
					OP_UINT8 Cmd[10] = {0x01, 0x07};
					m_nOffset = 0;
					if (m_nfSize > 1500)
					{
						length = 1500;
					}
					else
					{
						length = m_nfSize;
					}
					
					memcpy(Cmd+2, (char*)&m_nOffset, 4);
					memcpy(Cmd+6, (char*)&length, 4);

					if (m_pfBuff != NULL) free(m_pfBuff);
					m_pfBuff = (OP_UINT8*)malloc(m_nfSize);

					OutText("Send Upload Data Command.\n");
					comm_send_command(Cmd, sizeof(Cmd));
				}
				
				///////////////////////////////////////////////////////////////////

				//receive upload data rsp
				//melody
				if ((tempBuffer[0] == 0x01) && (tempBuffer[1] == 0x27) && (tempBuffer[2] == 0x00))
				{
				//	OP_UINT16 offset; 
					OP_UINT32 length;
					
					//memcpy(&offset, tempBuffer+3, 2);
					//memcpy(&length, tempBuffer+5, 2);
					//if (offset == m_nOffset)
					//{
						memcpy(m_pfBuff+m_nOffset, tempBuffer+3, DataLen-5);
						m_nOffset += DataLen-5;
					
						if (m_nOffset < m_nfSize)
						{
							//send next upload data
							OP_UINT8 Cmd[10] = {0x01, 0x07};
							
							if ((m_nfSize-m_nOffset) > 1500)
							{
								length = 1500;
							}
							else
							{
								length = m_nfSize - m_nOffset;
							}
							memcpy(Cmd+2, &m_nOffset, 4);
							memcpy(Cmd+6, &length, 4);
							
							OutText("Send Upload Data Command.\n");
							comm_send_command(Cmd, sizeof(Cmd));
						}
						else
						{
							//send upload done
							OP_UINT8 Cmd[2] = {0x01, 0x08};
							CFile f(m_strfName, CFile::modeCreate|CFile::modeWrite);
							f.Write(m_pfBuff, m_nfSize);
							f.Close();
							OutText("Upload Data Finished.\n");
							OutText("Save to File:");
							OutText(m_strfName);
							OutText("\n");
							


							if (m_pfBuff != OP_NULL) 
							{
								free(m_pfBuff);
								m_pfBuff = NULL;
							}
							m_nfSize = 0;
							m_nOffset = 0;
							m_strfName = "";
							
							OutText("Send Upload Done Command.\n");
							comm_send_command(Cmd, sizeof(Cmd));	
						}
					//}
				//	else
					//{
						//error
				//		OutText("Receive Upload Data Error!\n");
				//	}
				}


				////////////////////////////////////////////////////////////////		
				//receive download header rsp
				//melody
				if ((tempBuffer[0] == 0x01) && (tempBuffer[1] == 0x23) && (tempBuffer[2] == 0x00))
				{
					OP_UINT8 Cmd[1530] = {0x01, 0x04};
					
					m_nOffset = 0;
					if (m_nfSize > 1500)
					{
						m_nLength = 1500;
					}
					else
					{
						m_nLength = m_nfSize;
					}
					memcpy(Cmd+2, &m_nOffset, 4);
					memcpy(Cmd+6, &m_nLength, 4);
					memcpy(Cmd+10, m_pfBuff, m_nLength);
					
					OutText("Send Download Data Command.\n");
					comm_send_command(Cmd, m_nLength+10);
				}
			
				
				///////////////////////////////////////////////////////////	
				//receive download data rsp
				//melody
				if ((tempBuffer[0] == 0x01) && (tempBuffer[1] == 0x24) && (tempBuffer[2] == 0x00))
				{
					OP_UINT8 Cmd[1530] = {0x01, 0x04};

⌨️ 快捷键说明

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