123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588 |
- #include "StdAfx.h"
- #include "MySeries.h"
- CMyCESeries::CMyCESeries(UINT portNo, UINT baud, UINT parity, UINT databits, UINT stopbits)
- :m_portNo(portNo)
- ,m_baud(baud)
- ,m_parity(parity)
- ,m_databits(databits)
- ,m_stopbits(stopbits)
- ,m_hReadThread(NULL)
- ,m_hReadCloseEvent(NULL)
- {
-
-
-
-
-
-
- m_hComm = INVALID_HANDLE_VALUE;
- m_OnSeriesRead = NULL;
- m_hReadCloseEvent = NULL;
- m_bOpened = false;
- bCanComRead = true;
- m_bSyncOrAsync = true;
- memset(&m_olWrite,0,sizeof(OVERLAPPED));
- }
- CMyCESeries::~CMyCESeries()
- {
- if (m_bOpened)
- ClosePort();
- }
- DWORD CMyCESeries::ReadThreadFunc(LPVOID lparam)
- {
- CMyCESeries *ceSeries = (CMyCESeries*)lparam;
-
-
- BYTE * readBuf = NULL;
- DWORD actualReadLen=0;
- DWORD willReadLen;
-
- DWORD dwReadErrors;
- COMSTAT cmState;
-
-
- OVERLAPPED olWait;
-
- memset(&olWait,0,sizeof(OVERLAPPED));
- olWait.hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
-
-
- DWORD evtMask;
-
-
- ASSERT(ceSeries->m_hComm !=INVALID_HANDLE_VALUE);
-
-
-
- PurgeComm(ceSeries->m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR );
-
- SetCommMask (ceSeries->m_hComm, EV_RXCHAR | EV_CTS | EV_DSR );
-
-
-
-
- OVERLAPPED olRead;
- memset(&olRead,0,sizeof(OVERLAPPED));
- olRead.hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
- while(TRUE)
- {
- if(ceSeries->m_bSyncOrAsync)
- {
- DWORD dwCommStatus = 0;
- BOOL bWait1=0;
- bWait1= WaitCommEvent(ceSeries->m_hComm,&dwCommStatus,&olWait);
-
- DWORD dwByte;
- if(GetOverlappedResult(ceSeries->m_hComm,&olWait,&dwByte,TRUE) == FALSE)
-
- {
- if(GetLastError() != ERROR_IO_PENDING)
- {
- return 0x30;
- }
-
- DWORD dwErrors;
- COMSTAT comStat;
- memset(&comStat,0,sizeof(comStat));
- ClearCommError(ceSeries->m_hComm,&dwErrors,&comStat);
- return 0x35;
- }
-
-
- if (dwCommStatus & EV_RXCHAR)
- {
- ClearCommError(ceSeries->m_hComm,&dwReadErrors,&cmState);
- willReadLen = cmState.cbInQue ;
- if (willReadLen <= 0)
- {
- continue;
- }
-
-
-
- readBuf = new BYTE[willReadLen+1];
- ZeroMemory(readBuf,willReadLen+1);
-
-
- if(ReadFile(ceSeries->m_hComm, readBuf, willReadLen, &actualReadLen,&olRead)==FALSE)
- {
-
- delete[] readBuf;
- readBuf = NULL;
- if(GetLastError() != ERROR_IO_PENDING)
- return 0x40;
- if(GetOverlappedResult(ceSeries->m_hComm,&olRead,&actualReadLen,TRUE) == FALSE)
- return 0x45;
- if(actualReadLen == 0)
- return 0x50;
- }
- else
- {
-
- if (actualReadLen>0)
- {
-
- if (ceSeries->m_OnSeriesRead)
- {
- ceSeries->m_OnSeriesRead(ceSeries->m_pOwner,readBuf,actualReadLen);
- }
- }
-
- delete[] readBuf;
- readBuf = NULL;
- }
- }
-
- }
- else
- {
- if (WaitCommEvent(ceSeries->m_hComm,&evtMask,0))
- {
- SetCommMask (ceSeries->m_hComm, EV_RXCHAR | EV_CTS | EV_DSR );
-
- if (evtMask & EV_RXCHAR)
- {
- ClearCommError(ceSeries->m_hComm,&dwReadErrors,&cmState);
- willReadLen = cmState.cbInQue ;
- if (willReadLen <= 0)
- {
- continue;
- }
-
-
- readBuf = new BYTE[willReadLen];
- ZeroMemory(readBuf,willReadLen);
-
- ReadFile(ceSeries->m_hComm, readBuf, willReadLen, &actualReadLen,0);
-
-
- if (actualReadLen>0)
- {
-
- if (ceSeries->m_OnSeriesRead)
- {
- ceSeries->m_OnSeriesRead(ceSeries->m_pOwner,readBuf,actualReadLen);
- }
- }
-
- delete[] readBuf;
- readBuf = NULL;
- }
- }
- }
-
-
-
-
-
-
- if (WaitForSingleObject(ceSeries->m_hReadCloseEvent, 10) == WAIT_OBJECT_0)
- {
- TRACE("线程ReadThreadFunc退出\n");
- break;
- }
- }
-
- CloseHandle( olRead.hEvent );
- CloseHandle( olWait.hEvent );
-
- if(readBuf)
- {
- delete[] readBuf;
- readBuf = NULL;
- }
- ExitThread(0);
- return 0;
- }
- void CMyCESeries::CloseReadThread()
- {
- if( m_hComm != INVALID_HANDLE_VALUE )
- {
-
-
-
-
-
-
- {
- TRACE("强行关闭ReadThreadFunc线程\n");
- TerminateThread(m_hReadThread,0);
- }
-
- m_hReadThread = NULL;
- }
- }
- BOOL CMyCESeries::OpenPort(void * pOwner,
- UINT portNo ,
- UINT baud ,
- UINT parity ,
- UINT databits ,
- UINT stopbits ,
- bool isASync
- )
- {
- m_portNo = portNo;
- m_baud = baud;
- m_parity = parity;
- m_databits = databits;
- m_stopbits = stopbits;
- m_bSyncOrAsync = isASync;
- DCB commParam;
- TCHAR szPort[15];
- ASSERT(pOwner!=NULL);
- m_pOwner = pOwner;
-
-
- if (m_hComm != INVALID_HANDLE_VALUE)
- {
- return TRUE;
- }
-
-
-
- wsprintf(szPort, _T("\\\\.\\COM%d"), portNo);
-
-
-
- if(m_bSyncOrAsync)
- {
- m_hComm = CreateFile(
- szPort,
- GENERIC_READ | GENERIC_WRITE,
- 0,
- NULL,
- OPEN_EXISTING,
- FILE_FLAG_OVERLAPPED,
- 0
- );
- }
- else
- {
- m_hComm = CreateFile(
- szPort,
- GENERIC_READ | GENERIC_WRITE,
- 0,
- NULL,
- OPEN_EXISTING,
- 0,
- NULL
- );
- }
- if (m_hComm == INVALID_HANDLE_VALUE)
- {
-
- TRACE(_T("CreateFile 返回无效句柄\n"));
- DWORD ErrNo = GetLastError();
- return FALSE;
-
- }
-
-
- if (!GetCommState(m_hComm,&commParam))
- {
-
- CloseHandle (m_hComm);
- m_hComm = INVALID_HANDLE_VALUE;
- return FALSE;
- }
- if(m_bSyncOrAsync)
- {
-
- m_olWrite.hEvent = CreateEvent(NULL,TRUE,FALSE,_T("WriteData"));
- }
-
-
- commParam.BaudRate = baud;
- commParam.fBinary = TRUE;
- commParam.fParity = TRUE;
- commParam.ByteSize = databits;
- commParam.Parity = parity;
- commParam.StopBits = stopbits;
-
- commParam.fOutxCtsFlow = FALSE;
- commParam.fOutxDsrFlow = FALSE;
- commParam.fDtrControl = DTR_CONTROL_ENABLE;
-
- commParam.fDsrSensitivity = FALSE;
- commParam.fTXContinueOnXoff = TRUE;
- commParam.fOutX = FALSE;
- commParam.fInX = FALSE;
- commParam.fErrorChar = FALSE;
- commParam.fNull = FALSE;
- commParam.fRtsControl = RTS_CONTROL_ENABLE;
-
- commParam.fAbortOnError = FALSE;
-
-
- if (!SetCommState(m_hComm, &commParam))
- {
- TRACE(_T("SetCommState error\n"));
- DWORD dwError = GetLastError();
-
- CloseHandle (m_hComm);
- m_hComm = INVALID_HANDLE_VALUE;
- return FALSE;
- }
-
-
- COMMTIMEOUTS CommTimeOuts;
- GetCommTimeouts (m_hComm, &CommTimeOuts);
- CommTimeOuts.ReadIntervalTimeout = MAXDWORD;
- CommTimeOuts.ReadTotalTimeoutMultiplier = 0;
- CommTimeOuts.ReadTotalTimeoutConstant = 0;
- CommTimeOuts.WriteTotalTimeoutMultiplier = 10;
- CommTimeOuts.WriteTotalTimeoutConstant = 1000;
- if(!SetCommTimeouts( m_hComm, &CommTimeOuts ))
- {
- TRACE( _T("SetCommTimeouts 返回错误\n") );
-
- CloseHandle (m_hComm);
- m_hComm = INVALID_HANDLE_VALUE;
- return FALSE;
- }
-
-
- SetCommMask (m_hComm, EV_RXCHAR);
-
- SetupComm(m_hComm,1024,1024);
-
-
- PurgeComm(m_hComm,PURGE_TXCLEAR|PURGE_RXCLEAR);
-
- CString strEvent;
- strEvent.Format(_T("Com_ReadCloseEvent%d"),portNo);
- m_hReadCloseEvent = CreateEvent(NULL,TRUE,FALSE,strEvent);
-
- if( bCanComRead == true )
- {
- m_hReadThread = CreateThread(NULL,0,ReadThreadFunc,this,0,&m_dwReadThreadID);
- }
-
-
- TRACE(_T("串口%d打开成功\n"), portNo);
- m_bOpened = true;
- return TRUE;
- }
- void CMyCESeries::ClosePort()
- {
-
- if (m_hComm == INVALID_HANDLE_VALUE)
- return ;
-
-
- Sleep(100);
-
- CloseReadThread();
-
- TRACE("CMyCESeries关闭串口%d句柄\n", m_portNo);
- CloseHandle (m_hComm);
-
-
- CloseHandle(m_olWrite.hEvent);
- m_hComm = INVALID_HANDLE_VALUE;
- m_bOpened = false;
- }
- BOOL CMyCESeries::WriteSyncPort(const BYTE*buf , DWORD bufLen)
- {
- if( false == m_bSyncOrAsync )
- {
- DWORD dwNumBytesWritten;
- DWORD dwHaveNumWritten =0 ;
-
- int iInc = 0;
- ASSERT(m_hComm != INVALID_HANDLE_VALUE);
- do
- {
- if (WriteFile (m_hComm,
- buf+dwHaveNumWritten,
- bufLen - dwHaveNumWritten,
- &dwNumBytesWritten,
- NULL))
- {
- dwHaveNumWritten = dwHaveNumWritten + dwNumBytesWritten;
-
- if (dwHaveNumWritten == bufLen)
- {
- break;
- }
- iInc++;
- if (iInc >= 3)
- {
- TRACE("CMyCESeries::WriteSyncPort 同步写串口失败3次\n");
- return FALSE;
- }
- Sleep(10);
- }
- else
- {
- TRACE("CMyCESeries::WriteSyncPort 同步写串口失败\n");
- return FALSE;
- }
- }while (TRUE);
-
- return TRUE;
- }
- else
- {
- DWORD dwNumBytesWritten;
- DWORD dwHaveNumWritten =0 ;
-
-
- if (WriteFile (m_hComm,
- buf+dwHaveNumWritten,
- bufLen - dwHaveNumWritten,
- &dwNumBytesWritten,
- &m_olWrite) == FALSE)
- {
- if(GetLastError() != ERROR_IO_PENDING)
- {
- TRACE("CMyCESeries::WriteSyncPort 异步写串口0x20\n");
- return 0x20;
- }
- if(GetOverlappedResult(m_hComm,&m_olWrite,&dwNumBytesWritten,TRUE) == FALSE)
- {
- TRACE("CMyCESeries::WriteSyncPort 异步写串口0x25\n");
- return 0x25;
- }
- }
-
- {
- dwHaveNumWritten = dwHaveNumWritten + dwNumBytesWritten;
-
- if (dwHaveNumWritten == bufLen)
- return TRUE;
- else
- {
- TRACE("CMyCESeries::WriteSyncPort 异步写串口%d失败\n", m_portNo);
- return FALSE;
- }
- }
- }
- }
- BOOL CMyCESeries::SetSeriesTimeouts(COMMTIMEOUTS CommTimeOuts)
- {
- ASSERT(m_hComm != INVALID_HANDLE_VALUE);
- return SetCommTimeouts(m_hComm,&CommTimeOuts);
- }
- BOOL CMyCESeries::GetComOpened()
- {
- return m_bOpened;
- }
|