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

📄 pocketcommdlg.cpp

📁 完整的windows ce下PPC通讯测试程序.
💻 CPP
字号:
// PocketCommDlg.cpp : implementation file
//

#include "stdafx.h"
#include "PocketComm.h"
#include "PocketCommDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CPocketCommDlg dialog

CPocketCommDlg::CPocketCommDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CPocketCommDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPocketCommDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CPocketCommDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPocketCommDlg)
	DDX_Control(pDX, IDC_STATIC_MS, m_TextMS);
	DDX_Control(pDX, IDC_STATIC_AUTOSEND, m_TextAutoSend);
	DDX_Control(pDX, IDC_BUTTON_OPENCLOSE, m_Open);
	DDX_Control(pDX, IDC_EDIT_SENDBUFFER, m_SendBuffer);
	DDX_Control(pDX, IDC_EDIT_AUTOTIME, m_AutoTime);
	DDX_Control(pDX, IDC_COMBO_PARITY, m_Parity);
	DDX_Control(pDX, IDC_COMBO_BAUDRATE, m_BaudRate);
	DDX_Control(pDX, IDC_CHECK_AUTOSEND, m_AutoEnable);
	DDX_Control(pDX, IDC_EDIT_RECEIVEBUFFER, m_ReceiveBuffer);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CPocketCommDlg, CDialog)
	//{{AFX_MSG_MAP(CPocketCommDlg)
	ON_BN_CLICKED(IDC_BUTTON_OPENCLOSE, OnButtonOpenclose)
	ON_BN_CLICKED(IDC_CHECK_AUTOSEND, OnCheckAutosend)
	ON_CBN_CLOSEUP(IDC_COMBO_PARITY, OnCloseupComboParity)
	ON_CBN_CLOSEUP(IDC_COMBO_BAUDRATE, OnCloseupComboBaudrate)
	ON_EN_SETFOCUS(IDC_EDIT_SENDBUFFER, OnSetfocusEditSendbuffer)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_EN_CHANGE(IDC_EDIT_AUTOTIME, OnChangeEditAutotime)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

BOOL gbStop = FALSE;
CSerial *pSerial;
HWND ghSend,ghReceive;
BOOL gbOpen = FALSE;
BOOL gbSend = FALSE;
BOOL gbAuto = FALSE;
int gnms = 1000;

// parity verify
BYTE gParity;
// Data bits
BYTE gDataBits;
// Stop Bits
BYTE gStopBits;
// Baud Rate
int gBaudRate;
//////////////////////////////////////////////////////////////////////////////
// n : 0 numric    1 : hand input
BOOL DisplaySIP(BOOL fShow,int n)
{
	SIPINFO si;
	CLSID cl;
	memset(&si, 0, sizeof(si));
	si.cbSize = sizeof(SIPINFO);

	if(!SHSipInfo(SPI_GETSIPINFO, 0, &si, 0)) 
	return FALSE;

	if(fShow)
	si.fdwFlags |= SIPF_ON;
	else
	si.fdwFlags &= ~SIPF_ON;

	SHSipInfo(SPI_SETSIPINFO, 0, &si, 0);

	SHSipInfo(SPI_GETCURRENTIM,0,&cl,0);
	// py DF2BF912-1A9A-11D2-8F89-00C04FAC52F9
	// hand 7D434AC1-EF63-11D1-90F0-0000F81E0CE9
	// ppc 2003 {9E06C6B8-FE4D-4206-9A5B-6E4C1B8B6AD7}

	unsigned char *ps,*pt;
	unsigned char lsNum[16] =  {0xdf,0x2b,0xf9,0x12,0x1a,0x9a,0x11,0xd2,0x8f,0x89,0x00,0xc0,0x4f,0xac,0x52,0xf9};
	//unsigned char lsHand[16] = {0x7d,0x43,0x4a,0xc1,0xef,0x63,0x11,0xd1,0x90,0xf0,0x00,0x00,0xf8,0x1e,0x0c,0xe9};
	unsigned char lsHand[16] = {0x9E,0x06,0xC6,0xB8,0xFE,0x4D,0x42,0x06,0x9A,0x5B,0x6E,0x4C,0x1B,0x8B,0x6A,0xD7};
	if(n == 0)
	{
		memcpy((unsigned char *)&cl,lsNum,16);
		pt = lsNum;
	}
	else
	{
		memcpy((unsigned char *)&cl,lsHand,16);
		pt = lsHand;
	}
	ps = (unsigned char *)&cl;
	*(ps+0) = *(pt+3);
	*(ps+1) = *(pt+2);
	*(ps+2) = *(pt+1);
	*(ps+3) = *(pt+0);

	*(ps+4) = *(pt+5);
	*(ps+5) = *(pt+4);

	*(ps+6) = *(pt+7);
	*(ps+7) = *(pt+6);

	SHSipInfo(SPI_SETCURRENTIM,0,&cl,0);

	return TRUE;
}

// 
char *BinToHexAscii(char *pbin,char *pascii,int BinLen)
{
   int i;

   pascii[0]=0;
   for(i=0;i<BinLen;i++)
      sprintf(pascii+(i<<1),"%2.2X",(unsigned char)pbin[i]);
   return pascii;
}

int CompressToBCD(char *sbcd,unsigned char *scompress,int sbcdlen)
{
	int i;
	UCHAR uch1,uch2;
	for(i=0;i<sbcdlen/2;i++)
	{
		uch1 = *(sbcd+2*i+0);
		if(uch1 >= 'a')
		{
			uch1 = uch1 - 0x61 + 10;
		}
		else
		{
			if(uch1 >= 'A')
			{
				uch1 = uch1 - 0x41 + 10;
			}
			else
			{
				uch1 = uch1 - 0x30;
			}
		}
		uch2 = *(sbcd+2*i+1);
		if(uch2 >= 'a')
		{
			uch2 = uch2 - 0x61 + 10;
		}
		else
		{
			if(uch2 >= 'A')
			{
				uch2 = uch2 - 0x41 + 10;
			}
			else
			{
				uch2 = uch2 - 0x30;
			}
		}
		*(scompress+i) = (unsigned char)((uch1)<<4) + (unsigned char)(uch2);
	}

	return sbcdlen/2;
}

UINT SendThread(LPVOID data)
{
	UCHAR lusBuffer[500];
	int lnReadLen;
	char lsShow[1000];
	char lsOld[1000];
	CString ls;
	CString lst;
	CSerial lSerial;
	int i;
	char lsEnter[3] = {0x0d,0x0a,0x0};
	BOOL lbOnce = TRUE;

	lSerial.m_BaudRate = gBaudRate;
	lSerial.m_DataBits = gDataBits;
	lSerial.m_Parity = gParity;
	lSerial.m_StopBits = gStopBits;
	lSerial.Open(1);
	for(;;)
	{
		Sleep(200);
		if(gbStop)
		{
			lSerial.Close();
			return 0;
		}

		if(gbOpen)
		{
			// send routine
			if(gbAuto)
			{
				gbSend = TRUE;
				Sleep(gnms);
			}
			if(gbSend)
			{
				if(lbOnce)
				{
					lbOnce = FALSE;
					lSerial.Close();
					Sleep(100);

					lSerial.m_BaudRate = gBaudRate;
					lSerial.m_DataBits = gDataBits;
					lSerial.m_Parity = gParity;
					lSerial.m_StopBits = gStopBits;

					lSerial.Open(1);
				}
				gbSend = FALSE;
				// get send buffer
				lnReadLen = ::GetWindowText(ghSend,(USHORT *)lusBuffer,500);
				WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK | WC_DEFAULTCHAR,(unsigned short *)lusBuffer,lnReadLen,lsOld,1000,0,0);
				lst = lsOld;
				CompressToBCD(lsOld,lusBuffer,lnReadLen);
				lSerial.SendData(lusBuffer,lnReadLen/2,TRUE);

				// send it;
			}

			// listen routine
			{
				lnReadLen = lSerial.ReadData(lusBuffer,500);
				BinToHexAscii((char *)lusBuffer,lsShow,lnReadLen);

				lst = lsShow;
				lnReadLen = lst.GetLength();
				for(i=0;i<lnReadLen;i++)
				{
					if(i%2 == 1)
					{
						ls += lst.Left(i+1).Right(2);
						ls += _T(" ");
					}
				}
				::SetWindowText(ghReceive,ls);
				if(ls.GetLength()>198)
					ls.Empty();
			}
		}
		else
		{
			lbOnce = TRUE;
		}
	}
	return 0;
}

