原创 vc++ 串口开发代码

2009-10-8 12:52 4606 5 5 分类: 软件与OS

应用程序中串口相关代码摘录如下:


<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

 


void CMotorCtl::OnOpenPort()    //应用程序界面上打开串口按键的响应函数


{


       // TODO: Add your control notification handler code here


             


              if(m_Com.GetPortOpen())        //若串口是打开的


              {


                     m_Com.SetPortOpen(FALSE);      //关闭串口


 


                     m_EditPrompt="串口已经关闭 ! ";  // 状态显示


                     SetDlgItemText(IDC_EDIT_PROMPT,m_EditPrompt); //显示串口已经关闭


                    


                     CButton  *pBUTTON=(CButton*)GetDlgItem(IDC_BUTTON_OPENPORT);  //改变按钮caption为“打开串口”


                     pBUTTON->SetWindowText("打开串口");    //setwindowtext函数改变按钮的caption. 


                    


                     CButton  *pLBUTTON=(CButton*)GetDlgItem(IDC_BUTTON_LINKPORT);  //连接按钮失能


                     pLBUTTON->EnableWindow(FALSE);


                           


              }


      


              else   //如果串口是关闭的


              {  


                     m_PortFlag=m_Port.GetCurSel()+1;   //设置端口


                     m_Com.SetCommPort(m_PortFlag); 


                     m_Com.SetInBufferSize(1024);  //接收缓冲区(默认值:1024字节)


                     m_Com.SetOutBufferSize(512); //发送缓冲区(默认值:512字节)


 


                     m_Com.SetSettings("9600,n,8,1"); //设置串口参数


                     m_Com.SetInputMode(1); //以二进制方式检到数据


                     m_Com.SetRThreshold(8); //(默认值为0:接受字符不产生OnComm事件)参数8表示当串口接收缓冲区中有多于8个的字符时,引发OnComm事件


                     m_Com.SetSThreshold(0); //(默认值为0:发送字符不产生OnComm事件)


                     m_Com.SetInputLen(8);   //每次读操作从缓冲区中取8个字符(默认值0:读接受缓冲区全部内容)


                  


                     m_Com.SetInBufferCount(0); //清空接收缓冲区


                     m_Com.SetOutBufferCount(0); //清除发送缓冲区


                    


 


                     m_Com.SetPortOpen(TRUE);//打开串口


 


                     m_EditPrompt="串口已经打开 ! ";  //状态显示


               SetDlgItemText(IDC_EDIT_PROMPT,m_EditPrompt); //显示串口已经打开


 


                     CButton  *pBUTTON=(CButton*)GetDlgItem(IDC_BUTTON_OPENPORT);  //改变按钮caption为“关闭串口”


                     pBUTTON->SetWindowText("关闭串口");    //setwindowtext函数改变按钮的caption. 


             


                     CButton  *pLBUTTON=(CButton*)GetDlgItem(IDC_BUTTON_LINKPORT);  //连接按钮使能


                     pLBUTTON->EnableWindow(TRUE);


                                  


              }


 


}


 


 


void CMotorCtl::SendComplexCmd(BYTE command, CString parameter) //多字符串口发送函数


{


       //command:命令(单字符)parameter:参数(1-4个有效字符)


 


       unsigned char n;


       n = parameter.GetLength();//字符串长度(不包括结尾符‘\<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />0


 


       BYTE para[5];        // para[4]正好,不过定义大于4也无所谓,后面代码没取5


       for(int i = 0; i < n; i ++)


              para = parameter.GetAt(i);//将字符转换为BYTE型量(字符对应的ASCII)


 


       CByteArray array;


       array.RemoveAll();


       array.SetSize(n+3);//命令的总长度


       int j="0";


       array.SetAt(j++,'@');//添加命令起始符


       array.SetAt(j++,command);//加入命令位


             


       for(i=0;i<n;i++)//加入参数位                    


              array.SetAt(j+i, para);


 


       array.SetAt(j+n,'$');//加入结束符


      


       m_Com.SetOutput(COleVariant(array)); // 发送数据


}


 


void CMotorCtl::SendReducedCmd(BYTE command)         //单字符串口发送函数


{


       CByteArray array;


       array.RemoveAll();


       array.SetSize(1);


       array.SetAt(0, command);    


       m_Com.SetOutput(COleVariant(array)); // 发送数据


}


 


void CMotorCtl::OnOnCommMscomm1() //对下位机发送上来的字符命令进行解析


{


       // TODO: Add your control notification handler code here


       VARIANT variant_inp;


       COleSafeArray safearray_inp;


       long len,k;     


       unsigned char receive_data[7];  //存储转换为char型的数据


       BYTE rxdata[100]; //设置BYTE数组,应该足够大.


   


       if(m_Com.GetCommEvent()==2)     //事件值为2表示接收缓冲区内有字符


       {           


              variant_inp=m_Com.GetInput();   //读缓冲区,一次读取8个字节


              m_Com.SetInBufferCount(0); //清空接收缓冲区


              safearray_inp=variant_inp;           //VARIANT型变量转换为ColeSafeArray型变量


              len=safearray_inp.GetOneDimSize(); //得到有效数据长度


              for(k=0;k<len;k++)


                     safearray_inp.GetElement(&k,rxdata+k);//转换为BYTE型数组数据(存入rxdata数组)


             


           for(k=0;k<len;k++)


              {


                     receive_data[k]=*((unsigned char*)(rxdata+k));   //依据rxdata数组内容,为receive_data赋值(char型)


              }


             


              for(k=0;k<len;k++)


              {


                     char c;


                     c=receive_data[k];


 


                       if(c == '@')//命令字开始


                            {


                                   CPublicData::cmd_length = 0;


                            }


                       else if(c == '$')//命令字结束


                            {


                                   decode();//解码函数,主要针对CPublicData::command[0]的数据解码


                            }


                       else if((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z')||(c=='%'))


                            {


                                   CPublicData::command[CPublicData::cmd_length] = c; //指令字符放入命令缓存


                                   CPublicData::cmd_length++; 


                            }    


                           


                       else


                            {


                                                        //空语句


                            }


                     }


       }


}

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
我要评论
0
5
关闭 站长推荐上一条 /3 下一条