Home > Software engineering >  Write a serial program, restart the computer after we won't be able to accept a information
Write a serial program, restart the computer after we won't be able to accept a information

Time:04-08

Write a serial program, after restart the computer, a serial port to accept less than information, this is how to return a responsibility?

Just send a message with a serial port tools at this time in the past, then open your serial procedures, if not restart the computer, the program has been can normal send and receive messages, this is how one and the same?

Is my own program problem?

CodePudding user response:

 
Int CHTDLPCard: : OpenPort ()//open the serial port
{
Cstrings strComm;
Int die;
Die=: : GetPrivateProfileInt (" HARDWARE ", "card serial number," 0, ". \ \ card. Ini ");
StrComm. The Format (" % s % d ", "COM", die);
M_hCom=CreateFile (strComm,//COM1 port
GENERIC_READ | GENERIC_WRITE,//allow to read and write
0,//exclusive way
NULL,
OPEN_EXISTING,//open rather than create
0,//synchronously
NULL);

Int a=GetLastError ();
If (m_hCom==INVALID_HANDLE_VALUE)
{
AfxMessageBox (" card serial ports open failure ");
M_bOpen=FALSE;
Return FALSE;
}
//else
//AfxMessageBox (" open the serial port success ");

SetCommMask (m_hCom EV_RXCHAR);
//clean out the input and output buffer
PurgeComm (m_hCom, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);

SetupComm (m_hCom, 2048, 2048);//input buffer and the size of the output buffer is 1024
COMMTIMEOUTS TimeOuts.
//set read timeout
TimeOuts. ReadIntervalTimeout=MAXDWORD;//set timeout interval for the biggest, the total timeout is set to 0, will cause the ReadFile returns immediately and complete the operation
TimeOuts. ReadTotalTimeoutMultiplier=1;
TimeOuts. ReadTotalTimeoutConstant=5;
//set to write timeout
TimeOuts. WriteTotalTimeoutMultiplier=1;
TimeOuts. WriteTotalTimeoutConstant=5;
SetCommTimeouts (m_hCom, & amp; TimeOuts);//set the timeout
GetCommState (m_hCom, & amp; DCB);
DCB. BaudRate=m_nBaud;//baud rate to 9600
DCB. ByteSize=m_nDataBits;//each byte has eight
DCB. Parity=m_nParity;//and white parity bit
DCB. StopBits=m_nStopBits;//stop bit
SetCommState (m_hCom, & amp; DCB);
M_bOpen=TRUE;

The COMSTAT COMSTAT;
DWORD dwErrorFlags=0;
ClearCommError (m_hCom, & amp; DwErrorFlags, & amp; The ComStat);

return 1;
}



Int CHTDLPCard: : SendData publishes the event (HANDLE com serial number/* */,
Function code BYTE bFun/* */,
BYTE bDataLen/* */data length,
PbData BYTE */* *///) to send data, such as SendData publishes the event (com, 0 x01, 0, NULL)
{
//check parameter
If (bDataLen & lt; 0)
return 0;

//total target send length
Int iTxLen=0
+ 2//byte header
+ 1//function code
+ 1//bytes
+ bDataLen//data length
+ 2//check
+ 2//word goeldi
+ 0;

//application buffer
BYTE * pTxData=https://bbs.csdn.net/topics/new BYTE [iTxLen];

//send data structure frame
Int iOfst=0;

PTxData [iOfst]=0 xc1; IOfst++;//byte header
PTxData [iOfst]=0 xc2; IOfst++;

PTxData [iOfst]=bFun; IOfst++;//function code

PTxData [iOfst]=bDataLen + 2;/* * a + two check/iOfst++;//bytes

Memcpy (pTxData + iOfst pbData, bDataLen); IOfst +=bDataLen;//data

{//computing parity
Uint16_t checksum=getChecksum (pTxData iOfst);

//ULONG uSumL=0;//and the cumulative of all data before
//for (int n=0; N & lt; IOfst; N++) {uSumL +=pTxData [n]. }
////low (high 16 + 16) after the
//WORD wSum=(WORD) (~ (HIWORD (uSumL) + LOWORD (uSumL)));

# ifdef _BIG_ENDIAN//big end model
PTxData [iOfst]=checksum & gt;> 8; IOfst++;
PTxData [iOfst]=checksum; IOfst++;
//pTxData [iOfst]=HIBYTE (wSum); IOfst++;
//pTxData [iOfst]=LOBYTE (wSum); IOfst++;
# the else//! _BIG_ENDIAN//small side mode
PTxData [iOfst]=checksum & gt;> 8; IOfst++;
PTxData [iOfst]=checksum; IOfst++;
//pTxData [iOfst]=LOBYTE (wSum); IOfst++;
//pTxData [iOfst]=HIBYTE (wSum); IOfst++;
# endif

}

PTxData [iOfst]=0 x0a; IOfst++;//word goeldi
PTxData [iOfst]=0 x0d; IOfst++;


//COMSTAT COMSTAT;//clean up buffer
//DWORD dwErrorFlags;
//ClearCommError (m_hCom, & amp; DwErrorFlags, & amp; The ComStat);
//PurgeComm (m_hCom, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);

//here with serial communication interface to send data
DWORD bytesWritten;
Bool ret=WriteFile (com, pTxData iOfst, & amp; BytesWritten, 0);
//end cleanup
The delete [] pTxData;
If (ret)
Return 1;
The else
return 0;
}

CodePudding user response:

Is I did not open serial port? Or is a computer problem, the first open the serial port have anything special to do, I didn't write in the program?

I send data, neither program executes instructions, also do not return information,

Use a serial port monitoring software to see the next, only issued by the ministry of information, not read information

CodePudding user response:

Wrong here?

I found that when performing SetCommState is a program error, which is set when a serial port to the mistake, check again carefully found Settings DCB. StopBits attribute direct assignment 1, but in fact it doesn't mean stop bit is 1, 1.5, but about StopBits description is like this: DCB, StopBits//two stop bit 0=1, 1.5, 2, when the StopBits value of 0 is on behalf of the stop bit is 1, when the value of 1 represents the stop bit is 1.5, and the computer did not 1.5 this setting, so SetCommState error, namely initialize serial port without success,
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Copyright statement: this article to CSDN blogger (kfhzy) of the original articles, follow BY CC 4.0 - SA the copyright agreement, reproduced and this statement, please attach the original source link
The original link: https://blog.csdn.net/kfhzy/article/details/11607545

CodePudding user response:

Before GetCommState DCB ascribed value DCBlength

DCB DCB={sizeof (DCB)};
GetCommState (m_hCom, & amp; DCB);

  • Related