/////////////////////////////////////////////////////////////////////////////
// CPocketCommDlg message handlers

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

	// 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
	
	CenterWindow(GetDesktopWindow());	// center to the hpc screen

	// TODO: Add extra initialization here
	this->m_BaudRate.SetCurSel(4);
	this->m_Parity.SetCurSel(0);

	gBaudRate = 2400;
	gParity = NOPARITY;
	gDataBits = 8;
	gStopBits = ONESTOPBIT;

	ghSend = this->m_SendBuffer.m_hWnd;
	ghReceive = this->m_ReceiveBuffer.m_hWnd;
	this->m_AutoTime.SetWindowText(_T("1000"));

	CWinThread *pProbeThread = AfxBeginThread(SendThread,
											NULL,
											THREAD_PRIORITY_NORMAL,
											0);

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

void CPocketCommDlg::OnButtonOpenclose() 
{
	if(!gbAuto)
	{
		if(gbOpen)
		{
			this->m_Open.SetWindowText(_T("Open"));
			gbOpen = FALSE;
		}
		else
		{
			this->m_Open.SetWindowText(_T("Close"));
			gbOpen = TRUE;
		}
	}
}

void CPocketCommDlg::OnCheckAutosend() 
{
	if(this->m_AutoEnable.GetCheck() == BST_CHECKED)
	{
		this->m_TextAutoSend.EnableWindow(FALSE);
		this->m_TextMS.EnableWindow(FALSE);
		this->m_AutoTime.EnableWindow(FALSE);
		gbAuto = TRUE;
	}
	else
	{
		this->m_TextAutoSend.EnableWindow();
		this->m_TextMS.EnableWindow();
		this->m_AutoTime.EnableWindow();
		gbAuto = FALSE;
	}
}

void CPocketCommDlg::OnCloseupComboParity() 
{
	switch(this->m_Parity.GetCurSel())
	{
	case 0:
		gParity = NOPARITY;
		break;
	case 1:
		gParity = EVENPARITY;
		break;
	case 2:
		gParity = ODDPARITY;
		break;
	}
}

void CPocketCommDlg::OnCloseupComboBaudrate() 
{
	switch(this->m_BaudRate.GetCurSel())
	{
	case 0:
		gBaudRate = 110;
		break;
	case 1:
		gBaudRate = 300;
		break;
	case 2:
		gBaudRate = 600;
		break;
	case 3:
		gBaudRate = 1200;
		break;
	case 4:
		gBaudRate = 2400;
		break;
	case 5:
		gBaudRate = 4800;
		break;
	case 6:
		gBaudRate = 9600;
		break;
	case 7:
		gBaudRate = 14400;
		break;
	case 8:
		gBaudRate = 19200;
		break;
	case 9:
		gBaudRate = 38400;
		break;
	case 10:
		gBaudRate = 56000;
		break;
	case 11:
		gBaudRate = 57600;
		break;
	case 12:
		gBaudRate = 115200;
		break;
	case 13:
		gBaudRate = 128000;
		break;
	case 14:
		gBaudRate = 256000;
		break;
	}
}

void CPocketCommDlg::OnSetfocusEditSendbuffer() 
{
	DisplaySIP(TRUE,0);
}

void CPocketCommDlg::OnButton1() 
{
	gbSend = TRUE;
}

void CPocketCommDlg::OnButton2() 
{
	this->m_SendBuffer.SetWindowText(_T(""));	
}

void CPocketCommDlg::OnButton3() 
{
	this->m_ReceiveBuffer.SetWindowText(_T(""));
}

void CPocketCommDlg::OnChangeEditAutotime() 
{
	int lnReadLen;
	UCHAR lusBuffer[500];
	char lsOld[1000];
	int lnTime=0;

	lnReadLen = this->m_AutoTime.GetWindowText((PUSHORT)lusBuffer,500);
	WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK | WC_DEFAULTCHAR,(unsigned short *)lusBuffer,lnReadLen,lsOld,1000,0,0);
	if(lnReadLen > 4)
		lnReadLen = 4;
	for(int i=0;i<lnReadLen;i++)
	{
		lnTime *= 10;
		lnTime += *(lsOld+i)-0x30;
	}

	gnms = lnTime;
}

⌨️ 快捷键说明

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