Home > Back-end >  Serial port Buildcommdcb () sets the DCB always fail
Serial port Buildcommdcb () sets the DCB always fail

Time:10-06

Bool CMySerial: : OpenSerialPort (CWnd * pParent, UINT portnr, UINT baud, would be parity, UINT databits, UINT stopbits, dwords dwCommEvents, UINT writebuffersize)
{
//assert (portnr & gt; 0 & amp; & Portnr & lt; 5);
Assert (pParent!=NULL);

//if a serial port monitoring thread, then close it
If (m_bThreadAlive)
{
Do
{
SetEvent (m_hShutdownEvent);
} while (m_bThreadAlive);
TRACE (" Thread ended \ n ");
}

//create serial asynchronous communication event
If (m_ov hEvent!=NULL)
ResetEvent (m_ov hEvent);
The else
M_ov. HEvent=CreateEvent (NULL, TRUE, FALSE, NULL);
//create send events
If (m_hWriteEvent!=NULL)
ResetEvent (m_hWriteEvent);
The else
M_hWriteEvent=CreateEvent (NULL, TRUE, FALSE, NULL);
//create a closed serial events
If (m_hShutdownEvent!=NULL)
ResetEvent (m_hShutdownEvent);
The else
M_hShutdownEvent=CreateEvent (NULL, TRUE, FALSE, NULL);

//initialization event array
M_hEventArray [0]=m_hShutdownEvent;//the highest priority
M_hEventArray [1]=m_ov. HEvent;
M_hEventArray [2]=m_hWriteEvent;

//initialize the critical resources
InitializeCriticalSection (& amp; M_csCommunicationSync);

//save a serial port operation window pointer
M_pParent=pParent;

//for sending buffer application space
If (m_szWriteBuffer!=NULL)
The delete [] m_szWriteBuffer;
M_szWriteBuffer=new would [writebuffersize];

//save serial number
M_nPortNr=portnr;

//will send data
M_nWriteBufferSize=writebuffersize;
M_dwCommEvents=dwCommEvents;

BOOL bResult=FALSE;
Would be * szPort=new would [100].
//would be * szBaud=new would [100].
Cstrings szBaud;


//lock the critical variable
The EnterCriticalSection (& amp; M_csCommunicationSync);

//if a serial port has opened, then closed his
If (m_hComm!=NULL)
{
The CloseHandle (m_hComm);
M_hComm=NULL;
}

//state information
COM swprintf (szPort, 100, _T (" % d "), portnr);
//swprintf (szBaud, 100, _T (" baud=% d parity data=https://bbs.csdn.net/topics/%d stop=% c=% d "), baud, parity, databits, stopbits);
SzBaud. The Format (_T (" baud=% d parity data=https://bbs.csdn.net/topics/%d stop=% c=% d "), baud, parity, databits, stopbits);
AfxMessageBox (szBaud);
//open the serial port
M_hComm=CreateFile (szPort,//the communication port string (COMX)
GENERIC_READ | GENERIC_WRITE,//read/write types
0,//comm devices must be the opened with exclusive access
NULL,//no security attributes
OPEN_EXISTING,//comm devices must use OPEN_EXISTING
FILE_FLAG_OVERLAPPED,//Async I/O
NULL);//the template must be zero for comm devices

If (m_hComm==INVALID_HANDLE_VALUE)
{
//open the failure
AfxMessageBox (_T (" open port failure "));
The delete [] szPort;
//delete [] szBaud;

return FALSE;
}

//timeout parameter
M_CommTimeouts. ReadIntervalTimeout=1000;
M_CommTimeouts. ReadTotalTimeoutMultiplier=1000;
M_CommTimeouts. ReadTotalTimeoutConstant=1000;
M_CommTimeouts. WriteTotalTimeoutMultiplier=1000;
M_CommTimeouts. WriteTotalTimeoutConstant=1000;

//set the serial port parameters
If (SetCommTimeouts (m_hComm, & amp; M_CommTimeouts))
{
If (SetCommMask (m_hComm, dwCommEvents))
{
If (GetCommState (m_hComm, & amp; M_dcb))
{
M_dcb. EvtChar='q';
M_dcb. FRtsControl=RTS_CONTROL_ENABLE;
//SetDCB (& amp; M_dcb, baud, parity, databits, stopbits);
//m_dcb. BaudRate=baud;
//m_dcb. Parity=Parity;
//m_dcb. ByteSize=databits;
//m_dcb. StopBits=StopBits;
If (BuildCommDCBW (szBaud, & amp; M_dcb))
{
if (! SetCommState (m_hComm, & amp; M_dcb))
ProcessErrorMessage (L "SetCommState ()");
}
The else
BuildCommDCBW ProcessErrorMessage (L "()");.
}
The else
ProcessErrorMessage (L "GetCommState ()");
}
The else
SetCommMask ProcessErrorMessage (L "()");
}
The else
SetCommTimeouts ProcessErrorMessage (L "()");

The delete [] szPort;
//delete [] szBaud;

//to empty the send and receive buffer
PurgeComm (m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);

//unlock the critical variable
LeaveCriticalSection (& amp; M_csCommunicationSync);

TRACE (" Initialisation for communicationport % d completed. \ nUse Startmonitor to communicate. \ n ", portnr);

return TRUE;
}
Void CMySerial: : ReceiveChar (CMySerial * port, COMSTAT COMSTAT)
{
BOOL bRead=TRUE;
BOOL bResult=TRUE;
DWORD dwError=0;
DWORD BytesRead=0;
Unsigned char RXBuff;

For (;; )
{
//Gain ownership of the comm port critical section.
//This process guarantees no other part of This program
//is using the port object.

The EnterCriticalSection (& amp; The port - & gt; M_csCommunicationSync);

//ClearCommError () will update the COMSTAT structure and
//the clear any other errors.

BResult=ClearCommError (port - & gt; M_hComm, & amp; DwError, & amp; The comstat);

LeaveCriticalSection (& amp; The port - & gt; M_csCommunicationSync);

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related