串口求教解决思路

串口求教
本帖最后由 ready231 于 2014-08-13 17:27:09 编辑
大神帮看一下,我把电脑串口2,3脚短接了,F5下能正常的到结果,ctrl+F5就没结果了,应该是下发了,读没数据。

// TODO: Add your control notification handler code here
int i,Sign;
int D1=0,D2=0,D3=0,D4=0,D5=0,D6=0,D7=0,D8=0,D9=0,D10=0,D11=0,D12=0,D13=0,D14=0,D15=0,D16=0;
int interlow =0,interhigh=0;
CString strtemp1,strtemp2,strtemp3,strtemp4,strtemp5,strtemp6;
CWnd *pWndcpu;
CString ctr1,clear;
CString Static3;

Delay delay;
Sign = 1;   
i = 0;     

clear =_T("");
Static3 = _T("");
unsigned char inter[6];
    inter[0] =0xFF;
inter[1] =0xAA;
inter[2] =0x55;
inter[3] =0x03;
inter[4] =0x03;
inter[5] =0x03;
    unsigned char buffer1[6];
DWORD wCount=0;

for(i = 0; i < 6; i++)
{
buffer1[i] = '\0';
}

((CStatic*) GetDlgItem(IDC_STATIC9))->SetWindowText(Static3);
m_interProgerinter.SetRange(0,100);


GetDlgItem(IDC_EDIT1)->SetWindowText(clear);
GetDlgItem(IDC_EDIT2)->SetWindowText(clear);
GetDlgItem(IDC_EDIT3)->SetWindowText(clear);
GetDlgItem(IDC_EDIT4)->SetWindowText(clear);
GetDlgItem(IDC_EDIT5)->SetWindowText(clear);
GetDlgItem(IDC_EDIT6)->SetWindowText(clear);
GetDlgItem(IDC_EDIT7)->SetWindowText(clear);
GetDlgItem(IDC_EDIT8)->SetWindowText(clear);
GetDlgItem(IDC_EDIT9)->SetWindowText(clear);
GetDlgItem(IDC_EDIT11)->SetWindowText(clear);
GetDlgItem(IDC_EDIT12)->SetWindowText(clear);
GetDlgItem(IDC_EDIT13)->SetWindowText(clear);
GetDlgItem(IDC_EDIT47)->SetWindowText(clear);
GetDlgItem(IDC_EDIT14)->SetWindowText(clear);
GetDlgItem(IDC_EDIT15)->SetWindowText(clear);

        ctrhcominter = comminterface.InitCom();
interdelay.XSleepone(1);
WriteFile(ctrhcominter,inter,6,&wCount,NULL);//发送数据
interdelay.XSleepone(1);
ReadFile(ctrhcominter,buffer1,6,&wCount,NULL);//发送数据
ctr1 = buffer1;
GetDlgItem(IDC_EDIT1)->SetWindowText(ctr1);
strtemp1.Format("%02X",(BYTE)buffer1[0]);
strtemp2.Format("%02X",(BYTE)buffer1[1]);
strtemp3.Format("%02X",(BYTE)buffer1[2]);
strtemp4.Format("%02X",(BYTE)buffer1[3]);

strtemp5.Format("%02X",(BYTE)buffer1[4]);
strtemp6.Format("%02X",(BYTE)buffer1[5]);
 
    sscanf(strtemp6.GetBuffer(0),"%x",&interlow);
strtemp6.ReleaseBuffer();   
sscanf(strtemp5.GetBuffer(0),"%x",&interhigh);
strtemp5.ReleaseBuffer();   


D1 = interlow & 0x01;
D2 = interlow & 0x02;
D3 = interlow & 0x04;
D4 = interlow & 0x08;
D5 = interlow & 0x10;
D6 = interlow & 0x20;
D7 = interlow & 0x40;
D8 = interlow & 0x80;

D9 = interhigh & 0x01;
D10 = interhigh & 0x02;
D11= interhigh & 0x04;
D12 = interhigh & 0x080;
D13 = interhigh & 0x10;
D14 = interhigh & 0x20;
D15 = interhigh & 0x40;
D16= interhigh & 0x80;

if(strtemp1== "FF"&& strtemp2== "AA"&& strtemp3== "55 "&& strtemp4== "03 "&& strtemp5== "FF "&& strtemp6== 

"FF ")
{
// GetDlgItem(IDC_EDIT1)->SetWindowText("正常");
GetDlgItem(IDC_EDIT2)->SetWindowText("正常");
GetDlgItem(IDC_EDIT3)->SetWindowText("正常");
GetDlgItem(IDC_EDIT4)->SetWindowText("正常");
GetDlgItem(IDC_EDIT5)->SetWindowText("正常");
GetDlgItem(IDC_EDIT6)->SetWindowText("正常");
GetDlgItem(IDC_EDIT7)->SetWindowText("正常");
GetDlgItem(IDC_EDIT8)->SetWindowText("正常");
GetDlgItem(IDC_EDIT9)->SetWindowText("正常");
GetDlgItem(IDC_EDIT11)->SetWindowText("正常");
GetDlgItem(IDC_EDIT12)->SetWindowText("正常");
GetDlgItem(IDC_EDIT13)->SetWindowText("正常");
GetDlgItem(IDC_EDIT47)->SetWindowText("正常");
GetDlgItem(IDC_EDIT14)->SetWindowText("正常");
GetDlgItem(IDC_EDIT15)->SetWindowText("正常");

}
else
{

if(strtemp1== "FF"&& strtemp2== "AA"&& strtemp3== "55"&& strtemp4== "03")
{
/* if(D1==0)
{
   GetDlgItem(IDC_EDIT1)->SetWindowText("故障");
   Static3 = Static3+"D1;";
   Sign = 0;
}
else
{
   GetDlgItem(IDC_EDIT1)->SetWindowText("正常");
}*/

if(D2==0)
{
   GetDlgItem(IDC_EDIT2)->SetWindowText("故障");
   Static3 = Static3+"D2;";
   Sign = 0;
}
else
{
   GetDlgItem(IDC_EDIT2)->SetWindowText("正常");
}
if(D3==0)
{
   GetDlgItem(IDC_EDIT3)->SetWindowText("故障");
   Static3 = Static3+"D3;";