我用的串口通信程序接收一批字符,每次都在接受到某个字符以后就停在那里了
然后程序就不能收也不能发数据了.好奇怪啊,大家帮我看看啊,谢谢了
下面是我的程序:BOOL CSCommTestDlg::OnInitDialog()
{
CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
} // Set the icon for this dialog.  The framework does this automatically
//  when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here
if(m_ctrlComm.GetPortOpen())
m_ctrlComm.SetPortOpen(FALSE); m_ctrlComm.SetCommPort(1);   //选择com1
if( !m_ctrlComm.GetPortOpen())
m_ctrlComm.SetPortOpen(TRUE);//打开串口
else
AfxMessageBox("cannot open serial port"); m_ctrlComm.SetSettings("115200,n,8,1"); 
//波特率115200,无校验,8个数据位,1个停止位
m_ctrlComm.SetRThreshold(1); 
//参数1表示每当串口接收缓冲区中有多于或等于1个字符时将引发一个接收数据的OnComm事件
m_ctrlComm.SetInputLen(0);  //设置当前接收区数据长度为0
m_ctrlComm.GetInput();//先预读缓冲区以清除残留数据

return TRUE;  // return TRUE  unless you set the focus to a control
}void CSCommTestDlg::OnComm() 
{
// TODO: Add your control notification handler code here
VARIANT variant_inp;
COleSafeArray safearray_inp;
LONG len,k;
BYTE rxdata[10000]; //设置BYTE数组 An 8-bit integerthat is not signed.
CString strtemp;
if(m_ctrlComm.GetCommEvent()==2)     //事件值为2表示接收缓冲区内有字符
{
variant_inp=m_ctrlComm.GetInput();   //读缓冲区
safearray_inp=variant_inp;     //VARIANT型变量转换为ColeSafeArray型变量
len=safearray_inp.GetOneDimSize(); //得到有效数据长度
for(k=0;k<len;k++)
safearray_inp.GetElement(&k,rxdata+k);//转换为BYTE型数组
for(k=0;k<len;k++)             //将数组转换为Cstring型变量
{
BYTE bt=*(char*)(rxdata+k);      //字符型
strtemp.Format("%c",bt);       //将字符送入临时变量strtemp存放
m_strRXData+=strtemp;    //加入接收编辑框对应字符串    
}
m_test=len;
UpdateData(FALSE);
}
UpdateData(FALSE);           //更新编辑框内容
}void CSCommTestDlg::OnButtonManualsend() 
{
// TODO: Add your control notification handler code here
UpdateData(TRUE); //读取编辑框内容
m_ctrlComm.SetOutput(COleVariant(m_strTXData));//发送数据

}
应该是接收那里出现的问题,是碰到了无法识别的字符吗?

解决方案 »

  1.   

    software flow control是不是打开了?
      

  2.   

    你好像少了这句话.
    m_ctrlComm.SetInputMode(1); //1:表示以二进制方式检取数据
      

  3.   

    software flow control在哪里打开的?
    好像没有打开啊
    有时候又不会出现这个错误,好烦恼啊m_ctrlComm.SetInputMode(1); //1:表示以二进制方式检取数据
    这句话我没有加,不知道加了会不会消除这个现象
    EY3:你说的没这句话就会出错是指不能编译成功吗?
        还是程序接受到某些字符会出错?
      

  4.   

    我没这句话好像可以运行,不过加了这句话以后系统好像稳定一些了
    希望不要再down掉
    给分了,呵呵