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

📄 cechat.cpp

📁 基于WINCE的硬件ID转换程序
💻 CPP
📖 第 1 页 / 共 3 页
字号:
//======================================================================
// CeChat - A Windows CE communication demo
//
// Written for the book Programming Windows CE
// Copyright (C) 2003 Douglas Boling
//======================================================================
#include <windows.h>				 // For all that Windows stuff
#include <string.h>
#include <stdio.h>
#include <commctrl.h>				 // Command bar includes
#include "CeChat.h" 				 // Program-specific stuff
#include "GPS.h"

#if defined(WIN32_PLATFORM_PSPC)
#include <aygshell.h>				 // Add Pocket PC includes.
#pragma comment( lib, "aygshell" )	 // Link Pocket PC lib for menu bar.
#endif
//----------------------------------------------------------------------
// Global data
//
const TCHAR szAppName[] = TEXT ("CeChat");
HINSTANCE hInst;					 // Program instance handle.

BOOL fContinue = TRUE;
HANDLE hComPort = INVALID_HANDLE_VALUE;
int nSpeed = CBR_38400;
int nLastDev = -1;

#if defined(WIN32_PLATFORM_PSPC) && (_WIN32_WCE >= 300)
SHACTIVATEINFO sai;
#endif


HANDLE m_hCom;

//#define DEBUG_GPS 1    //gps debug message

#define        MAXBLOCK 1024
#define        XON 0x11
#define        XOFF 0x13
#define  GPS_BUF_SIZE   1024     //GPS信息最大长度
unsigned int gps_rec_num=0;	//gps数据接收指针
int		iGpsTime, iGpsLong, iGpsLat;
int		iGSV_Index = 0; //卫星信息序号

TCHAR sShowGPS_Byte[64];
#define MAX_STAR_NUM 4
#define STAR_TEXT_SIZE_X 180
#define STAR_TEXT_SIZE_Y 25
#define START_POS_X 70
#define START_POS_Y 4

void dr_vGPS_DataProcess(HWND hWnd,char *GPS_DATA);
int i_ShowGpsData(HWND hWnd,char *GPS_DATA);//显示NMEA-0183信息
int i_ShowDetailGpsInfo(HWND hWnd,ORI_GPSMsg_t *pstGpsMsg);//显示详细GPS信息
static BYTE SrIoControl(DWORD dwCmd, BYTE Input, BYTE Output);

char gps_send_buffer[GPS_BUF_SIZE];	//gps数据发送缓冲区
char gps_rec_buffer[GPS_BUF_SIZE];	//gps数据交换区域
char gps_uart_reg_rec_value[GPS_BUF_SIZE];	//串口输入缓冲区数据
ORI_GPSMsg_t ORI_Gps;

int dr_ReadCom(HWND hWnd, HANDLE hComm, DWORD &nBytesRead, int ReadTime);//读串口操作
int Calc_checksum(char* sz, int nCount);//CRC校验

static HANDLE	m_hGio = INVALID_HANDLE_VALUE;	// GIO句柄
HANDLE g_hSendEvent = INVALID_HANDLE_VALUE;
HANDLE g_hEnterEvent = INVALID_HANDLE_VALUE;
HANDLE hReadThread = INVALID_HANDLE_VALUE;
HANDLE hShowGPSInfoThread = INVALID_HANDLE_VALUE; //show gps message thread handler
HWND hRecStarSNRInfo0;

HWND hRecStarPRNInfo0;

HWND hRecStarELEVInfo0;

HWND hRecStarAZIMUTHInfo0;

