Home > Software engineering >  VC serial communication
VC serial communication

Time:10-06

//send a message
Void Serial: : Write (cstrings message) {
//DWORD res;
DWORD factdatda=(DWORD) 20;
//IsStop=false;

/* if (WriteFile (hCom, message, the message GetLength (), & amp; Factdatda, NULL)) {
IsStop=TRUE;
PurgeComm (hCom, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
AllocConsole ();
//_cprintf (message + "\ r \ n");
}
The else {
AfxMessageBox (" failure ");
} */
Char * data=https://bbs.csdn.net/topics/(LPSTR) (LPCSTR) message;
OVERLAPPED m_osWrite;//set up Overlapped structure
M_osWrite. InternalHigh=0;
M_osWrite. Offset=0;
M_osWrite. OffsetHigh=0;
The COMSTAT COMSTAT;
DWORD dwErrorFlags;
BOOL bWriteStat;
M_osWrite. HEvent=CreateEvent (NULL, FALSE, FALSE, NULL);//initialize the Overlapped structure
BWriteStat=WriteFile (hCom, data, factdatda, & amp; Factdatda, & amp; M_osWrite);//write asynchronous serial port
}
//close a serial port
Void Serial: : Close () {
The CloseHandle (hCom);
}

Void Serial: : read () {
DWORD dwBytesRead=1000;
Char * STR=new char [(int) dwBytesRead];
For (int t=0; T & lt;=(int) dwBytesRead - 1; T++) STR [t]=0;
The COMSTAT COMSTAT;
DWORD dwErrorFlags;
OVERLAPPED m_osRead;//set up Overlapped structure
Memset (& amp; M_osRead, 0, sizeof (OVERLAPPED));
M_osRead. HEvent=CreateEvent (NULL, TRUE, FALSE, NULL);
The ClearCommError (hCom, & amp; DwErrorFlags, & amp; The ComStat);

DwBytesRead=min (dwBytesRead, (DWORD) ComStat. CbInQue);
if (! DwBytesRead) {//if the buffer is empty, then there will be return
AfxMessageBox (" error ");
return;
}
BOOL bReadStatus;
BReadStatus=ReadFile (hCom, STR, dwBytesRead, & amp; DwBytesRead, & amp; M_osRead);//serial asynchronous read, read the contents of the buffer
if (! BReadStatus) {
AfxMessageBox (" error 2 ");
return ;
}
AllocConsole ();
If (dwBytesRead) _cprintf (" the content of the serial port to receive is: % d \ r \ n \ r \ n % s ", (int) dwBytesRead, STR);//if read out the contents of the buffer is not null, then read out the contents of the send out
return ;
}
Error prompt every time buffer is empty, that is, 1, o great god answers

CodePudding user response:

ReadFile before, should WaitCommEvent, Internet search code,

CodePudding user response:

The other equipment need hardware shake hands? If necessary, the code you need to set up the RTS for high potential, so as to receive the data,
  • Related