|
Hey, I'm programming serial port with MSComm currently. In a program I try to communicate with MCU, but something goes wrong that the program will always die the minute I execute it.
Would anyone have a look at it and help me out? Thank you!!
void CCharTerminalDlg::OnOnCommMscomm()
{
VARIANT input1;
BYTE rxData;
COleSafeArray safeArray1;
switch(m_Com.GetCommEvent())
{
case 2:
input1=m_Com.GetInput();
safeArray1=input1;
safeArray1.GetElement(0,&rxData);
if(rxData==06)//if MCU reply with 06
//m_state would be 0 to tell PC to send another byte
m_state=0;
break;
}
}
void CCharTerminalDlg::OnSend()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
UpdateData(FALSE);
FILE* fp=fopen(m_strSend,"rb");//Get the location
//of file to be sent
while(!feof(fp))
{
if(m_state==0)
{
char TxData;
fread( &TxData, sizeof( char ), 1, fp);
//1 byte each time
CByteArray array;
array.RemoveAll();
array.SetSize(1);
array.SetAt(0, TxData);
m_Com.SetOutput(COleVariant(array));
m_state=1;//When a byte is sent,
//m_state is 1 to invalidate the next sending, giving MCU
//enough time to receive and do other things
}
}
}
Thank you!!!
|