// Message dispatch table for MainWindowProc
const struct decodeUINT MainMessages[] = {
	WM_CREATE, DoCreateMain,
	WM_SIZE, DoSizeMain,
	WM_COMMAND, DoCommandMain,
	WM_SETTINGCHANGE, DoPocketPCShell,
	WM_ACTIVATE, DoPocketPCShell,
	WM_SETFOCUS, DoSetFocusMain,
	WM_DESTROY, DoDestroyMain,
};
// Command Message dispatch for MainWindowProc
const struct decodeCMD MainCommandItems[] = {
	IDC_COMPORT, DoMainCommandComPort,
	ID_SENDBTN, DoMainCommandSendText,
	ID_ENTERBTN, DoMainCommandEnterText,
	IDM_EXIT, DoMainCommandExit,
	IDM_ABOUT, DoMainCommandAbout,
};

HANDLE hMem;      // Storage for Inbound Strings
DWORD  dwCount;   // Size of current String

void  VtoC(unsigned char *a,unsigned char *cov );
DWORD ConvertHWID_to_DeviceID(unsigned char *rdata);

BOOL GetDeviceID( void );
unsigned char CHARtoINT(char str);

HANDLE hENC=NULL; 
//char GetID[20]={0}; 
DWORD Count=0;
int iFlagError=0;

