vc版的风机模拟代码,备忘

来源:互联网 发布:windows无法连接到wifi 编辑:程序博客网 时间:2024/06/09 22:44
BOOL Dialog1::OnInitDialog() {CDialog::OnInitDialog();// TODO: Add extra initialization herem_hIconRed=AfxGetApp()->LoadIcon(IDI_ICON_ON);//iconm_hIconOff=AfxGetApp()->LoadIcon(IDI_ICON_NOT);m_hIconErr=AfxGetApp()->LoadIcon(IDI_ICON_ERR);m_ctrlComboCom.SetCurSel(0);//com combo initm_ctrlComboSpeed.SetCurSel(0);m_ctrlComboDatabits.SetCurSel(0);m_ctrlComboParity.SetCurSel(0);m_ctrlComboStopbits.SetCurSel(0);m_ctrlComboFan1.SetCurSel(1);//fan combo initm_ctrlComboFan2.SetCurSel(1);m_ctrlComboFan3.SetCurSel(1);m_ctrlComboFan4.SetCurSel(1);m_ctrlComboFan5.SetCurSel(1);m_ctrlComboFan6.SetCurSel(1);m_ctrlComboFan7.SetCurSel(1);m_ctrlComboFan8.SetCurSel(1);m_ctrlComboFan9.SetCurSel(1);m_ctrlComboFan10.SetCurSel(1);m_ctrlComboFan11.SetCurSel(1);m_ctrlComboFan12.SetCurSel(1);m_ctrlComboFan13.SetCurSel(1);m_ctrlComboFan14.SetCurSel(1);//radio init  m_nBaud=9600;//com initm_nCom=1;m_cParity='N';m_nDatabits=8;m_nStopbits=1;m_dwCommEvents = EV_RXFLAG | EV_RXCHAR;//if (m_Port.InitPort(this, 1, 9600,'N',8,1,dwCommEvents,512))CString strStatus;if (m_Port.InitPort(this, m_nCom, m_nBaud,m_cParity,m_nDatabits,m_nStopbits,m_dwCommEvents,512)){m_Port.StartMonitoring();strStatus.Format("STATUS:COM%d OPENED,%d,%c,%d,%d",m_nCom, m_nBaud,m_cParity,m_nDatabits,m_nStopbits);m_ctrlIconOpenoff.SetIcon(m_hIconRed);//m_ctrlIconOpenoff.SetIcon(m_hIconOff);//"当前状态:串口打开,无奇偶校验,8数据位,1停止位");}else{AfxMessageBox("没有发现此串口");m_ctrlIconOpenoff.SetIcon(m_hIconOff);}m_ctrlPortStatus.SetWindowText(strStatus);return TRUE;  // return TRUE unless you set the focus to a control// EXCEPTION: OCX Property Pages should return FALSE}void Dialog1::OnButtonOpenport() {// TODO: Add your control notification handler code herem_bOpenPort=!m_bOpenPort;if(m_bOpenPort)  //关闭串口{m_ctrlOpenPort.SetWindowText("打开串口");m_Port.ClosePort();//关闭串口m_ctrlPortStatus.SetWindowText("STATUS:COM Port Closed");m_ctrlIconOpenoff.SetIcon(m_hIconOff);}else  //打开串口{m_ctrlOpenPort.SetWindowText("关闭串口");CString strStatus;if (m_Port.InitPort(this, m_nCom, m_nBaud,m_cParity,m_nDatabits,m_nStopbits,m_dwCommEvents,512)){m_Port.StartMonitoring();m_ctrlIconOpenoff.SetIcon(m_hIconRed);strStatus.Format("STATUS:COM%d OPENED,%d,%c,%d,%d",m_nCom, m_nBaud,m_cParity,m_nDatabits,m_nStopbits);//"当前状态:串口打开,无奇偶校验,8数据位,1停止位");}else{AfxMessageBox("没有发现此串口或被占用");m_ctrlIconOpenoff.SetIcon(m_hIconOff);}m_ctrlPortStatus.SetWindowText(strStatus);}}void Dialog1::OnSelchangeComboFan1() {// TODO: Add your control notification handler code hereint i=m_ctrlComboFan1.GetCurSel();switch (i){case 0:GetDlgItem(IDC_ANIGIF_FAN1)->ShowWindow(SW_SHOW);m_ctrlAniGifFan1.SetPlaying(TRUE);m_ctrlIcon1.SetIcon(m_hIconRed);break;case 1:GetDlgItem(IDC_ANIGIF_FAN1)->ShowWindow(SW_SHOW);m_ctrlAniGifFan1.SetPlaying(FALSE);m_ctrlIcon1.SetIcon(m_hIconOff);break;case 2:GetDlgItem(IDC_ANIGIF_FAN1)->ShowWindow(SW_HIDE);m_ctrlIcon1.SetIcon(m_hIconErr);break;}}void Dialog1::OnSelchangeComboFan2() {// TODO: Add your control notification handler code hereint i=m_ctrlComboFan2.GetCurSel();switch (i){case 0:GetDlgItem(IDC_ANIGIF_FAN2)->ShowWindow(SW_SHOW);m_ctrlAniGifFan2.SetPlaying(TRUE);m_ctrlIcon2.SetIcon(m_hIconRed);break;case 1:GetDlgItem(IDC_ANIGIF_FAN2)->ShowWindow(SW_SHOW);m_ctrlAniGifFan2.SetPlaying(FALSE);m_ctrlIcon2.SetIcon(m_hIconOff);break;case 2:GetDlgItem(IDC_ANIGIF_FAN2)->ShowWindow(SW_HIDE);m_ctrlIcon2.SetIcon(m_hIconErr);break;}}void Dialog1::OnSelchangeComboFan3() {// TODO: Add your control notification handler code hereint i=m_ctrlComboFan3.GetCurSel();switch (i){case 0:GetDlgItem(IDC_ANIGIF_FAN3)->ShowWindow(SW_SHOW);m_ctrlAniGifFan3.SetPlaying(TRUE);m_ctrlIcon3.SetIcon(m_hIconRed);break;case 1:GetDlgItem(IDC_ANIGIF_FAN3)->ShowWindow(SW_SHOW);m_ctrlAniGifFan3.SetPlaying(FALSE);m_ctrlIcon3.SetIcon(m_hIconOff);break;case 2:GetDlgItem(IDC_ANIGIF_FAN3)->ShowWindow(SW_HIDE);m_ctrlIcon3.SetIcon(m_hIconErr);break;}}void Dialog1::OnSelchangeComboFan4() {// TODO: Add your control notification handler code hereint i=m_ctrlComboFan4.GetCurSel();switch (i){case 0:GetDlgItem(IDC_ANIGIF_FAN4)->ShowWindow(SW_SHOW);m_ctrlAniGifFan4.SetPlaying(TRUE);m_ctrlIcon4.SetIcon(m_hIconRed);break;case 1:GetDlgItem(IDC_ANIGIF_FAN4)->ShowWindow(SW_SHOW);m_ctrlAniGifFan4.SetPlaying(FALSE);m_ctrlIcon4.SetIcon(m_hIconOff);break;case 2:GetDlgItem(IDC_ANIGIF_FAN4)->ShowWindow(SW_HIDE);m_ctrlIcon4.SetIcon(m_hIconErr);break;}}void Dialog1::OnSelchangeComboFan5() {// TODO: Add your control notification handler code hereint i=m_ctrlComboFan5.GetCurSel();switch (i){case 0:GetDlgItem(IDC_ANIGIF_FAN5)->ShowWindow(SW_SHOW);m_ctrlAniGifFan5.SetPlaying(TRUE);m_ctrlIcon5.SetIcon(m_hIconRed);break;case 1:GetDlgItem(IDC_ANIGIF_FAN5)->ShowWindow(SW_SHOW);m_ctrlAniGifFan5.SetPlaying(FALSE);m_ctrlIcon5.SetIcon(m_hIconOff);break;case 2:GetDlgItem(IDC_ANIGIF_FAN5)->ShowWindow(SW_HIDE);m_ctrlIcon5.SetIcon(m_hIconErr);break;}}void Dialog1::OnSelchangeComboFan6() {// TODO: Add your control notification handler code hereint i=m_ctrlComboFan6.GetCurSel();switch (i){case 0:GetDlgItem(IDC_ANIGIF_FAN6)->ShowWindow(SW_SHOW);m_ctrlAniGifFan6.SetPlaying(TRUE);m_ctrlIcon6.SetIcon(m_hIconRed);break;case 1:GetDlgItem(IDC_ANIGIF_FAN6)->ShowWindow(SW_SHOW);m_ctrlAniGifFan6.SetPlaying(FALSE);m_ctrlIcon6.SetIcon(m_hIconOff);break;case 2:GetDlgItem(IDC_ANIGIF_FAN6)->ShowWindow(SW_HIDE);m_ctrlIcon6.SetIcon(m_hIconErr);break;}}void Dialog1::OnSelchangeComboFan7() {// TODO: Add your control notification handler code hereint i=m_ctrlComboFan7.GetCurSel();switch (i){case 0:GetDlgItem(IDC_ANIGIF_FAN7)->ShowWindow(SW_SHOW);m_ctrlAniGifFan7.SetPlaying(TRUE);m_ctrlIcon7.SetIcon(m_hIconRed);break;case 1:GetDlgItem(IDC_ANIGIF_FAN7)->ShowWindow(SW_SHOW);m_ctrlAniGifFan7.SetPlaying(FALSE);m_ctrlIcon7.SetIcon(m_hIconOff);break;case 2:GetDlgItem(IDC_ANIGIF_FAN7)->ShowWindow(SW_HIDE);m_ctrlIcon7.SetIcon(m_hIconErr);break;}}void Dialog1::OnSelchangeComboFan8() {// TODO: Add your control notification handler code hereint i=m_ctrlComboFan8.GetCurSel();switch (i){case 0:GetDlgItem(IDC_ANIGIF_FAN8)->ShowWindow(SW_SHOW);m_ctrlAniGifFan8.SetPlaying(TRUE);m_ctrlIcon8.SetIcon(m_hIconRed);break;case 1:GetDlgItem(IDC_ANIGIF_FAN8)->ShowWindow(SW_SHOW);m_ctrlAniGifFan8.SetPlaying(FALSE);m_ctrlIcon8.SetIcon(m_hIconOff);break;case 2:GetDlgItem(IDC_ANIGIF_FAN8)->ShowWindow(SW_HIDE);m_ctrlIcon8.SetIcon(m_hIconErr);break;}}void Dialog1::OnSelchangeComboFan9() {// TODO: Add your control notification handler code hereint i=m_ctrlComboFan9.GetCurSel();switch (i){case 0:GetDlgItem(IDC_ANIGIF_FAN9)->ShowWindow(SW_SHOW);m_ctrlAniGifFan9.SetPlaying(TRUE);m_ctrlIcon9.SetIcon(m_hIconRed);break;case 1:GetDlgItem(IDC_ANIGIF_FAN9)->ShowWindow(SW_SHOW);m_ctrlAniGifFan9.SetPlaying(FALSE);m_ctrlIcon9.SetIcon(m_hIconOff);break;case 2:GetDlgItem(IDC_ANIGIF_FAN9)->ShowWindow(SW_HIDE);m_ctrlIcon9.SetIcon(m_hIconErr);break;}}void Dialog1::OnSelchangeComboFan10() {// TODO: Add your control notification handler code hereint i=m_ctrlComboFan10.GetCurSel();switch (i){case 0:GetDlgItem(IDC_ANIGIF_FAN10)->ShowWindow(SW_SHOW);m_ctrlAniGifFan10.SetPlaying(TRUE);m_ctrlIcon10.SetIcon(m_hIconRed);break;case 1:GetDlgItem(IDC_ANIGIF_FAN10)->ShowWindow(SW_SHOW);m_ctrlAniGifFan10.SetPlaying(FALSE);m_ctrlIcon10.SetIcon(m_hIconOff);break;case 2:GetDlgItem(IDC_ANIGIF_FAN10)->ShowWindow(SW_HIDE);m_ctrlIcon10.SetIcon(m_hIconErr);break;}}void Dialog1::OnSelchangeComboFan11() {// TODO: Add your control notification handler code hereint i=m_ctrlComboFan11.GetCurSel();switch (i){case 0:GetDlgItem(IDC_ANIGIF_FAN11)->ShowWindow(SW_SHOW);m_ctrlAniGifFan11.SetPlaying(TRUE);m_ctrlIcon11.SetIcon(m_hIconRed);break;case 1:GetDlgItem(IDC_ANIGIF_FAN11)->ShowWindow(SW_SHOW);m_ctrlAniGifFan11.SetPlaying(FALSE);m_ctrlIcon11.SetIcon(m_hIconOff);break;case 2:GetDlgItem(IDC_ANIGIF_FAN11)->ShowWindow(SW_HIDE);m_ctrlIcon11.SetIcon(m_hIconErr);break;}}void Dialog1::OnSelchangeComboFan12() {// TODO: Add your control notification handler code hereint i=m_ctrlComboFan12.GetCurSel();switch (i){case 0:GetDlgItem(IDC_ANIGIF_FAN12)->ShowWindow(SW_SHOW);m_ctrlAniGifFan12.SetPlaying(TRUE);m_ctrlIcon12.SetIcon(m_hIconRed);break;case 1:GetDlgItem(IDC_ANIGIF_FAN12)->ShowWindow(SW_SHOW);m_ctrlAniGifFan12.SetPlaying(FALSE);m_ctrlIcon12.SetIcon(m_hIconOff);break;case 2:GetDlgItem(IDC_ANIGIF_FAN12)->ShowWindow(SW_HIDE);m_ctrlIcon12.SetIcon(m_hIconErr);break;}}void Dialog1::OnSelchangeComboFan13() {// TODO: Add your control notification handler code hereint i=m_ctrlComboFan13.GetCurSel();switch (i){case 0:GetDlgItem(IDC_ANIGIF_FAN13)->ShowWindow(SW_SHOW);m_ctrlAniGifFan13.SetPlaying(TRUE);m_ctrlIcon13.SetIcon(m_hIconRed);break;case 1:GetDlgItem(IDC_ANIGIF_FAN13)->ShowWindow(SW_SHOW);m_ctrlAniGifFan13.SetPlaying(FALSE);m_ctrlIcon13.SetIcon(m_hIconOff);break;case 2:GetDlgItem(IDC_ANIGIF_FAN13)->ShowWindow(SW_HIDE);m_ctrlIcon13.SetIcon(m_hIconErr);break;}}void Dialog1::OnSelchangeComboFan14() {// TODO: Add your control notification handler code hereint i=m_ctrlComboFan14.GetCurSel();switch (i){case 0:GetDlgItem(IDC_ANIGIF_FAN14)->ShowWindow(SW_SHOW);m_ctrlAniGifFan14.SetPlaying(TRUE);m_ctrlIcon14.SetIcon(m_hIconRed);break;case 1:GetDlgItem(IDC_ANIGIF_FAN14)->ShowWindow(SW_SHOW);m_ctrlAniGifFan14.SetPlaying(FALSE);m_ctrlIcon14.SetIcon(m_hIconOff);break;case 2:GetDlgItem(IDC_ANIGIF_FAN14)->ShowWindow(SW_HIDE);m_ctrlIcon14.SetIcon(m_hIconErr);break;}}char Dialog1::HexChar(char c){if((c>='0')&&(c<='9'))return c-0x30;else if((c>='A')&&(c<='F'))return c-'A'+10;else if((c>='a')&&(c<='f'))return c-'a'+10;else return 0x10;}//将一个字符串作为十六进制串转化为一个字节数组,字节间可用空格分隔,//返回转换后的字节数组长度,同时字节数组长度自动设置。int Dialog1::Str2Hex(CString str, char* data){int t,t1;int rlen=0,len=str.GetLength();//data.SetSize(len/2);for(int i=0;i<len;){char l,h=str[i];if(h==' '){i++;continue;}i++;if(i>=len)break;l=str[i];t=HexChar(h);t1=HexChar(l);if((t==16)||(t1==16))break;else t=t*16+t1;i++;data[rlen]=(char)t;rlen++;}return rlen;}LONG Dialog1::OnCommunication(WPARAM ch, LPARAM port){if (m_bTimer==TRUE){SetTimer(1,5,NULL);m_bTimer=FALSE;}CString str;str.Format("%02x ",ch);m_strPortRXData+=str;return 0;}void Dialog1::OnTimer(UINT nIDEvent) {// TODO: Add your message handler code here and/or call defaultif (nIDEvent==1){char data[512];BYTE rxdata[512];CString str,strTemp;unsigned short c1,c2;int len=Str2Hex(m_strPortRXData,data);for (LONG k=0;k<len;k++){rxdata[k]=data[k];}c1=CRC16(rxdata,len-2);c2=rxdata[len-1]|(rxdata[len-2]<<8);if (c1=c2){if (rxdata[0]==0x01){switch(rxdata[1]){case 0x10:{switch(rxdata[8]){case 0x05:{//alloffEven(FALSE);Odd(FALSE);}break;case 0x06:{Even(FALSE);Odd(TRUE);//oddon}break;case 0x09:{//evenonEven(TRUE);Odd(FALSE);}break;case 0x0a:{//allonEven(TRUE);Odd(TRUE);}break;}c1=CRC16(rxdata,len-5);rxdata[len-5]=c1>>8;rxdata[len-4]=c1&0xff;for (LONG k=0;k<len-3;k++){data[k]=rxdata[k];str.Format("%02x ",rxdata[k]);strTemp+=str;}m_Port.WriteToPort(data,len-3);m_List1.AddString("接收:"+m_strPortRXData);m_List1.AddString("发送:"+strTemp);}break;case 0x03:{rxdata[2]=0x06;//通讯字节数为6UpdateData(TRUE);rxdata[3]=((m_nRadioDelay==0)?0:((m_nRadioDelay==2)?1:0))|(((m_ctrlComboFan1.GetCurSel()==2)?1:0)<<1)|(((m_ctrlComboFan2.GetCurSel()==2)?1:0)<<2)|(((m_ctrlComboFan3.GetCurSel()==2)?1:0)<<3)|(((m_ctrlComboFan4.GetCurSel()==2)?1:0)<<4)|(((m_ctrlComboFan5.GetCurSel()==2)?1:0)<<5)|(((m_ctrlComboFan6.GetCurSel()==2)?1:0)<<6)|(((m_ctrlComboFan7.GetCurSel()==2)?1:0)<<7);rxdata[4]=(m_nRadioPower1)|(m_nRadioPower2<<1)|(m_nRadioPowerErr1<<2)|(m_nRadioPowerErr2<<3)|(m_nRadioCooler<<4)|(m_nRadioCooleroff<<5)|(m_nRadioTrigear<<6)|(((m_nRadioDelay==0)?0:((m_nRadioDelay==1)?1:0))<<7);rxdata[5]=(m_ctrlAniGifFan2.GetPlaying()?1:0)|((m_ctrlAniGifFan3.GetPlaying()?1:0)<<1)|((m_ctrlAniGifFan4.GetPlaying()?1:0)<<2)|((m_ctrlAniGifFan5.GetPlaying()?1:0)<<3)|((m_ctrlAniGifFan6.GetPlaying()?1:0)<<4)|((m_ctrlAniGifFan7.GetPlaying()?1:0)<<5)|((m_ctrlAniGifFan8.GetPlaying()?1:0)<<6)|((m_ctrlAniGifFan9.GetPlaying()?1:0)<<7);rxdata[6]=((m_ctrlComboFan8.GetCurSel()==2)?1:0)|(((m_ctrlComboFan9.GetCurSel()==2)?1:0)<<1)|(((m_ctrlComboFan10.GetCurSel()==2)?1:0)<<2)|(((m_ctrlComboFan11.GetCurSel()==2)?1:0)<<3)|(((m_ctrlComboFan12.GetCurSel()==2)?1:0)<<4)|(((m_ctrlComboFan13.GetCurSel()==2)?1:0)<<5)|(((m_ctrlComboFan14.GetCurSel()==2)?1:0)<<6)|((m_ctrlAniGifFan1.GetPlaying()?1:0)<<7);rxdata[7]=(m_nRadioPos?0:1)|((m_nRadioOdd?0:1)<<1)|((m_nRadioOdd?1:0)<<2)|((m_nRadioEven?0:1)<<3)|((m_nRadioEven?1:0)<<4);rxdata[8]=(m_ctrlAniGifFan10.GetPlaying()?1:0)|((m_ctrlAniGifFan11.GetPlaying()?1:0)<<1)|((m_ctrlAniGifFan12.GetPlaying()?1:0)<<2)|((m_ctrlAniGifFan13.GetPlaying()?1:0)<<3)|((m_ctrlAniGifFan14.GetPlaying()?1:0)<<4)|((m_nRadioMode?1:0)<<5)|((m_nRadioMode?0:1)<<6)|((m_nRadioPos?1:0)<<7);c1=CRC16(rxdata,9);rxdata[9]=c1>>8;rxdata[10]=c1&0xff;for (LONG k=0;k<11;k++){data[k]=rxdata[k];str.Format("%02x ",rxdata[k]);strTemp+=str;}m_Port.WriteToPort(data,11);m_List1.AddString("接收:"+m_strPortRXData);m_List1.AddString("发送:"+strTemp);}break;}}KillTimer(1);m_bTimer=TRUE;m_strPortRXData="";}}CDialog::OnTimer(nIDEvent);}void Dialog1::Even(BOOL b){m_ctrlAniGifFan2.SetPlaying(b);m_ctrlAniGifFan4.SetPlaying(b);m_ctrlAniGifFan6.SetPlaying(b);m_ctrlAniGifFan8.SetPlaying(b);m_ctrlAniGifFan10.SetPlaying(b);m_ctrlAniGifFan12.SetPlaying(b);m_ctrlAniGifFan14.SetPlaying(b);m_ctrlComboFan2.SetCurSel(!b);m_ctrlComboFan4.SetCurSel(!b);m_ctrlComboFan6.SetCurSel(!b);m_ctrlComboFan8.SetCurSel(!b);m_ctrlComboFan10.SetCurSel(!b);m_ctrlComboFan12.SetCurSel(!b);m_ctrlComboFan14.SetCurSel(!b);}void Dialog1::Odd(BOOL b){m_ctrlAniGifFan1.SetPlaying(b);m_ctrlAniGifFan3.SetPlaying(b);m_ctrlAniGifFan5.SetPlaying(b);m_ctrlAniGifFan7.SetPlaying(b);m_ctrlAniGifFan9.SetPlaying(b);m_ctrlAniGifFan11.SetPlaying(b);m_ctrlAniGifFan13.SetPlaying(b);m_ctrlComboFan1.SetCurSel(!b);m_ctrlComboFan3.SetCurSel(!b);m_ctrlComboFan5.SetCurSel(!b);m_ctrlComboFan7.SetCurSel(!b);m_ctrlComboFan9.SetCurSel(!b);m_ctrlComboFan11.SetCurSel(!b);m_ctrlComboFan13.SetCurSel(!b);}void Dialog1::OnButtonClear() {// TODO: Add your control notification handler code herem_List1.ResetContent();}