最新进展:
CSerialPort串口类最新修正版2017-12-16
http://blog.csdn.net/itas109/article/details/78823082
这是一份优秀的类文件,好多的地方值得我们学习,具体在多线程,事件,自定义消息,类的封装方面等等。
Remon提供的串口类网址为: http://codeguru.earthweb.com/network/serialport.shtml,
其他贡献者:http://blog.csdn.net/liquanhai/article/details/6941574
代码下载:http://download.csdn.net/detail/itas109/6855323
代码托管:https://code.csdn.net/itas109/cserialport
源代码如下:
CSerialPort.h
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
#ifndef __SERIALPORT_H__
-
#define __SERIALPORT_H__
-
-
#define WM_COMM_BREAK_DETECTED WM_USER+1 // A break was detected on input.
-
#define WM_COMM_CTS_DETECTED WM_USER+2 // The CTS (clear-to-send) signal changed state.
-
#define WM_COMM_DSR_DETECTED WM_USER+3 // The DSR (data-set-ready) signal changed state.
-
#define WM_COMM_ERR_DETECTED WM_USER+4 // A line-status error occurred. Line-status errors are CE_FRAME, CE_OVERRUN, and CE_RXPARITY.
-
#define WM_COMM_RING_DETECTED WM_USER+5 // A ring indicator was detected.
-
#define WM_COMM_RLSD_DETECTED WM_USER+6 // The RLSD (receive-line-signal-detect) signal changed state.
-
#define WM_COMM_RXCHAR WM_USER+7 // A character was received and placed in the input buffer.
-
#define WM_COMM_RXFLAG_DETECTED WM_USER+8 // The event character was received and placed in the input buffer.
-
#define WM_COMM_TXEMPTY_DETECTED WM_USER+9 // The last character in the output buffer was sent.
-
-
#define MaxSerialPortNum 20 ///有效的串口总个数,不是串口的号 //add by itas109 2014-01-09
-
class CSerialPort
-
{
-
public:
-
-
-
CSerialPort();
-
virtual ~CSerialPort();
-
-
-
-
BOOL InitPort(CWnd* pPortOwner,
-
UINT portnr = 1,
-
UINT baud = 19200,
-
char parity = 'N',
-
UINT databits = 8,
-
UINT stopbits = 1,
-
DWORD dwCommEvents = EV_RXCHAR,
-
UINT writebuffersize = 1024
-
);
-
-
-
-
-
BOOL StartMonitoring();
-
BOOL RestartMonitoring();
-
BOOL StopMonitoring();
-
-
DWORD GetWriteBufferSize();
-
DWORD GetCommEvents();
-
DCB GetDCB();
-
-
COMMTIMEOUTS GetCommTimeOuts();
-
BOOL SetCommTimeOuts(COMMTIMEOUTS * lpTimeOuts);
-
-
-
void WriteToPort(char* string);
-
void WriteToPort(char* string,int n);
-
void WriteToPort(LPCTSTR string);
-
void WriteToPort(LPCTSTR string,int n);
-
void WriteToPort(BYTE* Buffer, int n);
-
void ClosePort();
-
-
BOOL RecvData(LPTSTR lpszData, const int nSize);
-
void SendData(LPCTSTR lpszData, const int nLength);
-
-
void QueryKey(HKEY hKey);
-
void Hkey2ComboBox(CComboBox& m_PortNO);
-
-
protected:
-
-
void ProcessErrorMessage(char* ErrorText);
-
static UINT CommThread(LPVOID pParam);
-
static void ReceiveChar(CSerialPort* port, COMSTAT comstat);
-
static void WriteChar(CSerialPort* port);
-
-
-
-
CWinThread* m_Thread;
-
BOOL m_bIsSuspened;
-
-
-
CRITICAL_SECTION m_csCommunicationSync;
-
BOOL m_bThreadAlive;
-
-
-
HANDLE m_hWriteEvent;
-
HANDLE m_hComm;
-
HANDLE m_hShutdownEvent;
-
-
-
-
-
-
-
-
-
-
HANDLE m_hEventArray[3];
-
-
-
OVERLAPPED m_ov;
-
COMMTIMEOUTS m_CommTimeouts;
-
DCB m_dcb;
-
-
-
CWnd* m_pOwner;
-
-
-
UINT m_nPortNr;
-
char* m_szWriteBuffer;
-
DWORD m_dwCommEvents;
-
DWORD m_nWriteBufferSize;
-
-
int m_nWriteSize;
-
};
-
-
#endif __SERIALPORT_H__
CSerialPort.cpp
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
#include "stdafx.h"
-
#include "SerialPort.h"
-
-
#include <assert.h>
-
-
int m_nComArray[20];
-
-
-
-
CSerialPort::CSerialPort()
-
{
-
m_hComm = NULL;
-
-
-
m_ov.Offset = 0;
-
m_ov.OffsetHigh = 0;
-
-
-
m_ov.hEvent = NULL;
-
m_hWriteEvent = NULL;
-
m_hShutdownEvent = NULL;
-
-
m_szWriteBuffer = NULL;
-
-
m_bThreadAlive = FALSE;
-
-
m_nWriteSize=1;
-
m_bIsSuspened = FALSE;
-
}
-
-
-
-
-
CSerialPort::~CSerialPort()
-
{
-
do
-
{
-
SetEvent(m_hShutdownEvent);
-
} while (m_bThreadAlive);
-
-
-
-
if (m_hComm != NULL)
-
{
-
CloseHandle(m_hComm);
-
m_hComm = NULL;
-
}
-
-
if(m_hShutdownEvent!=NULL)
-
CloseHandle( m_hShutdownEvent);
-
if(m_ov.hEvent!=NULL)
-
CloseHandle( m_ov.hEvent );
-
if(m_hWriteEvent!=NULL)
-
CloseHandle( m_hWriteEvent );
-
-
TRACE("Thread endedn");
-
delete [] m_szWriteBuffer;
-
}
-
-
-
-
-
-
BOOL CSerialPort::InitPort(CWnd* pPortOwner,
-
UINT portnr,
-
UINT baud,
-
char parity,
-
UINT databits,
-
UINT stopbits,
-
DWORD dwCommEvents,
-
UINT writebuffersize)
-
{
-
assert(portnr > 0 && portnr < MaxSerialPortNum+1);
-
assert(pPortOwner != NULL);
-
-
-
-
if (m_bThreadAlive)
-
{
-
do
-
{
-
SetEvent(m_hShutdownEvent);
-
} while (m_bThreadAlive);
-
TRACE("Thread endedn");
-
}
-
-
-
if (m_ov.hEvent != NULL)
-
ResetEvent(m_ov.hEvent);
-
else
-
m_ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
-
-
if (m_hWriteEvent != NULL)
-
ResetEvent(m_hWriteEvent);
-
else
-
m_hWriteEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
-
-
if (m_hShutdownEvent != NULL)
-
ResetEvent(m_hShutdownEvent);
-
else
-
m_hShutdownEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
-
-
-
-
m_hEventArray[0] = m_hShutdownEvent;
-
m_hEventArray[1] = m_ov.hEvent;
-
m_hEventArray[2] = m_hWriteEvent;
-
-
-
-
InitializeCriticalSection(&m_csCommunicationSync);
-
-
-
m_pOwner = pPortOwner;
-
-
if (m_szWriteBuffer != NULL)
-
delete [] m_szWriteBuffer;
-
m_szWriteBuffer = new char[writebuffersize];
-
-
m_nPortNr = portnr;
-
-
m_nWriteBufferSize = writebuffersize;
-
m_dwCommEvents = dwCommEvents;
-
-
BOOL bResult = FALSE;
-
char *szPort = new char[50];
-
char *szBaud = new char[50];
-
-
-
-
-
-
-
-
-
-
-
EnterCriticalSection(&m_csCommunicationSync);
-
-
-
-
if (m_hComm != NULL)
-
{
-
CloseHandle(m_hComm);
-
m_hComm = NULL;
-
}
-
-
-
-
sprintf(szPort, ".COM%d", portnr);
-
sprintf(szBaud, "baud=%d parity=%c data=%d stop=%d", baud, parity, databits, stopbits);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
m_hComm = CreateFile(szPort,
-
GENERIC_READ | GENERIC_WRITE,
-
0,
-
NULL,
-
OPEN_EXISTING,
-
FILE_FLAG_OVERLAPPED,
-
0);
-
-
-
if (m_hComm == INVALID_HANDLE_VALUE)
-
{
-
-
delete [] szPort;
-
delete [] szBaud;
-
-
return FALSE;
-
}
-
-
-
-
m_CommTimeouts.ReadIntervalTimeout = 1000;
-
m_CommTimeouts.ReadTotalTimeoutMultiplier = 1000;
-
m_CommTimeouts.ReadTotalTimeoutConstant = 1000;
-
m_CommTimeouts.WriteTotalTimeoutMultiplier = 1000;
-
m_CommTimeouts.WriteTotalTimeoutConstant = 1000;
-
-
-
-
-
if (SetCommTimeouts(m_hComm, &m_CommTimeouts))
-
{
-
-
-
-
-
-
-
-
-
-
-
-
if (SetCommMask(m_hComm, dwCommEvents))
-
{
-
if (GetCommState(m_hComm, &m_dcb))
-
{
-
m_dcb.EvtChar = 'q';
-
m_dcb.fRtsControl = RTS_CONTROL_ENABLE;
-
if (BuildCommDCB(szBaud, &m_dcb))
-
{
-
if (SetCommState(m_hComm, &m_dcb))
-
;
-
else
-
ProcessErrorMessage("SetCommState()");
-
}
-
else
-
ProcessErrorMessage("BuildCommDCB()");
-
}
-
else
-
ProcessErrorMessage("GetCommState()");
-
}
-
else
-
ProcessErrorMessage("SetCommMask()");
-
}
-
else
-
ProcessErrorMessage("SetCommTimeouts()");
-
-
delete [] szPort;
-
delete [] szBaud;
-
-
-
-
PurgeComm(m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
-
-
-
-
LeaveCriticalSection(&m_csCommunicationSync);
-
-
TRACE("Initialisation for communicationport %d completed.nUse Startmonitor to communicate.n", portnr);
-
-
return TRUE;
-
}
-
-
-
-
-
-
-
-
UINT CSerialPort::CommThread(LPVOID pParam)
-
{
-
-
-
-
CSerialPort *port = (CSerialPort*)pParam;
-
-
-
-
-
port->m_bThreadAlive = TRUE;
-
-
-
DWORD BytesTransfered = 0;
-
DWORD Event = 0;
-
DWORD CommEvent = 0;
-
DWORD dwError = 0;
-
COMSTAT comstat;
-
BOOL bResult = TRUE;
-
-
-
-
if (port->m_hComm)
-
PurgeComm(port->m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
-
-
-
-
for (;;)
-
{
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
bResult = WaitCommEvent(port->m_hComm, &Event, &port->m_ov);
-
-
if (!bResult)
-
{
-
-
-
-
switch (dwError = GetLastError())
-
{
-
case ERROR_IO_PENDING:
-
{
-
-
-
-
break;
-
}
-
case 87:
-
{
-
-
-
-
break;
-
}
-
default:
-
{
-
-
-
port->ProcessErrorMessage("WaitCommEvent()");
-
break;
-
}
-
}
-
}
-
else
-
{
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
bResult = ClearCommError(port->m_hComm, &dwError, &comstat);
-
-
if (comstat.cbInQue == 0)
-
continue;
-
}
-
-
-
-
-
-
Event = WaitForMultipleObjects(3,
-
port->m_hEventArray,
-
FALSE,
-
INFINITE);
-
-
switch (Event)
-
{
-
case 0:
-
{
-
-
-
-
CloseHandle(port->m_hComm);
-
port->m_hComm=NULL;
-
port->m_bThreadAlive = FALSE;
-
-
-
AfxEndThread(100);
-
-
break;
-
}
-
case 1:
-
{
-
memset(&comstat, 0, sizeof(COMSTAT));
-
GetCommMask(port->m_hComm, &CommEvent);
-
if (CommEvent & EV_RXCHAR)
-
-
ReceiveChar(port, comstat);
-
if (CommEvent & EV_CTS)
-
::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_CTS_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);
-
if (CommEvent & EV_BREAK)
-
::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_BREAK_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);
-
if (CommEvent & EV_ERR)
-
::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_ERR_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);
-
if (CommEvent & EV_RING)
-
::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_RING_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);
-
if (CommEvent & EV_RXFLAG)
-
::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_RXFLAG_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);
-
-
break;
-
}
-
case 2:
-
{
-
-
WriteChar(port);
-
break;
-
}
-
default:
-
{
-
AfxMessageBox("接收有问题!");
-
break;
-
}
-
-
}
-
-
}
-
-
return 0;
-
}
-
-
-
-
-
-
BOOL CSerialPort::StartMonitoring()
-
{
-
if (!(m_Thread = AfxBeginThread(CommThread, this)))
-
return FALSE;
-
TRACE("Thread startedn");
-
m_bIsSuspened = false;
-
return TRUE;
-
}
-
-
-
-
-
-
BOOL CSerialPort::RestartMonitoring()
-
{
-
TRACE("Thread resumedn");
-
m_bIsSuspened = false;
-
m_Thread->ResumeThread();
-
return TRUE;
-
}
-
-
-
-
-
-
-
BOOL CSerialPort::StopMonitoring()
-
{
-
TRACE("Thread suspendedn");
-
m_bIsSuspened = true;
-
m_Thread->SuspendThread();
-
return TRUE;
-
}
-
-
-
-
-
-
-
void CSerialPort::ProcessErrorMessage(char* ErrorText)
-
{
-
char *Temp = new char[200];
-
-
LPVOID lpMsgBuf;
-
-
FormatMessage(
-
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
-
NULL,
-
GetLastError(),
-
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
-
(LPTSTR) &lpMsgBuf,
-
0,
-
NULL
-
);
-
-
sprintf(Temp, "WARNING: %s Failed with the following error: n%snPort: %dn", (char*)ErrorText, lpMsgBuf, m_nPortNr);
-
MessageBox(NULL, Temp, "Application Error", MB_ICONSTOP);
-
-
LocalFree(lpMsgBuf);
-
delete [] Temp;
-
return;
-
}
-
-
-
-
-
-
void CSerialPort::WriteChar(CSerialPort* port)
-
{
-
BOOL bWrite = TRUE;
-
BOOL bResult = TRUE;
-
-
DWORD BytesSent = 0;
-
-
ResetEvent(port->m_hWriteEvent);
-
-
-
EnterCriticalSection(&port->m_csCommunicationSync);
-
-
if (bWrite)
-
{
-
-
port->m_ov.Offset = 0;
-
port->m_ov.OffsetHigh = 0;
-
-
-
PurgeComm(port->m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
-
-
-
bResult = WriteFile(port->m_hComm,
-
port->m_szWriteBuffer,
-
-
port->m_nWriteSize,
-
&BytesSent,
-
&port->m_ov);
-
-
-
if (!bResult)
-
{
-
DWORD dwError = GetLastError();
-
switch (dwError)
-
{
-
case ERROR_IO_PENDING:
-
{
-
-
BytesSent = 0;
-
bWrite = FALSE;
-
break;
-
}
-
default:
-
{
-
-
port->ProcessErrorMessage("WriteFile()");
-
break;
-
}
-
}
-
}
-
else
-
{
-
LeaveCriticalSection(&port->m_csCommunicationSync);
-
}
-
}
-
-
if (!bWrite)
-
{
-
bWrite = TRUE;
-
-
bResult = GetOverlappedResult(port->m_hComm,
-
&port->m_ov,
-
&BytesSent,
-
TRUE);
-
-
LeaveCriticalSection(&port->m_csCommunicationSync);
-
-
-
-
{
-
-
}
-
}
-
-
-
if (BytesSent != port->m_nWriteSize)
-
{
-
TRACE("WARNING: WriteFile() error.. Bytes Sent: %d; Message Length: %dn", BytesSent, strlen((char*)port->m_szWriteBuffer));
-
}
-
-
-
}
-
-
-
-
-
-
void CSerialPort::ReceiveChar(CSerialPort* port, COMSTAT comstat)
-
{
-
BOOL bRead = TRUE;
-
BOOL bResult = TRUE;
-
DWORD dwError = 0;
-
DWORD BytesRead = 0;
-
unsigned char RXBuff;
-
-
for (;;)
-
{
-
-
if(WaitForSingleObject(port->m_hShutdownEvent,0) == WAIT_OBJECT_0)
-
return;
-
-
-
-
-
-
EnterCriticalSection(&port->m_csCommunicationSync);
-
-
-
-
-
-
bResult = ClearCommError(port->m_hComm, &dwError, &comstat);
-
-
LeaveCriticalSection(&port->m_csCommunicationSync);
-
-
-
-
-
-
-
-
-
-
-
-
-
if (comstat.cbInQue == 0)
-
{
-
-
break;
-
}
-
-
EnterCriticalSection(&port->m_csCommunicationSync);
-
-
if (bRead)
-
{
-
-
bResult = ReadFile(port->m_hComm,
-
&RXBuff,
-
1,
-
&BytesRead,
-
&port->m_ov);
-
-
-
if (!bResult)
-
{
-
switch (dwError = GetLastError())
-
{
-
case ERROR_IO_PENDING:
-
{
-
-
-
-
bRead = FALSE;
-
break;
-
}
-
default:
-
{
-
-
port->ProcessErrorMessage("ReadFile()");
-
break;
-
-
}
-
}
-
}
-
else
-
{
-
-
bRead = TRUE;
-
}
-
}
-
-
-
if (!bRead)
-
{
-
bRead = TRUE;
-
bResult = GetOverlappedResult(port->m_hComm,
-
&port->m_ov,
-
&BytesRead,
-
TRUE);
-
-
-
if (!bResult)
-
{
-
port->ProcessErrorMessage("GetOverlappedResults() in ReadFile()");
-
}
-
}
-
-
LeaveCriticalSection(&port->m_csCommunicationSync);
-
-
-
::SendMessage((port->m_pOwner)->m_hWnd, WM_COMM_RXCHAR, (WPARAM) RXBuff, (LPARAM) port->m_nPortNr);
-
}
-
-
}
-
-
-
-
-
void CSerialPort::WriteToPort(char* string)
-
{
-
assert(m_hComm != 0);
-
-
memset(m_szWriteBuffer, 0, sizeof(m_szWriteBuffer));
-
strcpy(m_szWriteBuffer, string);
-
m_nWriteSize=strlen(string);
-
-
-
SetEvent(m_hWriteEvent);
-
}
-
-
-
void CSerialPort::WriteToPort(char* string,int n)
-
{
-
assert(m_hComm != 0);
-
-
memset(m_szWriteBuffer, 0, sizeof(m_szWriteBuffer));
-
-
-
memcpy(m_szWriteBuffer, string, n);
-
m_nWriteSize=n;
-
-
-
SetEvent(m_hWriteEvent);
-
}
-
-
void CSerialPort::WriteToPort(LPCTSTR string,int n)
-
{
-
assert(m_hComm != 0);
-
-
memset(m_szWriteBuffer, 0, sizeof(m_szWriteBuffer));
-
memcpy(m_szWriteBuffer, string, n);
-
m_nWriteSize = n;
-
-
-
SetEvent(m_hWriteEvent);
-
}
-
-
void CSerialPort::WriteToPort(LPCTSTR string)
-
{
-
assert(m_hComm != 0);
-
-
memset(m_szWriteBuffer, 0, sizeof(m_szWriteBuffer));
-
strcpy(m_szWriteBuffer, string);
-
m_nWriteSize=strlen(string);
-
-
-
SetEvent(m_hWriteEvent);
-
}
-
-
void CSerialPort::WriteToPort(BYTE* Buffer, int n)
-
{
-
assert(m_hComm != 0);
-
memset(m_szWriteBuffer, 0, sizeof(m_szWriteBuffer));
-
int i;
-
for(i=0; i<n; i++)
-
{
-
m_szWriteBuffer[i] = Buffer[i];
-
}
-
m_nWriteSize=n;
-
-
-
SetEvent(m_hWriteEvent);
-
}
-
-
-
-
-
DCB CSerialPort::GetDCB()
-
{
-
return m_dcb;
-
}
-
-
-
-
-
DWORD CSerialPort::GetCommEvents()
-
{
-
return m_dwCommEvents;
-
}
-
-
-
-
-
DWORD CSerialPort::GetWriteBufferSize()
-
{
-
return m_nWriteBufferSize;
-
}
-
-
-
void CSerialPort::ClosePort()
-
{
-
if(m_bIsSuspened)
-
{
-
RestartMonitoring();
-
}
-
if (m_bThreadAlive)
-
{
-
MSG message;
-
while (m_bThreadAlive)
-
{
-
-
if(::PeekMessage(&message, m_pOwner->m_hWnd, 0, 0, PM_REMOVE))
-
{
-
::TranslateMessage(&message);
-
::DispatchMessage(&message);
-
}
-
SetEvent(m_hShutdownEvent);
-
}
-
TRACE("Thread endedn");
-
-
}
-
if(m_szWriteBuffer != NULL)
-
{
-
delete [] m_szWriteBuffer;
-
m_szWriteBuffer = NULL;
-
}
-
-
if(m_hComm)
-
{
-
CloseHandle(m_hComm);
-
m_hComm = NULL;
-
}
-
-
-
if(m_hShutdownEvent!=NULL)
-
ResetEvent(m_hShutdownEvent);
-
if(m_ov.hEvent!=NULL)
-
ResetEvent(m_ov.hEvent);
-
if(m_hWriteEvent!=NULL)
-
ResetEvent(m_hWriteEvent);
-
}
-
-
void CSerialPort::SendData(LPCTSTR lpszData, const int nLength)
-
{
-
assert(m_hComm != 0);
-
memset(m_szWriteBuffer, 0, nLength);
-
strcpy(m_szWriteBuffer, lpszData);
-
m_nWriteSize=nLength;
-
-
SetEvent(m_hWriteEvent);
-
}
-
-
BOOL CSerialPort::RecvData(LPTSTR lpszData, const int nSize)
-
{
-
-
-
-
assert(m_hComm!=0);
-
memset(lpszData,0,nSize);
-
DWORD mylen = 0;
-
DWORD mylen2 = 0;
-
while (mylen < nSize) {
-
if(!ReadFile(m_hComm,lpszData,nSize,&mylen2,NULL))
-
return FALSE;
-
mylen += mylen2;
-
}
-
-
return TRUE;
-
}
-
-
COMMTIMEOUTS CSerialPort:: GetCommTimeOuts()
-
{
-
return m_CommTimeouts;
-
}
-
-
BOOL CSerialPort::SetCommTimeOuts(COMMTIMEOUTS *lpTimeOuts)
-
{
-
SetCommTimeouts(m_hComm, lpTimeOuts);
-
return true;
-
}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
void CSerialPort::QueryKey(HKEY hKey)
-
{
-
#define MAX_KEY_LENGTH 255
-
#define MAX_VALUE_NAME 16383
-
-
-
TCHAR achClass[MAX_PATH] = TEXT("");
-
DWORD cchClassName = MAX_PATH;
-
DWORD cSubKeys=0;
-
DWORD cbMaxSubKey;
-
DWORD cchMaxClass;
-
DWORD cValues;
-
DWORD cchMaxValue;
-
DWORD cbMaxValueData;
-
DWORD cbSecurityDescriptor;
-
FILETIME ftLastWriteTime;
-
-
DWORD i, retCode;
-
-
TCHAR achValue[MAX_VALUE_NAME];
-
DWORD cchValue = MAX_VALUE_NAME;
-
-
-
retCode = RegQueryInfoKey(
-
hKey,
-
achClass,
-
&cchClassName,
-
NULL,
-
&cSubKeys,
-
&cbMaxSubKey,
-
&cchMaxClass,
-
&cValues,
-
&cchMaxValue,
-
&cbMaxValueData,
-
&cbSecurityDescriptor,
-
&ftLastWriteTime);
-
-
for (i=0;i<20;i++)
-
{
-
m_nComArray[i] = -1;
-
}
-
-
-
if (cValues > 0) {
-
for (i=0, retCode=ERROR_SUCCESS; i<cValues; i++) {
-
cchValue = MAX_VALUE_NAME; achValue[0] = '