请各位高手帮忙解决VC++编写的上位机与单片机串口通信的问题 30
我是想用VC上位机的按钮控制单片机的发光二极管的亮灭的,但得不到像要的结果。貌似接收的数据不对。请各位高手帮忙解决。控件的部分程序如下voidCLedDlg::OnOnC...
我是想用VC上位机的按钮控制单片机的发光二极管的亮灭的,但得不到像要的结果。貌似接收的数据不对。请各位高手帮忙解决。控件的部分程序如下
void CLedDlg::OnOnCommMscomm1()
{
// TODO: Add your control notification handler code here
VARIANT variant_inp;
COleSafeArray safearray_inp;
LONG len,k;
BYTE rxdata[2048];
if(m_comm.GetCommEvent()==2)
{
variant_inp=m_comm.GetInput();
safearray_inp=variant_inp;
len=safearray_inp.GetOneDimSize();
for(k=0;k<len;k++)
safearray_inp.GetElement(&k,rxdata+k);
binData.SetSize(len);//binData为CByteArray型全局变量
for(k=0;k<len;k++)
{
BYTE bt=*(int*)(rxdata+k);
binData.SetAt(k,bt);
}
}
}
void CLedDlg::OnButton1()
{
// TODO: Add your control notification handler code here
unsigned char chData[8];
LONG len,k;
CString str;
chData[1]=0x01;
chData[2]=0xfe;
len=binData.GetSize();
for(k=0;k<len;k++)
chData[0]+=binData.GetAt(k);
if(GetDlgItem(IDC_BUTTON1)->GetWindowText(str),str=="led1亮")
{
GetDlgItem(IDC_BUTTON1)->SetWindowText("led1灭");
chData[0] = chData[0] | chData[1] ;
}
else
{
GetDlgItem(IDC_BUTTON1)->SetWindowText("led1亮");
chData[0] = chData[0] & chData[2] ;
}
binData.RemoveAll();
binData.Add(chData[0]);
COleVariant var(binData);
m_comm.SetOutput(var);
} 展开
void CLedDlg::OnOnCommMscomm1()
{
// TODO: Add your control notification handler code here
VARIANT variant_inp;
COleSafeArray safearray_inp;
LONG len,k;
BYTE rxdata[2048];
if(m_comm.GetCommEvent()==2)
{
variant_inp=m_comm.GetInput();
safearray_inp=variant_inp;
len=safearray_inp.GetOneDimSize();
for(k=0;k<len;k++)
safearray_inp.GetElement(&k,rxdata+k);
binData.SetSize(len);//binData为CByteArray型全局变量
for(k=0;k<len;k++)
{
BYTE bt=*(int*)(rxdata+k);
binData.SetAt(k,bt);
}
}
}
void CLedDlg::OnButton1()
{
// TODO: Add your control notification handler code here
unsigned char chData[8];
LONG len,k;
CString str;
chData[1]=0x01;
chData[2]=0xfe;
len=binData.GetSize();
for(k=0;k<len;k++)
chData[0]+=binData.GetAt(k);
if(GetDlgItem(IDC_BUTTON1)->GetWindowText(str),str=="led1亮")
{
GetDlgItem(IDC_BUTTON1)->SetWindowText("led1灭");
chData[0] = chData[0] | chData[1] ;
}
else
{
GetDlgItem(IDC_BUTTON1)->SetWindowText("led1亮");
chData[0] = chData[0] & chData[2] ;
}
binData.RemoveAll();
binData.Add(chData[0]);
COleVariant var(binData);
m_comm.SetOutput(var);
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询