用vc写了个串口通讯的程序 刚刚接触串口 和单片机进行串口通信,在串口调试助手里通过,但实际硬件通讯时,不能接收数据。
void CTestDlg::OpenPort()
{
//设置Comm端口和波特率
if(m_ctrComm.get_PortOpen())
{
m_ctrComm.put_PortOpen(FALSE);
}
m_sBrate += ",n,8,1";
m_ctrComm.put_Settings(m_sBrate);
m_ctrComm.put_InputMode(1); //输入方式设置为二进制方式
m_ctrComm.put_InBufferSize(1024); //设置输入缓冲区的大小
m_ctrComm.put_OutBufferSize(512); //设置输出缓冲区的大小
m_ctrComm.put_CommPort(m_iCom); //选择COM1
if(!m_ctrComm.get_PortOpen())
{
m_ctrComm.put_PortOpen(TRUE);
}
else
{
MessageBox(TEXT("Cannot open serial port!"));
}
m_ctrComm.put_RThreshold(1);
m_ctrComm.put_SThreshold(1);
m_ctrComm.put_InputLen(0);
m_ctrComm.get_Input();
}
void CTestDlg::OnCommMscomm1()
{
// TODO: Add your message handler code here
//MessageBox(TEXT("OK"));
static bool flag = FALSE;
VARIANT variant_inp;
COleSafeArray safearray_inp;
LONG len, k;
BYTE rxdata[2048];
CString strtemp;
node *temp = new node;
char ch;
if(m_ctrComm.get_CommEvent() == 2)
{
variant_inp = m_ctrComm.get_Input();
safearray_inp = variant_inp;
len = safearray_inp.GetOneDimSize();
for(k = 0; k < len; k++)
safearray_inp.GetElement(&k, rxdata + k);
for(k = 0; k < len; k++)
{
BYTE bt = *(char *)(rxdata + k);
ch = bt;
if (k >= 0 && k <= 3)
temp->ID += ch;
//strtemp.Format("%c", (TCHAR *)bt);
}
}
if (!m_listData.IsExist(temp))
{
m_listData.AddNode(temp);
m_List.InsertItem(m_List.GetItemCount(), temp->ID);
//m_List.AddItem(0, 0, "ddfd", CListItemCheckBox::GenerateProperty());
}
}
这个函数触发不了啊
[解决办法]
今年暑假我写过MSComm的程序,显示部分使用的是TeeChar控件
[解决办法]
检查单片机程序