CSerialPort串口类nlpir init初始化失败Initport总是失败,该怎么处理

CserialPort串口类
#ifndef __SERIALPORT_H__
#define __SERIALPORT_H__
WM_COMM_BREAK_DETECTED&&WM_USER+1&//
A break was detected on input.
WM_COMM_CTS_DETECTED&&WM_USER+2&//
The CTS (clear-to-send) signal changed state.
WM_COMM_DSR_DETECTED&&WM_USER+3&//
The DSR (data-set-ready) signal changed state.
WM_COMM_ERR_DETECTED&&WM_USER+4&//
A line-status error occurred. Line-status errors are CE_FRAME,
CE_OVERRUN, and CE_RXPARITY.
WM_COMM_RING_DETECTED&&WM_USER+5&//
A ring indicator was detected.
WM_COMM_RLSD_DETECTED&&WM_USER+6&//
The RLSD (receive-line-signal-detect) signal changed state.
WM_COMM_RXCHAR&&&&WM_USER+7&//
A character was received and placed in the input buffer.
WM_COMM_RXFLAG_DETECTED&&WM_USER+8&//
The event character was received and placed in the input
WM_COMM_TXEMPTY_DETECTED&WM_USER+9&//
The last character in the output buffer was
class CSerialPort
{&&&&&&&&&&&&&&
&// contruction and destruction
&CSerialPort();
&virtual&&~CSerialPort();
initialisation&&&&&&&&&&&
&BOOL&&InitPort(CWnd*
pPortOwner, UINT portnr = 1, UINT baud = 19200,
parity = 'N', UINT databits = 8, UINT stopsbits = 1,
dwCommEvents = EV_RXCHAR | EV_CTS, UINT nBufferSize = 512,
ReadIntervalTimeout = 1000,
ReadTotalTimeoutMultiplier = 1000,
ReadTotalTimeoutConstant = 1000,
WriteTotalTimeoutMultiplier = 1000,
WriteTotalTimeoutConstant = 1000);
&// start/stop comm watching
&BOOL&&StartMonitoring();
&BOOL&&RestartMonitoring();
&BOOL&&StopMonitoring();
&DWORD&&GetWriteBufferSize();
&DWORD&&GetCommEvents();
&DCB&&&GetDCB();
&void&&WriteToPort(char*
&void&&&&&&&
WriteToPort(char* string,int n); // add by mrlong
&void&&WriteToPort(LPCTSTR
string);&&&&
// add by mrlong
&void&&&&&&&
WriteToPort(BYTE* Buffer, int n);// add by mrlong
&void&&&&&&&
ClosePort();&&&&&&&&&&&&&&&&&&&&
// add by mrlong &
&void SendData(LPCTSTR lpszData, const int
nLength);&& //串口发送函数 by mrlong
&BOOL RecvData(LPTSTR lpszData, const int
nSize);&&&&&
//串口接收函数 by mrlong
protected:
&// protected memberfunctions
&void&&ProcessErrorMessage(char*
ErrorText);
&static UINT&CommThread(LPVOID
void&ReceiveChar(CSerialPort* port, COMSTAT
void&WriteChar(CSerialPort* port);
&// thread
&CWinThread*&&&m_T
&// synchronisation objects
&CRITICAL_SECTION&m_csCommunicationS
&BOOL&&&&m_bThreadA
&// handles
&HANDLE&&&&m_hShutdownE&
//stop发生的事件
&HANDLE&&&&m_hC&&&&&&&&&&
&HANDLE&&&&m_hWriteE&&&&
&// Event array.
&// One element is used for each event. There are
two event handles for each port.
&// A Write event and a receive character event
which is located in the overlapped structure (m_ov.hEvent).
&// There is a general shutdown when the port is
&HANDLE&&&&m_hEventArray[3];
&// structures
&OVERLAPPED&&&m_
&COMMTIMEOUTS&&m_CommT
&DCB&&&&&m_
&// owner window
&CWnd*&&&&m_pO
&UINT&&&&m_nPortNr;&&&&&&&
&char*&&&&m_szWriteB
&DWORD&&&&m_dwCommE
&DWORD&&&&m_nWriteBufferS
&int&&&&&&&&&&&&&&&&
m_nWriteS //add by mrlong
#endif __SERIALPORT_H__
&---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include "stdafx.h"
#include "SerialPort.h"
#include &assert.h&
// Constructor
CSerialPort::CSerialPort()
&m_hComm = NULL;
&// initialize overlapped structure members to
&m_ov.Offset = 0;
&m_ov.OffsetHigh = 0;
&// create events
&m_ov.hEvent = NULL;
&m_hWriteEvent = NULL;
&m_hShutdownEvent = NULL;
&m_szWriteBuffer = NULL;
&m_bThreadAlive = FALSE;
&m_nWriteSize = 1;
// Delete dynamic memory
CSerialPort::~CSerialPort()
&&SetEvent(m_hShutdownEvent);
&} while (m_bThreadAlive);
&if (m_hComm != NULL)
&&CloseHandle(m_hComm);
&&m_hComm = NULL;
&// Close Handles&
&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 ended\n");
&delete [] m_szWriteB
// Initialize the port. This can be port 1 to 4.
//& n=none
//& e=even
//& m=mark
//& s=space
//& 5,6,7,8
//& 1,1.5,2
BOOL CSerialPort::InitPort(CWnd* pPortOwner,&//
the owner (CWnd) of the port (receives message)
portnr,&&// portnumber
baud,&&&//
parity,&&// parity
databits,&&// databits
stopbits,&&// stopbits
DWORD dwCommEvents,&// EV_RXCHAR, EV_CTS etc
UINT& writebuffersize,// size to the
writebuffer
ReadIntervalTimeout,
ReadTotalTimeoutMultiplier,
ReadTotalTimeoutConstant,
WriteTotalTimeoutMultiplier,
DWORD&& WriteTotalTimeoutConstant
&assert(portnr & 0
&& portnr &
&assert(pPortOwner != NULL);
&// if the thread is alive: Kill
&if (m_bThreadAlive)
&&&SetEvent(m_hShutdownEvent);
&&} while (m_bThreadAlive);
&&TRACE("Thread ended\n");
&// create events
&if (m_ov.hEvent != NULL)
&&ResetEvent(m_ov.hEvent);
&&m_ov.hEvent = CreateEvent(NULL,
TRUE, FALSE, NULL);
&if (m_hWriteEvent != NULL)
&&ResetEvent(m_hWriteEvent);
&&m_hWriteEvent =
CreateEvent(NULL, TRUE, FALSE, NULL);
&if (m_hShutdownEvent != NULL)
&&ResetEvent(m_hShutdownEvent);
&&m_hShutdownEvent =
CreateEvent(NULL, TRUE, FALSE, NULL);
&// initialize the event objects
&m_hEventArray[0] =
m_hShutdownE&// highest priority
&m_hEventArray[1] = m_ov.hE
&m_hEventArray[2] = m_hWriteE
&// initialize critical section
&InitializeCriticalSection(&m_csCommunicationSync);
&// set buffersize for writing and save the
&m_pOwner = pPortO
&if (m_szWriteBuffer != NULL)
&&delete []
m_szWriteB
&m_szWriteBuffer = new char[writebuffersize];
&m_nPortNr =
&m_nWriteBufferSize =
&m_dwCommEvents = dwCommE
&BOOL bResult = FALSE;
&char *szPort = new char[50];
&char *szBaud = new char[50];
&// now it critical!
&EnterCriticalSection(&m_csCommunicationSync);
&// if the port is already opened: close
&if (m_hComm != NULL)
&&CloseHandle(m_hComm);
&&m_hComm = NULL;
&// prepare port strings
&sprintf(szPort, "COM%d", portnr);
&// stop is index 0 = 1 1=1.5 2=2
&switch(stopbits)
&&&mystop =
ONESTOPBIT;
&&&mystop =
ONE5STOPBITS;
&&&mystop =
TWOSTOPBITS;
&myparity = 0;
&switch(parity)
&&case 'N':
&&&myparity =
&&case 'E':
&&&myparity =
&&case 'O':
&&&myparity =
&&case 'M':
&&&myparity =
&&case 'S':
&&&myparity =
&sprintf(szBaud, "baud=%d parity=%c data=%d
stop=%d", baud, parity, databits, mystop);
&// get a handle to the port
&m_hComm =
CreateFile(szPort,&&&&&&//
communication port string (COMX)
GENERIC_READ | GENERIC_WRITE,&// read/write
0,&&&&&&&&//
comm devices must be opened with exclusive access
NULL,&&&&&&&//
no security attributes
OPEN_EXISTING,&&&&&//
comm devices must use OPEN_EXISTING
FILE_FLAG_OVERLAPPED,&&&//
0);&&&&&&&//
template must be 0 for comm devices
&if (m_hComm == INVALID_HANDLE_VALUE)
&&// port not found
&&delete [] szP
&&delete [] szB
&&return FALSE;
&// set the timeout values
&m_CommTimeouts.ReadIntervalTimeout&&&&&&&&
= ReadIntervalTimeout * 1000;
&m_CommTimeouts.ReadTotalTimeoutMultiplier&
= ReadTotalTimeoutMultiplier * 1000;
&m_CommTimeouts.ReadTotalTimeoutConstant&&&
= ReadTotalTimeoutConstant * 1000;
&m_CommTimeouts.WriteTotalTimeoutMultiplier =
WriteTotalTimeoutMultiplier * 1000;
&m_CommTimeouts.WriteTotalTimeoutConstant&&
= WriteTotalTimeoutConstant * 1000;
&// configure
&if (SetCommTimeouts(m_hComm,
&m_CommTimeouts))
&{&&&&&&&&
&&if (SetCommMask(m_hComm,
dwCommEvents))
(GetCommState(m_hComm, &m_dcb))
&&&&m_dcb.EvtChar
&&&&m_dcb.fRtsControl
= RTS_CONTROL_ENABLE;&&// set RTS
&&&&m_dcb.BaudRate
=& // add by mrlong
&&&&m_dcb.Parity&&
&&&&m_dcb.ByteSize
&&&&m_dcb.StopBits
(BuildCommDCB(szBaud, &m_dcb))
(SetCommState(m_hComm, &m_dcb))
// normal operation... continue
&&&&&&ProcessErrorMessage("SetCommState()");
&&&&//else
&&&&//&ProcessErrorMessage("BuildCommDCB()");
&&&&ProcessErrorMessage("GetCommState()");
&&&ProcessErrorMessage("SetCommMask()");
&&ProcessErrorMessage("SetCommTimeouts()");
&delete [] szP
&delete [] szB
&// flush the port
&PurgeComm(m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR
| PURGE_RXABORT | PURGE_TXABORT);
&// release critical section
&LeaveCriticalSection(&m_csCommunicationSync);
&TRACE("Initialisation for communicationport %d
completed.\nUse Startmonitor to communicate.\n", portnr);
&return TRUE;
//& The CommThread Function.
UINT CSerialPort::CommThread(LPVOID pParam)
&// Cast the void pointer passed to the thread
&// a pointer of CSerialPort class
&CSerialPort *port = (CSerialPort*)pP
&// Set the status variable in the dialog class
&// TRUE to indicate the thread is running.
&port-&m_bThreadAlive =
&// Misc. variables
&DWORD BytesTransfered = 0;
&DWORD Event = 0;
&DWORD CommEvent = 0;
&DWORD dwError = 0;
&BOOL& bResult = TRUE;
&// Clear comm buffers at startup
(port-&m_hComm)&&//
check if the port is opened
&&PurgeComm(port-&m_hComm,
PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
&// begin forever loop.& This
loop will run as long as the thread is alive.
&&// Make a call to
WaitCommEvent().& This call will return
immediatly
&&// because our port was created
as an async port (FILE_FLAG_OVERLAPPED
&&// and an
m_OverlappedStructerlapped structure specified).&
This call will cause the
&&// m_OverlappedStructerlapped
element m_OverlappedStruct.hEvent, which is part of the
m_hEventArray to
&&// be placed in a non-signeled
state if there are no bytes available to be read,
&&// or to a signeled state if
there are bytes available.& If this event
&&// is set to the non-signeled
state, it will be set to signeled when a
&&// character arrives at the
&&// we do this for each
&&bResult =
WaitCommEvent(port-&m_hComm, &Event,
&port-&m_ov);
(!bResult)&
WaitCommEvent() returns FALSE, process the last error to
(dwError = GetLastError())
ERROR_IO_PENDING: &
This is a normal return value if there are no bytes
to read at the port.
Do nothing and continue
Under Windows NT, this value is returned for some reason.
I have not investigated why, but it is also a valid reply
Also do nothing and continue.
&&&default:
All other error codes indicate a serious error has
occured.& Process this error.
&&&&&port-&ProcessErrorMessage("WaitCommEvent()");
WaitCommEvent() returns TRUE, check to be sure there are
&&&// actually
bytes in the buffer to read.&
&&&// If you are
reading more than one byte at a time from the buffer
&&&// (which
this program does not do) you will have the situation occur
&&&// where the
first byte to arrive will cause the WaitForMultipleObjects()
&&&// function
to stop waiting.& The WaitForMultipleObjects()
&&&// resets the
event handle in m_OverlappedStruct.hEvent to the non-signelead
&&&// as it
&&&// If in the
time between the reset of this event and the call to
&&&// ReadFile()
more bytes arrive, the m_OverlappedStruct.hEvent handle will be set
&&&// to the
signeled state. When the call to ReadFile() occurs, it will
&&&// read all
of the bytes from the buffer, and the program will
&&&// loop back
around to WaitCommEvent().
&&&// At this
point you will be in the situation where m_OverlappedStruct.hEvent
&&&// but there
are no bytes available to read.& If you proceed
ReadFile(), it will return immediatly due to the async port setup,
GetOverlappedResults() will not return until the next character
&&&// It is not
desirable for the GetOverlappedResults() function to be in
&&&// this
state.& The thread shutdown event (event 0) and
the WriteFile()
&&&// event
(Event2) will not work if the thread is blocked by
GetOverlappedResults().
solution to this is to check the buffer with a call to
ClearCommError().
&&&// This call
will reset the event handle, and if there are no bytes to
&&&// we can
loop back through WaitCommEvent() again, then proceed.
&&&// If there
are really bytes to read, do nothing and proceed.
&&&bResult =
ClearCommError(port-&m_hComm,
&dwError, &comstat);
(comstat.cbInQue == 0)
&&}&// end if
&&// Main wait
function.& This function will normally block the
&&// until one of nine events
occur that require action.
WaitForMultipleObjects(3, port-&m_hEventArray,
FALSE, INFINITE);
&&switch (Event)
Shutdown event.& This is event zero so it will
the higest priority and be serviced first.
&port-&m_bThreadAlive =
Kill this thread.& break is not needed, but makes
me feel better.
&&&&AfxEndThread(100);
&&case 1:&//
read event
&&&&GetCommMask(port-&m_hComm,
&CommEvent);
(CommEvent & EV_RXCHAR) //接收到字符,并置于输入缓冲区中
&&&&&ReceiveChar(port,
(CommEvent & EV_CTS) //CTS信号状态发生变化
&&&&&::SendMessage(port-&m_pOwner-&m_hWnd,
WM_COMM_CTS_DETECTED, (WPARAM) 0, (LPARAM)
port-&m_nPortNr);
(CommEvent & EV_RXFLAG) //接收到事件字符,并置于输入缓冲区中
&&&&&::SendMessage(port-&m_pOwner-&m_hWnd,
WM_COMM_RXFLAG_DETECTED, (WPARAM) 0, (LPARAM)
port-&m_nPortNr);
(CommEvent & EV_BREAK)&
//输入中发生中断
&&&&&::SendMessage(port-&m_pOwner-&m_hWnd,
WM_COMM_BREAK_DETECTED, (WPARAM) 0, (LPARAM)
port-&m_nPortNr);
(CommEvent & EV_ERR)
//发生线路状态错误,线路状态错误包括CE_FRAME,CE_OVERRUN和CE_RXPARITY
&&&&&::SendMessage(port-&m_pOwner-&m_hWnd,
WM_COMM_ERR_DETECTED, (WPARAM) 0, (LPARAM)
port-&m_nPortNr);
(CommEvent & EV_RING) //检测到振铃指示
&&&&&::SendMessage(port-&m_pOwner-&m_hWnd,
WM_COMM_RING_DETECTED, (WPARAM) 0, (LPARAM)
port-&m_nPortNr);
&&case 2: // write event
Write character event from port
&&&&WriteChar(port);
&&} // end switch
&} // close forever loop
&return 0;
// start comm watching
BOOL CSerialPort::StartMonitoring()
&if (!(m_Thread = AfxBeginThread(CommThread,
&&return FALSE;
&TRACE("Thread started\n");
&return TRUE;&
// Restart the comm thread
BOOL CSerialPort::RestartMonitoring()
&TRACE("Thread resumed\n");
&m_Thread-&ResumeThread();
&return TRUE;&
// Suspend the comm thread
BOOL CSerialPort::StopMonitoring()
&TRACE("Thread suspended\n");
&m_Thread-&SuspendThread();
&return TRUE;&
// If there is a error, give the right message
void CSerialPort::ProcessErrorMessage(char* ErrorText)
&char *Temp = new char[200];
&LPVOID lpMsgB
&FormatMessage(
&&FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM,
&&GetLastError(),
&&MAKELANGID(LANG_NEUTRAL,
SUBLANG_DEFAULT), // Default language
&&(LPTSTR)
&lpMsgBuf,
&sprintf(Temp, "WARNING:& %s
Failed with the following error: \n%s\nPort: %d\n",
(char*)ErrorText, lpMsgBuf, m_nPortNr);
&MessageBox(NULL, Temp, "Application Error",
MB_ICONSTOP);
&LocalFree(lpMsgBuf);
&delete[] T
// Write a character.
void CSerialPort::WriteChar(CSerialPort* port)
&BOOL bWrite = TRUE;
&BOOL bResult = TRUE;
&DWORD BytesSent = 0;
&DWORD SendLen&&
= port-&m_nWriteS
&ResetEvent(port-&m_hWriteEvent);
&// Gain ownership of the critical section
&EnterCriticalSection(&port-&m_csCommunicationSync);
&if (bWrite)
&&// Initailize variables
&&port-&m_ov.Offset
&&port-&m_ov.OffsetHigh
&&// Clear buffer
&&PurgeComm(port-&m_hComm,
PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
&&bResult =
WriteFile(port-&m_hComm,&&&&&&&//
Handle to COMM Port
&&&&&&&port-&m_szWriteBuffer,&&&&&//
Pointer to message buffer in calling finction
&&&&&&&SendLen,&&&
// add by mrlong
&&&&&&&//strlen((char*)port-&m_szWriteBuffer),&//
Length of message to send
&&&&&&&&BytesSent,&&&&&&&&//
Where to store the number of bytes sent
&&&&&&&&port-&m_ov);&&&&&&&//
Overlapped structure
&&// deal with any error
(!bResult)&
&&&DWORD dwError
= GetLastError();
ERROR_IO_PENDING:
continue to GetOverlappedResults()
&&&&&&BytesSent
&&&&&&bWrite
&&&&default:
all other error codes
&&&&&&port-&ProcessErrorMessage("WriteFile()");
&&&LeaveCriticalSection(&port-&m_csCommunicationSync);
&} // end if(bWrite)
&if (!bWrite)
&&bWrite = TRUE;
&&bResult =
GetOverlappedResult(port-&m_hComm,&//
Handle to COMM port
&&&&&&&&&&
&port-&m_ov,&&//
Overlapped structure
&&&&&&&&&&
&BytesSent,&&//
Stores number of bytes sent
&&&&&&&&&&
TRUE); &&&//
&&LeaveCriticalSection(&port-&m_csCommunicationSync);
&&// deal with the error
(!bResult)&
&&&port-&ProcessErrorMessage("GetOverlappedResults()
in WriteFile()");
&} // end if (!bWrite)
&// Verify that the data size send equals what
we tried to send
&if (BytesSent != SendLen )& //
&&TRACE("WARNING: WriteFile()
error.. Bytes Sent: %d; Message Length: %d\n", BytesSent,
strlen((char*)port-&m_szWriteBuffer));
// Character received. Inform the owner
void CSerialPort::ReceiveChar(CSerialPort* port, COMSTAT
&BOOL& bRead = TRUE;
&BOOL& bResult = TRUE;
&DWORD dwError = 0;
&DWORD BytesRead = 0;
&unsigned char RXB
&&&&&&&&&&&&
//add by liquanhai& 防止死锁
&&&&&&&&&&&&
if(WaitForSingleObject(port-&m_hShutdownEvent,0)==WAIT_OBJECT_0)
&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&
&// Gain ownership of the comm port critical
&&// This process guarantees no
other part of this program
&&// is using the port
&&EnterCriticalSection(&port-&m_csCommunicationSync);
&&// ClearCommError() will
update the COMSTAT structure and
&&// clear any other
&&bResult =
ClearCommError(port-&m_hComm,
&dwError, &comstat);
&&LeaveCriticalSection(&port-&m_csCommunicationSync);
&&// start forever
loop.& I use this type of loop because I
&&// do not know at runtime how
many loops this will have to
&&// run. My solution is to start
a forever loop and to
&&// break out of it when I have
processed all of the
available.& Be careful with this approach
&&// be sure your loop will
&&// My reasons for this are not
as clear in this sample
&&// as it is in my production
code, but I have found this
&&// solutiion to be the most
efficient way to do this.
&&if (comstat.cbInQue == 0)
&&&// break out
when all bytes have been read
&&EnterCriticalSection(&port-&m_csCommunicationSync);
&&if (bRead)
&&&bResult =
ReadFile(port-&m_hComm,&&//
Handle to COMM port
&RXBuff,&&&&//
RX Buffer Pointer
Read one byte
&BytesRead,&&&//
Stores number of bytes read
&port-&m_ov);&&//
pointer to the m_ov structure
&&&// deal with
the error code
(!bResult)&
&&&&switch
(dwError = GetLastError())
ERROR_IO_PENDING: &
asynchronous i/o is still in progress
Proceed on to GetOverlappedResults();
&&&&&&&bRead
&&&&&default:
Another error has occured.& Process this
&&&&&&&port-&ProcessErrorMessage("ReadFile()");
ReadFile() returned complete. It is not necessary to call
GetOverlappedResults()
&&}& // close if
&&if (!bRead)
&&&bRead =
&&&bResult =
GetOverlappedResult(port-&m_hComm,&//
Handle to COMM port
&&&&&&&&&&&
&port-&m_ov,&&//
Overlapped structure
&&&&&&&&&&&
&BytesRead,&&//
Stores number of bytes read
&&&&&&&&&&&
TRUE); &&&//
&&&// deal
with the error code
(!bResult)&
&&&&port-&ProcessErrorMessage("GetOverlappedResults()
in ReadFile()");
&&}& // close if
&&LeaveCriticalSection(&port-&m_csCommunicationSync);
&&// notify parent that a byte
was received
&&::SendMessage((port-&m_pOwner)-&m_hWnd,
WM_COMM_RXCHAR, (WPARAM) RXBuff, (LPARAM)
port-&m_nPortNr);
&} // end forever loop
// Write a string to the port
void CSerialPort::WriteToPort(char* string)
&assert(m_hComm != 0);
&memset(m_szWriteBuffer, 0,
sizeof(m_szWriteBuffer));
&strcpy(m_szWriteBuffer, string);
&m_nWriteSize=strlen(string); // add by
&// set event for write
&SetEvent(m_hWriteEvent);
// Return the device control block
DCB CSerialPort::GetDCB()
&return m_
// Return the communication event masks
DWORD CSerialPort::GetCommEvents()
&return m_dwCommE
// Return the output buffer size
DWORD CSerialPort::GetWriteBufferSize()
&return m_nWriteBufferS
void CSerialPort::ClosePort()
SetEvent(m_hShutdownEvent);
&&&&&&&&&&&&&
if(::PeekMessage(&message,m_pOwner-&m_hWnd,0,0,PM_REMOVE))
&&&&&&&&&&&&&&
&&&&&&&&&&&&&&
::TranslateMessage(&message);
&&&&&&&&&&&&&&&
::DispatchMessage(&message);
&&&&&&&&&&&&&
&&&&&&&&&&&&&&
&} while (m_bThreadAlive);
&// if the port is still opened: close it
&if (m_hComm != NULL)
&&CloseHandle(m_hComm);
&&m_hComm = NULL;
&// Close Handles&
&if(m_hShutdownEvent!=NULL)
&&ResetEvent(m_hShutdownEvent);
&if(m_ov.hEvent!=NULL)
&&ResetEvent(m_ov.hEvent);
&if(m_hWriteEvent!=NULL)
&&ResetEvent(m_hWriteEvent);&&&
&//delete [] m_szWriteB
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 =
&// set event for write
&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);
&// set event for write
&SetEvent(m_hWriteEvent);
void CSerialPort::WriteToPort(BYTE* Buffer, int n)
&assert(m_hComm != 0);
&memset(m_szWriteBuffer, 0,
sizeof(m_szWriteBuffer));
&for(i=0; i&n; i++)
&&m_szWriteBuffer[i] =
Buffer[i];
&m_nWriteSize=n;
&// set event for write
&SetEvent(m_hWriteEvent);
void CSerialPort::SendData(LPCTSTR lpszData, const int
&assert(m_hComm != 0);
&memset(m_szWriteBuffer, 0, nLength);
&strcpy(m_szWriteBuffer, lpszData);
&m_nWriteSize=nL
&// set event for write
&SetEvent(m_hWriteEvent);
BOOL CSerialPort::RecvData(LPTSTR lpszData, const int
&//接收数据
&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))
&&mylen += mylen2;
&return TRUE;
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。}

我要回帖

更多关于 nlpir init初始化失败 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信