//返回TRUE表示执行成功,FALSE表示执行失败
BOOL GetDeviceID( void )
{
	LPSTR GetID = new char[21];   
	LPWSTR wpstr = new WORD[21];
//	LPVOID GetID; 
#if 1
	hENC=CreateFile ( TEXT ("ENC1:"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);

	if(hENC==NULL)
		return FALSE;

	if(0 == ReadFile(  hENC,  GetID,  20, &Count, NULL))
		return FALSE;
#else
	GetID="12345123451234512345";
#endif

	MultiByteToWideChar(CP_ACP,0,GetID,-1,wpstr,20);
	SetWindowText(hRecStarELEVInfo0, wpstr);

	CloseHandle(hENC );
	return TRUE; 
}

//======================================================================
// Program entry point
//
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
					LPWSTR lpCmdLine, int nCmdShow) {
	HWND hwndMain;
	HACCEL hAccel;
	MSG msg;
	int rc = 0;

	// Initialize this instance.
	hwndMain = InitInstance (hInstance, lpCmdLine, nCmdShow);
	if (hwndMain == 0)
		return 0x10;

	// Load accelerator table.
	hAccel = LoadAccelerators (hInst, MAKEINTRESOURCE (ID_ACCEL));

	// Application message loop
	while (GetMessage (&msg, NULL, 0, 0)) {
		if (!TranslateAccelerator (hwndMain, hAccel, &msg)) {
			TranslateMessage (&msg);
			DispatchMessage (&msg);
		}
	}
	// Instance cleanup
	return TermInstance (hInstance, msg.wParam);
}
//----------------------------------------------------------------------
// InitInstance - Instance initialization
//
HWND InitInstance (HINSTANCE hInstance, LPWSTR lpCmdLine, int nCmdShow){
	HWND hWnd;
	HANDLE hThread;
	WNDCLASS wc;
	INITCOMMONCONTROLSEX icex;

	// Save program instance handle in global variable.
	hInst = hInstance;

#if defined(WIN32_PLATFORM_PSPC)
	// If Pocket PC, allow only one instance of the application.
	HWND hWnd = FindWindow (szAppName, NULL);
	if (hWnd) {
		SetForegroundWindow ((HWND)(((DWORD)hWnd) | 0x01));    
		return 0;
	}
#endif
// Register application main window class.
	wc.style = 0;							  // Window style
	wc.lpfnWndProc = MainWndProc;			  // Callback function
	wc.cbClsExtra = 0;						  // Extra class data
	wc.cbWndExtra = 0;						  // Extra window data
	wc.hInstance = hInstance;				  // Owner handle
	wc.hIcon = NULL;						  // Application icon
	wc.hCursor = LoadCursor (NULL, IDC_ARROW);// Default cursor
	wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
	wc.lpszMenuName =  NULL;				  // Menu name
	wc.lpszClassName = szAppName;			  // Window class name

	if (RegisterClass (&wc) == 0) return 0;

	// Load the command bar common control class.
	icex.dwSize = sizeof (INITCOMMONCONTROLSEX);
	icex.dwICC = ICC_BAR_CLASSES;
	InitCommonControlsEx (&icex);

	// Create unnamed auto-reset event initially false.
	g_hSendEvent = CreateEvent (NULL, FALSE, FALSE, NULL);

	// Create unnamed auto-reset event initially false.
	g_hEnterEvent = CreateEvent (NULL, FALSE, FALSE, NULL);

	// Create main window.
	hWnd = CreateWindow (szAppName, TEXT ("CeChat"),
						 WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT,
						 CW_USEDEFAULT, CW_USEDEFAULT, NULL,
						 NULL, hInstance, NULL);
	// Return fail code if window not created.
	if (!IsWindow (hWnd)) return 0;

	// Create write thread. Read thread created when port opened.
	hThread = CreateThread (NULL, 0, SendThread, hWnd, 0, NULL);
	if (hThread)
		CloseHandle (hThread);
	else {
		DestroyWindow (hWnd);
		return 0;
	}
	GetDeviceID();
	// Standard show and update calls
	ShowWindow (hWnd, nCmdShow);
	UpdateWindow (hWnd);
	return hWnd;
}
//----------------------------------------------------------------------
// TermInstance - Program cleanup
//
int TermInstance (HINSTANCE hInstance, int nDefRC) {
	HANDLE hPort = hComPort;

	fContinue = FALSE;

	hComPort = INVALID_HANDLE_VALUE;
	if (hPort != INVALID_HANDLE_VALUE)
		CloseHandle (hPort);

	if (g_hSendEvent != INVALID_HANDLE_VALUE) {
		PulseEvent (g_hSendEvent);
		Sleep(100);
		CloseHandle (g_hSendEvent);
	}
	return nDefRC;
}
//======================================================================
// Message handling procedures for MainWindow
//----------------------------------------------------------------------
// MainWndProc - Callback function for application window
//
LRESULT CALLBACK MainWndProc (HWND hWnd, UINT wMsg, WPARAM wParam,
							  LPARAM lParam) {
	int i;
	//
	// Search message list to see if we need to handle this
	// message.  If in list, call procedure.
	//
	for (i = 0; i < dim(MainMessages); i++) {
		if (wMsg == MainMessages[i].Code)
			 return (*MainMessages[i].Fxn)(hWnd, wMsg, wParam, lParam);
	}
	return DefWindowProc (hWnd, wMsg, wParam, lParam);
}
//----------------------------------------------------------------------
// DoCreateMain - Process WM_CREATE message for window.
//
LRESULT DoCreateMain (HWND hWnd, UINT wMsg, WPARAM wParam,
					  LPARAM lParam) {
	HWND hwndCB, hC1, hC2, hC3;
	int  i;
	TCHAR szFirstDev[32];
	LPCREATESTRUCT lpcs = (LPCREATESTRUCT) lParam;

#if defined(WIN32_PLATFORM_PSPC) && (_WIN32_WCE >= 300)
	memset (&sai, 0, sizeof (sai));
	sai.cbSize = sizeof (sai);
	{
	SHMENUBARINFO mbi;						// For Pocket PC, create
	memset(&mbi, 0, sizeof(SHMENUBARINFO)); // menu bar so that we
	mbi.cbSize = sizeof(SHMENUBARINFO); 	// have a sip button.
	mbi.hwndParent = hWnd;
	mbi.dwFlags = SHCMBF_EMPTYBAR;
	SHCreateMenuBar(&mbi);
	SetWindowPos (hWnd, 0, 0, 0, lpcs->cx, lpcs->cy-26, 
				  SWP_NOZORDER | SWP_NOMOVE);
	}
#endif	  
   
   // Create a command bar.
	hwndCB = CommandBar_Create (hInst, hWnd, IDC_CMDBAR);
	CommandBar_InsertMenubar (hwndCB, hInst, ID_MENU, 0);

	// Insert the COM port combo box.
	CommandBar_InsertComboBox (hwndCB, hInst, 140, CBS_DROPDOWNLIST,
							   IDC_COMPORT, 1);
	FillComComboBox (hWnd);

	// Add exit button to command bar.
	CommandBar_AddAdornments (hwndCB, 0, 0);

	// Create child windows. They will be positioned in WM_SIZE.
	// Create receive text window.
	hC1 = CreateWindowEx (WS_EX_CLIENTEDGE, TEXT ("edit"),
						  TEXT (""), WS_VISIBLE | WS_CHILD |
						  WS_VSCROLL | ES_MULTILINE | ES_AUTOHSCROLL |
						  ES_READONLY, 0, 0, 10, 10, hWnd,
						  (HMENU)ID_RCVTEXT, hInst, NULL);

	//create edit box for star sn
//**************************************************************************
	hRecStarPRNInfo0 = CreateWindowEx (WS_EX_CLIENTEDGE, TEXT ("edit"),
							  TEXT (""), WS_VISIBLE | WS_CHILD |
							  BS_DEFPUSHBUTTON, 220, 100, 50, 30,
							  hWnd, (HMENU)ID_STARPRNINDEX0, hInst, NULL);   
//**************************************************************************
	hRecStarSNRInfo0 = CreateWindowEx (WS_EX_CLIENTEDGE, TEXT ("edit"),
							  TEXT (""), WS_VISIBLE | WS_CHILD |
							  BS_DEFPUSHBUTTON, 220, 100, 50, 30,
							  hWnd, (HMENU)ID_STARSNRINDEX0, hInst, NULL);   
//**************************************************************************
	hRecStarELEVInfo0 = CreateWindowEx (WS_EX_CLIENTEDGE, TEXT ("edit"),
							  TEXT (""), WS_VISIBLE | WS_CHILD |
							  BS_DEFPUSHBUTTON, 220, 100, 50, 30,
							  hWnd, (HMENU)ID_STARELEVINDEX0, hInst, NULL);   
//**************************************************************************
	hRecStarAZIMUTHInfo0 = CreateWindowEx (WS_EX_CLIENTEDGE, TEXT ("button"),
						  TEXT ("&Convert"), WS_VISIBLE | WS_CHILD |
						  BS_DEFPUSHBUTTON, 0, 0, 10, 10,
						  hWnd, (HMENU)ID_ENTERBTN, hInst, NULL);

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

	// Create send text window.
	hC2 = CreateWindowEx (WS_EX_CLIENTEDGE, TEXT ("edit"),
						  TEXT (""), WS_VISIBLE | WS_CHILD,
						  0, 0, 10, 10,  hWnd, (HMENU)ID_SENDTEXT, 
							 hInst, NULL);
	// Create send text window.
	hC3 = CreateWindowEx (WS_EX_CLIENTEDGE, TEXT ("button"),
						  TEXT ("&Send"), WS_VISIBLE | WS_CHILD |
						  BS_DEFPUSHBUTTON, 0, 0, 10, 10,
						  hWnd, (HMENU)ID_SENDBTN, hInst, NULL);
	// Destroy frame if window not created.
	if (!IsWindow (hC1) || !IsWindow (hC2) || !IsWindow (hC3)) {
		DestroyWindow (hWnd);
		return 0;
	}

	if (m_hGio == INVALID_HANDLE_VALUE)
		m_hGio = CreateFile(L"GIO1:", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

	SrIoControl(CMD_GPS_ENON, 0, 0);

	// Open a COM port.
	for (i = 0; i < 10; i++) {
		if (SendDlgItemMessage (hwndCB, IDC_COMPORT, CB_GETLBTEXT, i,
							(LPARAM)szFirstDev) == CB_ERR)
			break;
		if (InitCommunication (hWnd, szFirstDev) !=
			INVALID_HANDLE_VALUE) {
			SendDlgItemMessage (hwndCB, IDC_COMPORT, CB_SETCURSEL, i,
								(LPARAM)szFirstDev);
			break;
		}
	}

	return 0;
}
//----------------------------------------------------------------------
// DoSizeMain - Process WM_SIZE message for window.
//
LRESULT DoSizeMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam){
	RECT rect;
	int i=0;
	HDC hdc;
	RECT rt;
	PAINTSTRUCT ps;
	// Adjust the size of the client rect to take into account
	// the command bar height.
	GetClientRect (hWnd, &rect);
	rect.top += CommandBar_Height (GetDlgItem (hWnd, IDC_CMDBAR));
//************************************************************************************
//ID_STARSNRINDEX2
	SetWindowPos (GetDlgItem (hWnd, ID_STARPRNINDEX0), NULL, START_POS_X,
				  rect.top + START_POS_Y, STAR_TEXT_SIZE_X, STAR_TEXT_SIZE_Y, SWP_NOZORDER);
//************************************************************************************
//ID_STARPRNINDEX0
	SetWindowPos (GetDlgItem (hWnd, ID_STARSNRINDEX0), NULL, START_POS_X,
				  rect.top + START_POS_Y + STAR_TEXT_SIZE_Y + 5, STAR_TEXT_SIZE_X, STAR_TEXT_SIZE_Y, SWP_NOZORDER);
//************************************************************************************
//ID_STARELEVINDEX
	SetWindowPos (GetDlgItem (hWnd, ID_STARELEVINDEX0), NULL, START_POS_X,
				  rect.top + START_POS_Y + (STAR_TEXT_SIZE_Y + 5) * 2, STAR_TEXT_SIZE_X, STAR_TEXT_SIZE_Y, SWP_NOZORDER);
//************************************************************************************
//ID_STARAZIMUTHINDEX

	SetWindowPos (GetDlgItem (hWnd, ID_ENTERBTN), NULL, (STAR_TEXT_SIZE_X - 2) + START_POS_X,
				  rect.top + START_POS_Y, STAR_TEXT_SIZE_X/3, STAR_TEXT_SIZE_Y, SWP_NOZORDER);

//************************************************************************************
	SetWindowPos (GetDlgItem (hWnd, ID_RCVTEXT), NULL, rect.left,
				  rect.top + START_POS_Y + (STAR_TEXT_SIZE_Y + 5) * 4, (rect.right - rect.left),
				  rect.bottom - (START_POS_Y + (STAR_TEXT_SIZE_Y + 5) * 5)-20, SWP_NOZORDER);

	SetWindowPos (GetDlgItem (hWnd, ID_SENDTEXT), NULL, rect.left,
				  rect.bottom - 25, (rect.right - rect.left) - 50,
				  25, SWP_NOZORDER);

	SetWindowPos (GetDlgItem (hWnd, ID_SENDBTN), NULL,
				  (rect.right - rect.left) - 50, rect.bottom - 25,
				  50, 25, SWP_NOZORDER);

	hdc = BeginPaint(hWnd, &ps);
	GetClientRect(hWnd, &rt);

	ExtTextOut(hdc, 1, rect.top + 3, ETO_CLIPPED, &rt, _T("Hardware ID:"),_tcslen(_T("Hardware ID:")),NULL);  
	ExtTextOut(hdc, 1, rect.top + START_POS_Y + (STAR_TEXT_SIZE_Y + 5) + 3 , ETO_CLIPPED, &rt, _T("Device ID:"),_tcslen(_T("Device ID:")),NULL);  
	ExtTextOut(hdc, 1, rect.top + START_POS_Y + (STAR_TEXT_SIZE_Y + 5) * 2 + 3, ETO_CLIPPED, &rt, _T("Current ID:"),_tcslen(_T("Current ID:")),NULL);  
//	ExtTextOut(hdc, 1, rect.top + START_POS_Y + (STAR_TEXT_SIZE_Y + 5) * 3 + 3, ETO_CLIPPED, &rt, _T("AZ"),_tcslen(_T("AZ")),NULL);  
	
	EndPaint(hWnd, &ps);
	return 0;
}
//----------------------------------------------------------------------
// DoPocketPCShell - Process Pocket PC required messages.
//
LRESULT DoPocketPCShell (HWND hWnd, UINT wMsg, WPARAM wParam,
						 LPARAM lParam) {
#if defined(WIN32_PLATFORM_PSPC) && (_WIN32_WCE >= 300)
	if (wMsg == WM_SETTINGCHANGE) 
		return SHHandleWMSettingChange(hWnd, wParam, lParam, &sai);
	if (wMsg == WM_ACTIVATE) 
		return SHHandleWMActivate(hWnd, wParam, lParam, &sai, 0);
#endif 
	return 0;
}
//----------------------------------------------------------------------
// DoFocusMain - Process WM_SETFOCUS message for window.
//
LRESULT DoSetFocusMain (HWND hWnd, UINT wMsg, WPARAM wParam,
						LPARAM lParam) {
	SetFocus (GetDlgItem (hWnd, ID_SENDTEXT));
	return 0;
}
//----------------------------------------------------------------------
// DoCommandMain - Process WM_COMMAND message for window.
//
LRESULT DoCommandMain (HWND hWnd, UINT wMsg, WPARAM wParam,
					   LPARAM lParam) {
	WORD	idItem, wNotifyCode;
	HWND hwndCtl;
	int  i;

	// Parse the parameters.
	idItem = (WORD) LOWORD (wParam);
	wNotifyCode = (WORD) HIWORD (wParam);
	hwndCtl = (HWND) lParam;

	// Call routine to handle control message.
	for (i = 0; i < dim(MainCommandItems); i++) {
		if (idItem == MainCommandItems[i].Code)
			 return (*MainCommandItems[i].Fxn)(hWnd, idItem, hwndCtl,
											   wNotifyCode);
	}
	return 0;
}
//----------------------------------------------------------------------
// DoDestroyMain - Process WM_DESTROY message for window.
//
LRESULT DoDestroyMain (HWND hWnd, UINT wMsg, WPARAM wParam,
					   LPARAM lParam) {
	PostQuitMessage (0);
	return 0;
}
//======================================================================
// Command handler routines
//----------------------------------------------------------------------
// DoMainCommandExit - Process Program Exit command.
//
LPARAM DoMainCommandExit (HWND hWnd, WORD idItem, HWND hwndCtl,
						  WORD wNotifyCode) {
	SendMessage (hWnd, WM_CLOSE, 0, 0);
	return 0;
}
//----------------------------------------------------------------------
// DoMainCommandComPort - Process the COM port combo box commands.
//
LPARAM DoMainCommandComPort (HWND hWnd, WORD idItem, HWND hwndCtl,
							 WORD wNotifyCode) {
	int i;
	TCHAR szDev[32];

	if (wNotifyCode == CBN_SELCHANGE) {
		i = SendMessage (hwndCtl, CB_GETCURSEL, 0, 0);
		if (i != nLastDev) {
			SendMessage (hwndCtl, CB_GETLBTEXT, i, (LPARAM)szDev);
			InitCommunication (hWnd, szDev);
			SetFocus (GetDlgItem (hWnd, ID_SENDTEXT));
		}
	}
	return 0;
}
//----------------------------------------------------------------------
// DoMainCommandEnterText - Process the Send text button.
//
LPARAM DoMainCommandEnterText (HWND hWnd, WORD idItem, HWND hwndCtl,
							  WORD wNotifyCode) {

⌨️ 快捷键说明

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