📄 xgpscomctl.cpp
字号:
// XGpscomCtl.cpp : Implementation of the CXGpscomCtrl ActiveX Control class.
#include "stdafx.h"
#include "xGpscom.h"
#include "XGpscomCtl.h"
#include "XGpscomPpg.h"
#include "Time.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
IMPLEMENT_DYNCREATE(CXGpscomCtrl, COleControl)
/////////////////////////////////////////////////////////////////////////////
// Message map
BEGIN_MESSAGE_MAP(CXGpscomCtrl, COleControl)
//{{AFX_MSG_MAP(CXGpscomCtrl)
// NOTE - ClassWizard will add and remove message map entries
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG_MAP
ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// Dispatch map
BEGIN_DISPATCH_MAP(CXGpscomCtrl, COleControl)
//{{AFX_DISPATCH_MAP(CXGpscomCtrl)
DISP_PROPERTY_EX(CXGpscomCtrl, "XComName", GetXComName, SetXComName, VT_I2)
DISP_PROPERTY_EX(CXGpscomCtrl, "XfParity", GetXfParity, SetXfParity, VT_BOOL)
DISP_PROPERTY_EX(CXGpscomCtrl, "ByteSize", GetByteSize, SetByteSize, VT_I2)
DISP_PROPERTY_EX(CXGpscomCtrl, "XParity", GetXParity, SetXParity, VT_I2)
DISP_PROPERTY_EX(CXGpscomCtrl, "XStopBits", GetXStopBits, SetXStopBits, VT_I2)
DISP_PROPERTY_EX(CXGpscomCtrl, "XBaudRate", GetXBaudRate, SetXBaudRate, VT_I4)
DISP_FUNCTION(CXGpscomCtrl, "setupcom", setupcom, VT_BOOL, VTS_NONE)
DISP_FUNCTION(CXGpscomCtrl, "setsystime", setsystime, VT_BOOL, VTS_NONE)
DISP_FUNCTION(CXGpscomCtrl, "TD2000", TD2000, VT_BOOL, VTS_NONE)
//}}AFX_DISPATCH_MAP
DISP_FUNCTION_ID(CXGpscomCtrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
END_DISPATCH_MAP()
/////////////////////////////////////////////////////////////////////////////
// Event map
BEGIN_EVENT_MAP(CXGpscomCtrl, COleControl)
//{{AFX_EVENT_MAP(CXGpscomCtrl)
// NOTE - ClassWizard will add and remove event map entries
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_EVENT_MAP
END_EVENT_MAP()
/////////////////////////////////////////////////////////////////////////////
// Property pages
// TODO: Add more property pages as needed. Remember to increase the count!
BEGIN_PROPPAGEIDS(CXGpscomCtrl, 1)
PROPPAGEID(CXGpscomPropPage::guid)
END_PROPPAGEIDS(CXGpscomCtrl)
/////////////////////////////////////////////////////////////////////////////
// Initialize class factory and guid
IMPLEMENT_OLECREATE_EX(CXGpscomCtrl, "XGPSCOM.XGpscomCtrl.1",
0xc4a8c6a1, 0x7248, 0x4878, 0x9b, 0xb5, 0xc1, 0xa2, 0xa1, 0xfd, 0x21, 0x9d)
/////////////////////////////////////////////////////////////////////////////
// Type library ID and version
IMPLEMENT_OLETYPELIB(CXGpscomCtrl, _tlid, _wVerMajor, _wVerMinor)
/////////////////////////////////////////////////////////////////////////////
// Interface IDs
const IID BASED_CODE IID_DXGpscom =
{ 0xcb7886b9, 0x3442, 0x47cb, { 0x9b, 0x66, 0x9, 0x8c, 0x87, 0x49, 0xd3, 0xd2 } };
const IID BASED_CODE IID_DXGpscomEvents =
{ 0x44d29056, 0xb1fd, 0x45e8, { 0xaa, 0xa8, 0xaa, 0xde, 0x2b, 0xb6, 0x75, 0x3e } };
/////////////////////////////////////////////////////////////////////////////
// Control type information
static const DWORD BASED_CODE _dwXGpscomOleMisc =
OLEMISC_ACTIVATEWHENVISIBLE |
OLEMISC_SETCLIENTSITEFIRST |
OLEMISC_INSIDEOUT |
OLEMISC_CANTLINKINSIDE |
OLEMISC_RECOMPOSEONRESIZE;
IMPLEMENT_OLECTLTYPE(CXGpscomCtrl, IDS_XGPSCOM, _dwXGpscomOleMisc)
/////////////////////////////////////////////////////////////////////////////
// CXGpscomCtrl::CXGpscomCtrlFactory::UpdateRegistry -
// Adds or removes system registry entries for CXGpscomCtrl
BOOL CXGpscomCtrl::CXGpscomCtrlFactory::UpdateRegistry(BOOL bRegister)
{
// TODO: Verify that your control follows apartment-model threading rules.
// Refer to MFC TechNote 64 for more information.
// If your control does not conform to the apartment-model rules, then
// you must modify the code below, changing the 6th parameter from
// afxRegApartmentThreading to 0.
if (bRegister)
return AfxOleRegisterControlClass(
AfxGetInstanceHandle(),
m_clsid,
m_lpszProgID,
IDS_XGPSCOM,
IDB_XGPSCOM,
afxRegApartmentThreading,
_dwXGpscomOleMisc,
_tlid,
_wVerMajor,
_wVerMinor);
else
return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
}
/////////////////////////////////////////////////////////////////////////////
// CXGpscomCtrl::CXGpscomCtrl - Constructor
CXGpscomCtrl::CXGpscomCtrl()
{
InitializeIIDs(&IID_DXGpscom, &IID_DXGpscomEvents);
// TODO: Initialize your control's instance data here.
xBaudRate=9600;
xfParity=TRUE;
xByteSize=8;
xParity=NOPARITY;
xStopBits=ONESTOPBIT;
ComName=0;
}
/////////////////////////////////////////////////////////////////////////////
// CXGpscomCtrl::~CXGpscomCtrl - Destructor
CXGpscomCtrl::~CXGpscomCtrl()
{
// TODO: Cleanup your control's instance data here.
}
/////////////////////////////////////////////////////////////////////////////
// CXGpscomCtrl::OnDraw - Drawing function
void CXGpscomCtrl::OnDraw(
CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{
// TODO: Replace the following code with your own drawing code.
pdc->FillRect(rcBounds, CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH)));
pdc->Ellipse(rcBounds);
}
/////////////////////////////////////////////////////////////////////////////
// CXGpscomCtrl::DoPropExchange - Persistence support
void CXGpscomCtrl::DoPropExchange(CPropExchange* pPX)
{
ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
COleControl::DoPropExchange(pPX);
// TODO: Call PX_ functions for each persistent custom property.
}
/////////////////////////////////////////////////////////////////////////////
// CXGpscomCtrl::OnResetState - Reset control to default state
void CXGpscomCtrl::OnResetState()
{
COleControl::OnResetState(); // Resets defaults found in DoPropExchange
// TODO: Reset any other control state here.
}
/////////////////////////////////////////////////////////////////////////////
// CXGpscomCtrl::AboutBox - Display an "About" box to the user
void CXGpscomCtrl::AboutBox()
{
CDialog dlgAbout(IDD_ABOUTBOX_XGPSCOM);
dlgAbout.DoModal();
}
/////////////////////////////////////////////////////////////////////////////
// CXGpscomCtrl message handlers
BOOL CXGpscomCtrl::setupcom()
{
// TODO: Add your dispatch handler code here
memset(&m_osRead, 0, sizeof(OVERLAPPED));
memset(&m_osWrite, 0, sizeof(OVERLAPPED));
CString Comn;
switch(ComName)
{
case 0:
Comn="COM1";
break;
case 1:
Comn="COM2";
break;
case 2:
Comn="COM3";
break;
case 3:
Comn="COM4";
}
hCom=CreateFile(Comn,GENERIC_READ|GENERIC_WRITE,0,
NULL,OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,NULL);
SetupComm(hCom,1024,512);
COMMTIMEOUTS TimeOuts;
SetCommMask(hCom,EV_RXCHAR);
TimeOuts.ReadIntervalTimeout=1000;
TimeOuts.ReadTotalTimeoutMultiplier=1000;
TimeOuts.ReadTotalTimeoutConstant=1000;
TimeOuts.WriteTotalTimeoutMultiplier=500;
TimeOuts.WriteTotalTimeoutConstant=2000;
SetCommTimeouts(hCom,&TimeOuts);
DCB dcb;
GetCommState(hCom,&dcb);
dcb.fBinary=TRUE;
dcb.BaudRate=xBaudRate;
dcb.fParity=xfParity;
dcb.ByteSize=(unsigned char)xByteSize;
dcb.Parity=(unsigned char)xParity;//偶校验
dcb.StopBits=(unsigned char)xStopBits;
SetCommState(hCom,&dcb);
PurgeComm(hCom,PURGE_TXCLEAR);//清空缓冲区
PurgeComm(hCom,PURGE_RXCLEAR);
return TRUE;
}
short CXGpscomCtrl::GetXComName()
{
// TODO: Add your property handler here
return ComName;
}
void CXGpscomCtrl::SetXComName(short nNewValue)
{
// TODO: Add your property handler here
ComName=nNewValue;
SetModifiedFlag();
}
BOOL CXGpscomCtrl::GetXfParity()
{
// TODO: Add your property handler here
return xfParity;
}
void CXGpscomCtrl::SetXfParity(BOOL bNewValue)
{
// TODO: Add your property handler here
xfParity=bNewValue;
SetModifiedFlag();
}
short CXGpscomCtrl::GetByteSize()
{
// TODO: Add your property handler here
return xByteSize;
}
void CXGpscomCtrl::SetByteSize(short nNewValue)
{
// TODO: Add your property handler here
xByteSize=nNewValue;
SetModifiedFlag();
}
short CXGpscomCtrl::GetXParity()
{
// TODO: Add your property handler here
return xParity;
}
void CXGpscomCtrl::SetXParity(short nNewValue)
{
// TODO: Add your property handler here
xParity=nNewValue;
SetModifiedFlag();
}
short CXGpscomCtrl::GetXStopBits()
{
// TODO: Add your property handler here
return (short)xStopBits;
}
void CXGpscomCtrl::SetXStopBits(short nNewValue)
{
// TODO: Add your property handler here
xStopBits=nNewValue;
SetModifiedFlag();
}
long CXGpscomCtrl::GetXBaudRate()
{
// TODO: Add your property handler here
return xBaudRate;
}
void CXGpscomCtrl::SetXBaudRate(long nNewValue)
{
// TODO: Add your property handler here
xBaudRate=nNewValue;
SetModifiedFlag();
}
BOOL CXGpscomCtrl::setsystime()
{
// TODO: Add your dispatch handler code here
PurgeComm(hCom,PURGE_TXCLEAR);//清空缓冲区
PurgeComm(hCom,PURGE_RXCLEAR);
DWORD k=18;
BOOL bo;
BYTE readbyte[18];
bo=ReadFile(hCom,readbyte,k,&k,&m_osRead);
if(bo && readbyte[0]==0x42)
{
SYSTEMTIME sysTime;
GetLocalTime(&sysTime);
sysTime.wSecond=short((readbyte[3]&0xf0) /16 *10)+short(readbyte[3]&0x0f);
sysTime.wMinute=short((readbyte[2]&0xf0) /16*10 )+short(readbyte[2]&0x0f);
sysTime.wHour=short((readbyte[1]&0xf0) /16*10 )+short(readbyte[1]&0x0f);
sysTime.wDay=short((readbyte[7]&0xf0) /16*10 )+short(readbyte[7]&0x0f);
sysTime.wMonth=short((readbyte[6]&0xf0) /16*10 )+short(readbyte[6]&0x0f);
sysTime.wYear=(short((readbyte[4]&0xf0) /16*10 )+short(readbyte[4]&0x0f))*100
+short((readbyte[5]&0xf0) /16*10 )+short(readbyte[5]&0x0f);
SetLocalTime(&sysTime);
}
return bo;
}
BOOL CXGpscomCtrl::TD2000()
{
// TODO: Add your dispatch handler code here
PurgeComm(hCom,PURGE_TXCLEAR);//清空缓冲区
PurgeComm(hCom,PURGE_RXCLEAR);
DWORD k=17;
BOOL bo;
BYTE readbyte[17];
bo=ReadFile(hCom,readbyte,k,&k,&m_osRead);
if(bo && readbyte[0]==0x42)
{
SYSTEMTIME sysTime;
GetLocalTime(&sysTime);
sysTime.wSecond=short((readbyte[3]&0xf0) /16 *10)+short(readbyte[3]&0x0f);
sysTime.wMinute=short((readbyte[2]&0xf0) /16*10 )+short(readbyte[2]&0x0f);
sysTime.wHour=short((readbyte[1]&0xf0) /16*10 )+short(readbyte[1]&0x0f);
sysTime.wDay=short((readbyte[7]&0xf0) /16*10 )+short(readbyte[7]&0x0f);
sysTime.wMonth=short((readbyte[6]&0xf0) /16*10 )+short(readbyte[6]&0x0f);
sysTime.wYear=(short((readbyte[4]&0xf0) /16*10 )+short(readbyte[4]&0x0f))*100
+short((readbyte[5]&0xf0) /16*10 )+short(readbyte[5]&0x0f);
SetLocalTime(&sysTime);
}
return bo;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -