// special command for this test serial Serial.h # if! Defined (AFX_SERIAL_H__F97E181C_46A7_404D_98FB_07595B7767F3__INCLUDED _) # define AFX_SERIAL_H__F97E181C_46A7_404D_98FB_07595B7767F3__INCLUDED_
#if _MSC_VER> 1000 # prgma overce # endif //_MSC_VER> 1000
#define maxserbuf 2048 // Serial port maximum cache
class CSerial {private: // Serial detection program static void CommWatchProc (LPVOID pParam); HANDLE m_hIDComDev; static BOOL m_bOpened; // port is open static HANDLE R_Event; // event reception static HANDLE W_Event; // send event static BYTE m_szRevBuf [ Maxserbuf]; static dword m_dwrevbuf; // read data to serial port BOOL READCOMM (PBYTE PBUF, DWORD * PBUFLEN); // Write data to serial BOOL WRITECMMM (CONST DWSIGNED Char * PBBUF, DWORD DWSIZE); public: void starmonitor (); // open the monitor thread VOID StarRevData (); // start receiving data VOID StopRevData (); // stops receiving data DWORD Transmit (PBYTE pbSendBuffer, DWORD cbSendLength, PBYTE pbRecvBuffer, DWORD * pcbRecvLength, DWORD dwOverTime = 1000); // serial Whether to open BOOL ISOPEN () () {RETURN M_BOPENED;}; // Open Serial Bool Open (int nport = 1, int NBAUD = 9600, int nbytesize = 8, int nParity = 0, int nstopbits = 1, int nsendbuffer = maxserbuf); // Close the serial port void close (); CSERIAL (); Virtual ~ CSerial ();
#ndif //! defined (AFX_SERIAL_H__F97E181C_46A7_404D_98FB_07595B7767F3__included_) serial.cpp # include "stdafx.h" #include "serial.h"
#include
/ / Create a read event, generate the event after reading the data; // Create a write event, generate the event after writing the data;
/ / The serial port is opened BOOL CSERIAL: :: m_bople
// Serial Cache Byte CSerial :: m_szrevbuf [MAXSERBUF]; DWORD CSERIAL :: m_dwrevbuf;
//// construction / destruction // cserial :: cserial () {m_bopENed = false;}
CSERIAL :: ~ cserial () {close ();
Bool CSerial :: Open (int NPORT, INT NBAUD, INT NBYTESIZE, INT NSENDBUFFER {IF (isopen ()) Return True; DWORD LPERRCODE = 0; Char Szport [15] = {"/ 0" }; wsprintf (szPort, "COM% d", nPort); m_hIDComDev = CreateFile (szPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL); if (m_hIDComDev == INVALID_HANDLE_VALUE) return FALSE; // setting a timeout COMMTIMEOUTS CommTimeOuts; CommTimeOuts.ReadIntervalTimeout = MAXDWORD; CommTimeOuts.ReadTotalTimeoutMultiplier = 0; CommTimeOuts.ReadTotalTimeoutConstant = 0; CommTimeOuts.WriteTotalTimeoutMultiplier = 0; CommTimeOuts.WriteTotalTimeoutConstant = 0; SetCommTimeouts (m_hIDComDev, & CommTimeOuts); // set to character shape if ( ! Setcommmask (m_hidcomdev, ev_rxchar)) {goto err;} // Setting the input / output cache if (! Setupcomm (m_hidcomdev, nsendbuffer, nsendbuffer) {Goto err;} // Clear all comunication errors ClearCommError (m_hIDComDev, & lpErrCode, NULL); // empty the cache if {goto err;} (PurgeComm (m_hIDComDev, PURGE_RXCLEAR | PURGE_TXCLEAR | | PURGE_RXABORT PURGE_TXABORT)!) // Gets COM Settings // Gets COM Settings DCB dcb; dcb.DCBlength = sizeof (DCB); GetCommState (m_hIDComDev, & dcb); dcb.BaudRate = nBaud; dcb.ByteSize = nByteSize; dcb.Parity = nParity; dcb.StopBits = nStopBits; char sDCBState [100]; wsprintf (sdcbstate, "BAUD =% d Parity = n Data =% D Stop =% D"
, NBAUD, NBYTESIZE, NSTOPBITS; if (! Buildcommdcb (SDCBSTATE, & DCB)) {DWORD RET = getLastError (); goto err;} if (! Setcommstate (m_hidcomdev, & dcb) {dword ret = getLastError (); goto err } M_bopened = true; return true; err: closehandle (m_hidcomdev); return false;} void cserial :: close () {if (! Isopen () || m_hidcomdev == null) return; closehandle (r_event); CloseHandle m_hidcomdev); m_bopened = false; m_hidcomdev = null; m_bopENed = false;}
Bool CSerial :: Readcomm (PBYTE PBUF, DWORD * PBUFLEN) {Assert (PBUF! = NULL); if (! Isopen ()) Return False; overlapped ovread; Memset (& Ovread, 0, Sizeof (Ovread); ovread.hevent = CreateEvent (0, True, 0, 0); if (! Readfile (M_HidComDev, PBUF, * Pbufflen, Pbufflen, & Ovread) Return False; CloseHandle (Ovread.hevent); return true;}
Bool CSerial :: WriteComm (Const unsigned char * pbbuf, dword dwsize) {assert (pbbuf! = Null); if (! Isopen ()) Return False; if (dwsize> maxserbuf) Return False; int rt = 0; Overlapped OV MEMSET (& OV, 0, SizeOf (OV)); oh., 0,0); DWORD DWBYTESWRITEN = 0; // DO {IRet = Writefile (M_HidComDev, Pbbuf, DWSIZE, & DWBYTESWRITEN, & OV ); If (IRET == 0) {WaitForsingleObject (Ov.hevent, Infinite);}} // while (ov.internalhigh! = Dwsize); CloseHandle (oov.hevent); return true;}
void CSerial :: CommWatchProc (LPVOID pParam) {assert (pParam = 0!); HANDLE _hIDComDev = pParam; DWORD dwBytesRead = 0; DWORD dwWaitOverTime = 0; // ms single bit time BOOL isSend = FALSE; char szTmp [MAXSERBUF]; OVERLAPPED ovRead; memset (& ovRead, 0, sizeof (ovRead)); ovRead.hEvent = CreateEvent (0, true, 0, 0); while (m_bOpened) {WaitForSingleObject (W_Event, INFINITE); if (isSend && dwWaitOverTime> = 2 ) {PulseEvent (R_Event); dwWaitOverTime = 0; isSend = FALSE;} dwBytesRead = 1; // ComStat.cbInQue; if (dwBytesRead m_dwRevBuf> MAXSERBUF) {PulseEvent (R_Event); continue;}! if (ReadFile (_hIDComDev, SZTMP, DWBYTESREAD, & DWBYTESREAD, & Ovread) Return; if (dwbytesread> 0) {issend = true; dwwaitovertime = 0; m_szrevbuf [m_dwrevbuf] = SZ TMP [0]; // dwbytesread; m_dwrevbuf = dwbytesread; Continue;} DWWAITOVERTIME ; Sleep (1);} closehandle (ovread.hevent);}
(! IsOpen ()) DWORD CSerial :: Transmit (PBYTE pbSendBuffer, DWORD cbSendLength, PBYTE pbRecvBuffer, DWORD * pcbRecvLength, DWORD dwOverTime) {if return ERROR_ACCESS_DENIED; if (cbSendLength> MAXSERBUF || pbRecvBuffer == NULL || pbSendBuffer == NULL || pcbRecvLength == NULL) return ERROR_INVALID_PARAMETER; WriteComm (pbSendBuffer, cbSendLength); m_dwRevBuf = 0; StarRevData (); if (WaitForSingleObject (R_Event, dwOverTime)) {StopRevData (); return WAIT_TIMEOUT;} StopRevData (); memcpy (pbRecvBuffer , m_szrevbuf, m_dwrevbuf); * pcbrecvlength = m_dwrevbuf; m_dwrevbuf = 0; return 0;} void cserial :: stopRevdata () {resetEvent (w_event);
Void CSerial :: StarRevdata () {set atvent (w_event);
Void CSerial :: Starmonitor () {// Create a read event, generate this event R_Event = CreateEvent (NULL, TRUE, FALSE, NULL) after reading the data; // Create a write event, generate the event W_Event after writing data = CreateEvent (NULL, TRUE, FALSE, NULL); // Start :: _ BeginThread (CommwatchProc, 0, M_HIDCOMDEV);