Home > Software engineering >  Using ReadFile function couldn't read data is how to return a responsibility? Use monitoring so
Using ReadFile function couldn't read data is how to return a responsibility? Use monitoring so

Time:10-02

Hc=CreateFile (Com, GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);

If (hc==INVALID_HANDLE_VALUE)
{
MessageBox (" Open COM error!" , "the Error Prompt," MB_ICONSTOP | MB_OK);
Return bRet.
}
if(! GetCommState (hc, & amp; DCB))
{
MessageBox (" Set COM error!" , "the Error Prompt," MB_ICONSTOP | MB_OK);
The CloseHandle (hc);
Return bRet.
}

DCB. BaudRate=COMM_SPEED2;//COMM_SPEED38400
DCB. ByteSize=COMM_BYTE;
DCB. Parity=COMM_PARITY;
DCB. FParity=COMM_ENABLE_PARITY;
DCB. StopBits=TWOSTOPBITS;//COMM_STOP
DCB. FOutxCtsFlow=FALSE;
DCB. FOutxDsrFlow=FALSE;
DCB. FBinary=TRUE;
DCB. FOutX=FALSE;
DCB. FInX=FALSE;

if(! SetCommState (hc, & amp; DCB))
{
The CloseHandle (hc);
MessageBox (" Set COM error!!!!! ", "the Error Prompt," MB_ICONSTOP | MB_OK);
Return bRet.
}
GetCommModemStatus (HCC, & amp; DwStatus);
Commtimeouts. ReadIntervalTimeout=400;
Commtimeouts. ReadTotalTimeoutConstant=500;
Commtimeouts. ReadTotalTimeoutMultiplier=500;
Commtimeouts. WriteTotalTimeoutConstant=500;
Commtimeouts. WriteTotalTimeoutMultiplier=2000;

SetCommTimeouts (hc, & amp; Commtimeouts);

PurgeComm (hc, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);

BYTE blockdata [41]={0 x00};
X3a BYTE checkdata [13]={0, 0 x16, 0 x30, 0 x05, 0 xf2, 0 x00 to 0 x00 to 0 x00 to 0 x00 to 0 x00 to 0 x00 to 0 x0d, 0 x0a};
BYTE readdata [10]={0 x00};

for(i=0; I & lt; Download. Bank_no; I++)
{
count=0;
Itoa (I, SSS, 10);
Strcpy (SSC, "Transfer error, block, No:");
Strcat (SSC, SSS);

Repe:
//send the checksum and block number
Int temp=download. The checksum [I] & amp; 0 XFF.
Blocknum=download. Blockno [I] - temp& 0 x7f;
Checkdata [5]=(blocknum> 8) & amp; 0 XFF.
Checkdata [6]=blocknum & amp; 0 XFF.
Checkdata [7]=(download. Checksum [I] & gt;> 8) & amp; 0 XFF.
Checkdata [8]=download. The checksum [I] & amp; 0 XFF.
CheckSum (checkdata, 13);
WriteFile (hc, checkdata, 13, & amp; Aa, NULL);
Sleep (200);
ReadFile (hc, readdata, 1, & amp; Aa, NULL);
ReadFile (hc, & amp; Readdata [1], 8, & amp; Aa, NULL);
If (readdata [4]==0 x88)
{
Sleep (200);
}
The else
{
count++;
If (count>
=3){
MessageBox (SSC, "Error", MB_ICONSTOP | MB_OK);
Return bRet.
}
The else
Goto repe.
}

CodePudding user response:

Such a problem, I say, I first read the agreement carefully, download a CRC or other inspection tools, and then use a serial port gadgets, hex format to send, debugging through the coding, good luck

CodePudding user response:

First of all, thank you, is the same, send data is right, also use monitoring software to see, also for CRC, is associated with the setting of timeouts, thank you

CodePudding user response:

CodePudding user response:

This program is a bit rough, you use CSerialPort to find this class are ready! To use

CodePudding user response:

Remind: serial port is divided into two kinds: the ring and the ring,
  • Related