|
Experiment between two computers, the software can receive the data sent from the serial port of the other computer (use the serial debugging tool to send manually or automatically).
But when sending data with a single-chip microcomputer, the software can receive a little bit of data and directly pop up an error saying that the program should be closed; or there is no response and no data is received.
The serial event source is:
void CSerialView :: OnOnCommMscomm1 ()
{
// TODO: Add your control notification handler code here
Ranch
VARIANT variant_inp;
COleSafeArray safearray_inp;
LONG len, k;
BYTE rxdata [2048]; // Set BYTE array An 8-bit integerthat is not signed.
CString strtemp;
if (m_comm.GetCommEvent () == 2) // Event value 2 means there are characters in the receive buffer
{///////// Add processing code below
variant_inp = m_comm.GetInput (); // Read buffer
safearray_inp = variant_inp; // VARIANT type variables are converted to ColeSafeArray type variables
len = safearray_inp.GetOneDimSize (); // Get effective data length
for (k = 0; k <len; k ++)
safearray_inp.GetElement (&k, rxdata + k); // Convert to BYTE type array and save it to a file later
for (k = 0; k <len; k ++) // Convert an array to a Cstring variable
{
BYTE bt = * (char *) (rxdata + k); // character
strtemp.Format ("% c", bt); // Send characters to the temporary variable strtemp to store
Ranch
m_strRXData + = strtemp; // Add the corresponding string to the receive edit box
}
FILE * pFile = fopen ("ecg.dat", "ab +"); // Append to open a binary (b) file
fwrite (rxdata, 1, len, pFile); // write file
fflush (pFile);
}
UpdateData (FALSE); // Update edit box content
} |
|