Home > Software engineering >  MFC to write a serial port communication program receive less than data
MFC to write a serial port communication program receive less than data

Time:11-18

My own written in MFC PC with a serial communication module, can be normal used a machine to send data to the microcontroller, but receive less than single chip from the data, in addition to using a serial port communication assistant also test, found that can send and receive data, found that after the DEBUG WaitCommEvent () there is no received any news of serial port communication, don't know what went wrong, consulting,,

PS: in the PC serial port communication module code is as follows:
CCommox: : CCommox (void)
{
Memset (& amp; OsWrite, 0, sizeof (osWrite));
COMFile=NULL;
OsWrite. HEvent=CreateEvent (NULL, FALSE, FALSE, NULL);
If (osWrite hEvent==NULL) {
AfxMessageBox (_T (" create event failed!" ));
}
Memset (& amp; OsRead, 0, sizeof (OVERLAPPED));
OsRead. HEvent=CreateEvent (NULL, FALSE, FALSE, NULL);
If (osRead hEvent==NULL) {
AfxMessageBox (_T (" create event failed!" ));
}
Memset (lpInBuffer, 0, sizeof (lpInBuffer));
}


CCommox: : ~ CCommox (void)
{
}

Bool CCommox: : OpenSerialPort (int COMx)
{
DCB dcb;
BOOL fRetVal;
COMMTIMEOUTS COMMTIMEOUTS;
Cstrings szCom;
SzCom. The Format (_T (" \ \ \ \. \ \ COM % d "), COMx);
COMFile=CreateFile (szCom GetBuffer (50), GENERIC_READ | GENERIC_WRITE,//to be read, write
FILE_SHARE_READ FILE_SHARE_READ | FILE_SHARE_WRITE,//no sharing, FILE_SHARE_WRITE, FILE_SHARE_DELETE
NULL,//no security tracing
OPEN_EXISTING,//open the existing file
/* FILE_ATTRIBUTE_NORMAL | */FILE_FLAG_OVERLAPPED,//file attributes, if want to have a serial port set to asynchronously, so should be set into FILE_FLAG_OVERLAPPED
NULL//A valid handle to an existing device handle A valid handle to A template file with the GENERIC_READ access right
);
If (INVALID_HANDLE_VALUE=COMFileTemp COMFile/* */https://bbs.csdn.net/topics/={
Return (FALSE);
}
//set the buffer, the input/output size (in bytes)
COMFileTemp SetupComm (/* */COMFile, 512, 512);
//specified monitoring events _ received character in the buffer
SetCommMask (/* */COMFile COMFileTemp EV_RXCHAR);//EV_RXCHAR: have received data from the input buffer, which receives a byte and into the input buffer,


CommTimeOuts. ReadIntervalTimeout=1/* 0 XFFFFFFFF */.//management of a single character reading time, says from the read operation (i.e. ReadFile) begin each a millisecond can start reading to the next character (including the first character), then OK, otherwise returns,
CommTimeOuts. ReadTotalTimeoutMultiplier=1;//
CommTimeOuts. ReadTotalTimeoutConstant=2;
CommTimeOuts. WriteTotalTimeoutMultiplier=2 * CBR_115200/115200;
CommTimeOuts. WriteTotalTimeoutConstant=0;
//given a serial port to read and operation time limit
COMFileTemp SetCommTimeouts (/* */COMFile, & amp; CommTimeOuts);

//set the serial port parameters: the baud rate=115200; 1 stop bit; Without checking; Eight
DCB. DCBlength=sizeof (DCB);
COMFileTemp GetCommState (/* */COMFile, & amp; DCB);
DCB. BaudRate=CBR_115200;//is 9200, is now I changed 115200
DCB. StopBits=ONESTOPBIT;
DCB. Parity=NOPARITY;
DCB. ByteSize=8;
DCB. FBinary=TRUE;//binary communication, the character communication
DCB. FOutxDsrFlow=0;
DCB. FDtrControl=DTR_CONTROL_ENABLE;
DCB. FOutxCtsFlow=0;
DCB. FRtsControl=RTS_CONTROL_ENABLE;
DCB. FInX=DCB. FOutX=1;
DCB. XonChar=0 x11;
DCB. XoffChar=0 x13;
DCB. XonLim=100;
DCB. XoffLim=100;
DCB. FParity=TRUE;

//communication equipment according to equipment control block configuration
COMFileTemp fRetVal=SetCommState (/* */COMFile, & amp; DCB);//if the function call is successful, the return value is a 0; If the function call fails, the return value is 0,

if (! FRetVal) return FALSE.

//clearing the buffer
COMFileTemp PurgeComm (/* */COMFile, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);

//specified serial execution extensions
COMFileTemp EscapeCommFunction (/* */COMFile, SETDTR);

////set serial port "received character in the buffer" event
/* if (! SetCommMask (COMFile EV_RXCHAR))
Return (FALSE); */

Return TRUE;
}

Bool CCommox: : CloseSerialPort (int COMx)
{
//serial port of all events
SetCommMask (COMFile, 0);

//clear data terminal ready signal
EscapeCommFunction (COMFile CLRDTR);

//discard output or input buffer character the communication resources and put an end to hang on communication resources to read and write operation//field in
PurgeComm (COMFile, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);

The CloseHandle (COMFile);
COMFile=NULL;
return true;
}

Bool CCommox: : TransmitData (PBYTE pOutData, int nDataLen)
{
If (NULL==pOutData | | nDataLen & lt; 1)
return FALSE;
//while (nDataLen & gt; 0 {
DWORD nLen=0;
if (! WriteFile (COMFile, LPCVOID pOutData, nDataLen, & amp; NLen, & amp;/* */osWrite ShareEvent)) {//parameters from left to right to handle respectively, data buffer pointer, write the number of bytes, used to hold the number of bytes actually written storage area, to determine whether to read the success,
As to//OVERAPPED structure pointer NULL
If (WAIT_OBJECT_0==WaitForSingleObject (osWrite hEvent, 0 XFFFFFF))
ResetEvent (osWrite hEvent);
The else
ResetEvent (osWrite hEvent);
}
return true;
}

Void CCommox: : QueryKey (hkeys hkeys)
{
# define MAX_KEY_LENGTH 255
# define MAX_VALUE_NAME 16383
//TCHAR achKey [MAX_KEY_LENGTH];//buffer for subkey name
//DWORD cbName;//the size of the name string
TCHAR achClass [MAX_PATH]=TEXT (" ");//buffer for the class name
DWORD cchClassName=MAX_PATH;//the size of the class string
DWORD cSubKeys=0;//the number of subkeys
DWORD cbMaxSubKey;//longest subkey size
DWORD cchMaxClass;//longest class string
DWORD cValues;//the number of values for the key
DWORD cchMaxValue;//longest value name
DWORD cbMaxValueData;//longest value data
DWORD cbSecurityDescriptor;//the size of the security descriptor
FILETIME ftLastWriteTime;//the last write time

DWORD I, retCode;

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related