Connecting Tech Pros Worldwide Help | Site Map

About MSComm

Newbie
 
Join Date: Aug 2008
Posts: 4
#1: Sep 4 '08
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!!!
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,366
#2: Sep 4 '08

re: About MSComm


I assume you have walkws through the code using your debugger. Yes?

Then you should be able to say on what line the code fails and would know the values of your variables, which you could then post.
Newbie
 
Join Date: Aug 2008
Posts: 4
#3: Sep 5 '08

re: About MSComm


Thank you!
I debugged it and found that what really the problem is my receive program.
It can only receive but would not send the byte 06 I want.
Totally faint...

Help me out..

void CCharTerminalDlg::OnCommMscomm()
{
VARIANT vResponse;
char *str;
char *str1;
int k, nEvent, i;
CByteArray array;
array.RemoveAll();
array.SetSize(1);
nEvent = m_Com.GetCommEvent();

switch(nEvent)
{
case 2:
k = m_Com.GetInBufferCount();
if(k > 0)
{
vResponse=m_Com.GetInput();
str = (char*)(unsigned char*) vResponse.parray->pvData;
}

i = 0;
str1 = str;
while (i < k)
{
i++;
str1++;
}
*str1 = '\0';

m_strReceive += (const char *)str;
for(i=0;i<Count;i++)
array.SetAt(0, 06);
m_Com.SetOutput(COleVariant(array));

break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
}

UpdateData(FALSE);
m_nEdit.Invalidate();
}
Reply