您好,欢迎光临本网站![请登录][注册会员]  
文件名称: 用多线程做自动机程序
  所属分类: 硬件开发
  开发工具:
  文件大小: 18kb
  下载次数: 0
  上传时间: 2010-12-24
  提 供 者: xujx*****
 详细说明: // Auto.cpp : implementation file // #include "stdafx.h" #include "xs.h" #include "Auto.h" #include "Parameter.h" #include "gep.h" #include "Public.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CAuto dialog CAuto::CAuto(CWnd* pParent /*=NULL*/) : CDialog(CAuto::IDD, pParent) { //{{AFX_DATA_INIT(CAuto) m_Warn = _T(""); m _Sys_Time=_T(""); m_PosX = _T(""); m_PosY = _T(""); //}}AFX_DATA_INIT m_LedOn.LoadBitmap(IDB_LED_ON); m_LedOff.LoadBitmap(IDB_LED_OFF); } void CAuto::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAuto) DDX_Text(pDX, IDC_ARM, m_Warn); DDX_Text(pDX, IDC_SYS_TIME, m_Sys_Time); DDX_Control(pDX, IDC_LED_HOME0, m_Led_Home[0]); DDX_Control(pDX, IDC_LED_HOME1, m_Led_Home[1]); DDX_Control(pDX, IDC_LED_Z, m_Led_Home[2]); DDX_Control(pDX, IDC_LED_U, m_Led_Home[3]); DDX_Control(pDX, IDC_LED_OUT4, m_Led_Out[4]); DDX_Control(pDX, IDC_LED_OUT3, m_Led_Out[3]); DDX_Control(pDX, IDC_LED_OUT2, m_Led_Out[2]); DDX_Control(pDX, IDC_LED_OUT1, m_Led_Out[1]); DDX_Control(pDX, IDC_LED_OUT0, m_Led_Out[0]); DDX_Control(pDX, IDC_LED_LIMIT3, m_Led_Limit[3]); DDX_Control(pDX, IDC_LED_LIMIT2, m_Led_Limit[2]); DDX_Control(pDX, IDC_LED_LIMIT1, m_Led_Limit[1]); DDX_Control(pDX, IDC_LED_LIMIT0, m_Led_Limit[0]); DDX_Control(pDX, IDC_LED_IN13, m_Led_In[13]); DDX_Control(pDX, IDC_LED_IN12, m_Led_In[12]); DDX_Control(pDX, IDC_LED_IN11, m_Led_In[11]); DDX_Control(pDX, IDC_LED_IN10, m_Led_In[10]); DDX_Control(pDX, IDC_LED_IN9, m_Led_In[9]); DDX_Control(pDX, IDC_LED_IN8, m_Led_In[8]); DDX_Control(pDX, IDC_LED_IN7, m_Led_In[7]); DDX_Control(pDX, IDC_LED_IN6, m_Led_In[6]); DDX_Control(pDX, IDC_LED_IN5, m_Led_In[5]); DDX_Control(pDX, IDC_LED_IN4, m_Led_In[4]); DDX_Control(pDX, IDC_LED_IN3, m_Led_In[3]); DDX_Control(pDX, IDC_LED_IN2, m_Led_In[2]); DDX_Control(pDX, IDC_LED_IN1, m_Led_In[1]); DDX_Control(pDX, IDC_LED_IN0, m_Led_In[0]); /* DDX_Control(pDX, IDC_LED_HOME0, m_Led_Home0); DDX_Control(pDX, IDC_LED_HOME1, m_Led_Home1); DDX_Control(pDX, IDC_LED_Z, m_Led_Home2); DDX_Control(pDX, IDC_LED_U, m_Led_Home3); DDX_Control(pDX, IDC_LED_OUT4, m_Led_Out4); DDX_Control(pDX, IDC_LED_OUT3, m_Led_Out3); DDX_Control(pDX, IDC_LED_OUT2, m_Led_Out2); DDX_Control(pDX, IDC_LED_OUT1, m_Led_Out1); DDX_Control(pDX, IDC_LED_OUT0, m_Led_Out0); DDX_Control(pDX, IDC_LED_LIMIT3, m_Led_Limit3); DDX_Control(pDX, IDC_LED_LIMIT2, m_Led_Limit2); DDX_Control(pDX, IDC_LED_LIMIT1, m_Led_Limit1); DDX_Control(pDX, IDC_LED_LIMIT0, m_Led_Limit0); DDX_Control(pDX, IDC_LED_IN9, m_Led_In9); DDX_Control(pDX, IDC_LED_IN8, m_Led_In8); DDX_Control(pDX, IDC_LED_IN7, m_Led_In7); DDX_Control(pDX, IDC_LED_IN6, m_Led_In6); DDX_Control(pDX, IDC_LED_IN5, m_Led_In5); DDX_Control(pDX, IDC_LED_IN4, m_Led_In4); DDX_Control(pDX, IDC_LED_IN3, m_Led_In3); DDX_Control(pDX, IDC_LED_IN2, m_Led_In2); DDX_Control(pDX, IDC_LED_IN13, m_Led_In13); DDX_Control(pDX, IDC_LED_IN12, m_Led_In12); DDX_Control(pDX, IDC_LED_IN11, m_Led_In11); DDX_Control(pDX, IDC_LED_IN10, m_Led_In10); DDX_Control(pDX, IDC_LED_IN1, m_Led_In1); DDX_Control(pDX, IDC_LED_IN0, m_Led_In0); */ DDX_Text(pDX, IDC_STC_AXIS_X, m_PosX); DDX_Text(pDX, IDC_STC_AXIS_Y, m_PosY); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAuto, CDialog) //{{AFX_MSG_MAP(CAuto) ON_BN_CLICKED(IDC_RUN, OnAutoRun) ON_BN_CLICKED(IDC_STOP, OnAutoRun) ON_BN_CLICKED(IDC_P, OnAutoRun) ON_BN_CLICKED(IDC_RESET, OnAutoRun) ON_WM_TIMER() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CAuto message handlers BOOL CAuto::OnInitDialog() { CDialog::OnInitDialog(); ZT=TRUE; bReset =FALSE; SetTimer(1,10,NULL); SetTimer(2,10,NULL); // void Init_MotionMsg(void); // bool MachineHomeFun(); // TODO: Add extra initialization here return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } void CAuto::OnAutoRun() { int nID = (UINT)LOWORD(GetCurrentMessage()->wParam); int i = 0; static bool ct1=0,ct2=0,ct3=0,ct4=0,ct5=0,ct6=0,ct7=0,ct8=0,ct9=0,ct10=0; short rtn=-1; short bit1; unsigned short exInpt=0xffff; MoveMode=1; switch(nID) { UpdateData(1); case IDC_RUN://运行 ///////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////// if( !bReset) { MessageBox(AlmMessage[3],"警告");// SetDlgItemText(IDC_ARM,AlmMessage[3]); return ; } switch(g_MotionMsg.MoveState) { case 0: // ::GetLocalTime(&g_MotionMsg.RunTime);//初始化启动时间 g_MotionMsg.MoveState = 1;//运行状态 g_MotionMsg.MoveMode = 3;//自动模式 // SetDlgItemText(IDC_ARM,AlmMessage[0]); // for(;;) // { AutoMoveFun(); // SetTimer(1,10,NULL); // SetTimer(2,10,NULL); // if(g_MotionMsg.MoveState==2)break; // } // SetDlgItemText(IDC_ARM,AlmMessage[1]); g_HWorkThread = CreateThread(NULL,0,WorkLedThread,NULL,0,NULL); CloseHandle(WorkLedThread); // g_HRecvThread = CreateThread(NULL,0,RecvLedThread,NULL,0,NULL); // CloseHandle(WRecvLedThread); //AfxBeginThread(WorkLedThread,NULL,THREAD_PRIORITY_NORMAL); // hMutex=CreateMutex(NULL,FALSE,NULL); InitializeCriticalSection(&g_cs); // ReleaseMutex(hMutex); // AfxBeginThread(WorkLedThread,NULL); // Sleep(1); // DeleteCriticalSection(&g_cs); break; case 1: MessageBox(AlmMessage[0],"警告信息"); break; case 2: MessageBox("请检查复位开关","警告信息"); SetDlgItemText(IDC_ARM,"请先检查复位旋钮开关,按“停止”—>“复位”—>“启动”继续..."); break; } ////////////////////////////////////////////////////////// break; case IDC_STOP://停止 if( g_MotionMsg.MoveState == 0 ) { MessageBox(AlmMessage[1],"警告信息"); return; } SetDlgItemText(IDC_ARM,AlmMessage[1]); // TerminateThread(g_HSendThread,i); // TerminateThread(g_HWorkThread,i); // TerminateThread(g_HRecvThread,i);//终止三个辅助线程 g_MotionMsg.MoveState = 0; //停止状态 g_MotionMsg.MoveMode = 0; //自然模式 // g_MotionMsg.AddTime = CurTime; //附加上以前加工的时间 if( !bReset ) bReset = FALSE; //停止后要硬复位 break; case IDC_P://暂停 switch(g_MotionMsg.MoveState) { case 0: MessageBox(AlmMessage[1],"警告信息"); break; case 1: SetDlgItemText(IDC_ARM,AlmMessage[1]); // SuspendThread(g_HSendThread); // SuspendThread(g_HWorkThread); // SuspendThread(g_HRecvThread);//线程挂起 SetDlgItemText(IDC_P,"继 续"); // g_MotionMsg.AddTime = CurTime;//附加上以前加工的时间 g_MotionMsg.MoveState = 2;//暂停状态 break; case 2: SetDlgItemText(IDC_ARM,AlmMessage[0]); // ResumeThread(g_HSendThread); // ResumeThread(g_HWorkThread); // ResumeThread(g_HRecvThread);//线程减挂 SetDlgItemText(IDC_P,"暂 停"); // ::GetLocalTime(&g_MotionMsg.RunTime);//初始化启动时间 g_MotionMsg.MoveState = 1;//运行状态 break; } break; case IDC_RESET: //复位 //////////////////////////////////////////////////////////////////////////////////////// // MachineHomeFun(); ////////////////////////////////////////////////////////////////////////////////////////// if( g_MotionMsg.MoveState == 1 || g_MotionMsg.MoveState == 2 )//如果是运行状态 { MessageBox(AlmMessage[0],"警告信息"); MessageBox(AlmMessage[4],"警告信息"); return; } if(bReset){MessageBox(AlmMessage[11],"警告信息"); return; } /* short rtn=-1; unsigned long status; GT_GetSts(1,&status); if(status&0x2){GT_DrvRst(1); GT_RstSts(1,0xffd);} GT_GetSts(2,&status); if(status&0x2){ GT_DrvRst(2); GT_RstSts(2,0xffd);} */ //GT_RstSts(unsigned short axis,unsigned short mask); // GT_Reset(); // Reset();//进行硬件复位 SetDlgItemText(IDC_ARM,AlmMessage[6]); // g_HHomeThread = CreateThread(NULL,0,HomeThread,NULL,0,NULL); // CloseHandle(HomeThread); MachineHomeFun(); bReset=TRUE; SetDlgItemText(IDC_ARM,"复位完成,可按“启动铵钮”运行机器"); ////////////////////////////////////////////////////////////////////////////////////////////// break; } } void CAuto::OnTimer(UINT nIDEvent) {////////////////////////////////////////////////////////// /////////////运动状态与模式(时时) ////////////////////////////////////////////////////////// if(nIDEvent==1)//界面信息时钟 0 { SYSTEMTIME sysTm; ::GetLocalTime(&sysTm); m_Sys_Time.Format("%02d:%02d:%02d",sysTm.wHour,sysTm.wMinute,sysTm.wSecond); SetDlgItemText(IDC_SYS_TIME,m_Sys_Time);//显示系统时间 ////////////////////////////////////////////////// ////////////工作时间与加工数量(自动模式的运行状态) ////////////////////////////////////////////////// if( g_MotionMsg.MoveMode == 1 && g_MotionMsg.MoveState == 1 )//如果是运动状态并且自动模式 {//工作时间与加工数量计算 CString temp,temp1,temp2; temp1.Format("%08ld",g_MotionMsg.RunPcs); SetDlgItemText(IDC_WORKPCS,temp1);// 加工数量 ::GetLocalTime(&CurTime); //计算加工时间 if (CurTime.wMilliseconds >= g_MotionMsg.RunTime.wMilliseconds) CurTime.wMilliseconds = CurTime.wMilliseconds-g_MotionMsg.RunTime.wMilliseconds; else { CurTime.wSecond = CurTime.wSecond-1; CurTime.wMilliseconds = CurTime.wMilliseconds+1000-g_MotionMsg.RunTime.wMilliseconds; } ///////////////////////// if (CurTime.wSecond >= g_MotionMsg.RunTime.wSecond) CurTime.wSecond = CurTime.wSecond-g_MotionMsg.RunTime.wSecond; else { CurTime.wMinute=CurTime.wMinute-1; CurTime.wSecond=CurTime.wSecond+60-g_MotionMsg.RunTime.wSecond; } ////////////////////// if (CurTime.wMinute >= g_MotionMsg.RunTime.wMinute) CurTime.wMinute = CurTime.wMinute-g_MotionMsg.RunTime.wMinute; else { CurTime.wHour = CurTime.wHour-1; CurTime.wMinute = CurTime.wMinute+60-g_MotionMsg.RunTime.wMinute; } /////////////////////// CurTime.wHour = CurTime.wHour-g_MotionMsg.RunTime.wHour; ////////////////////////// if( (CurTime.wMilliseconds + g_MotionMsg.AddTime.wMilliseconds) >= 1000 ) { CurTime.wSecond++; CurTime.wMilliseconds = CurTime.wMilliseconds + g_MotionMsg.AddTime.wMilliseconds - 1000; } else CurTime.wMilliseconds += g_MotionMsg.AddTime.wMilliseconds; //////////////////////////////// if( (CurTime.wSecond + g_MotionMsg.AddTime.wSecond) >= 60 ) { CurTime.wMinute++; CurTime.wSecond = CurTime.wSecond + g_MotionMsg.AddTime.wSecond - 60; } else CurTime.wSecond += g_MotionMsg.AddTime.wSecond; /////////////////////////////// if( (CurTime.wMinute + g_MotionMsg.AddTime.wMinute) >= 60 ) { CurTime.wHour++; CurTime.wMinute = CurTime.wMinute + g_MotionMsg.AddTime.wMinute - 60; } else CurTime.wMinute += g_MotionMsg.AddTime.wMinute; //////////////////////////////////// CurTime.wHour += g_MotionMsg.AddTime.wHour; temp.Format("%02d:%02d:%02d:%02d",CurTime.wHour,CurTime.wMinute, CurTime.wSecond,CurTime.wMilliseconds); SetDlgItemText(IDC_STC_WORK_TIME,temp);//显示加工时间 temp2=atof(temp1)/atof(temp); SetDlgItemText(IDC_STC_WORK_VEL,temp2); //获取运动状态 CString tempStr; long Pos; //----------------运动数据的显示---------------------------------------// GT_GetAtlPos(1,&Pos); tempStr.Format(_T("%ld"),Pos/250); SetDlgItemText(IDC_STC_AXIS_X,tempStr); GT_Update(1); GT_GetAtlPos(2,&Pos); tempStr.Format(_T("%ld"),Pos/250); SetDlgItemText(IDC_STC_AXIS_Y,tempStr); GT_Update(2); } } else if(nIDEvent==2)//系统信息时钟 1 { unsigned short limit=65535,bit=1; GT_GetLmtSwt(&limit); if(!(limit&bit)) { m_Led_Limit[0].SetBitmap(m_LedOn); } else m_Led_Limit[0].SetBitmap(m_LedOff); bit<<=1; if(!(limit&bit)) { m_Led_Limit[1].SetBitmap(m_LedOn); } else m_Led_Limit[1].SetBitmap(m_LedOff); /////////////////////////////////// bit<<=1; if(!(limit&bit)) { m_Led_Limit[2].SetBitmap(m_LedOn); } else m_Led_Limit[2].SetBitmap(m_LedOff); bit<<=1; if(!(limit&bit)) { m_Led_Limit[3].SetBitmap(m_LedOn); } else m_Led_Limit[3].SetBitmap(m_LedOff); /////////////////////////////////// unsigned short exInpt=65535,bit1=1; GT_ExInpt(&exInpt); if(!(exInpt&bit1)) { m_Led_In[0].SetBitmap(m_LedOn); } else m_Led_In[0].SetBitmap(m_LedOff); bit1<<=1; if(!(exInpt&bit1)) { m_Led_In[1].SetBitmap(m_LedOn); } else m_Led_In[1].SetBitmap(m_LedOff); /////////////////////////////////// bit1<<=1; if(!(exInpt&bit1)) { m_Led_In[2].SetBitmap(m_LedOn); } else m_Led_In[2].SetBitmap(m_LedOff); bit1<<=1; if(!(exInpt&bit1)) { m_Led_In[3].SetBitmap(m_LedOn); } else m_Led_In[3].SetBitmap(m_LedOff); bit1<<=1; if(!(exInpt&bit1)) { m_Led_In[4].SetBitmap(m_LedOn); } else m_Led_In[4].SetBitmap(m_LedOff); bit1<<=1; if(!(exInpt&bit1)) { m_Led_In[5].SetBitmap(m_LedOn); } else m_Led_In[5].SetBitmap(m_LedOff); /////////////////////////////////// bit1<<=1; if(!(exInpt&bit1)) { m_Led_In[6].SetBitmap(m_LedOn); } else m_Led_In[6].SetBitmap(m_LedOff); bit1<<=1; if(!(exInpt&bit1)) { m_Led_In[7].SetBitmap(m_LedOn); } else m_Led_In[7].SetBitmap(m_LedOff); bit1<<=1; if(!(exInpt&bit1)) { m_Led_In[8].SetBitmap(m_LedOn); } else m_Led_In[8].SetBitmap(m_LedOff); bit1<<=1; if(!(exInpt&bit1)) { m_Led_In[9].SetBitmap(m_LedOn); } else m_Led_In[9].SetBitmap(m_LedOff); /////////////////////////////////// bit1<<=1; if(!(exInpt&bit1)) { m_Led_In[10].SetBitmap(m_LedOn); } else m_Led_In[10].SetBitmap(m_LedOff); bit1<<=1; if(!(exInpt&bit1)) { m_Led_In[11].SetBitmap(m_LedOn); } else m_Led_In[11].SetBitmap(m_LedOff); bit1<<=1; if(!(exInpt&bit1)) { m_Led_In[12].SetBitmap(m_LedOn); } else m_Led_In[12].SetBitmap(m_LedOff); bit1<<=1; if(!(exInpt&bit1)) { m_Led_In[13].SetBitmap(m_LedOn); } else m_Led_In[13].SetBitmap(m_LedOff); bit1<<=1; if(!(exInpt&bit1)) { m_Led_In[14].SetBitmap(m_LedOn); } else m_Led_In[14].SetBitmap(m_LedOff); bit1<<=1; if(!(exInpt&bit1)) { m_Led_In[15].SetBitmap(m_LedOn); // SetDlgItemText(IDC_ARM,"检查复位旋钮开关,继续..."); } else { m_Led_In[15].SetBitmap(m_LedOff); } /////////////////////////////////// unsigned short Home=65535,bit2=1; GT_GetHomeSwt(&Home); if(!(Home&bit2)) { m_Led_Home[0].SetBitmap(m_LedOn); } else m_Led_Home[0].SetBitmap(m_LedOff); bit2<<=1; if(!(Home&bit2)) { m_Led_Home[1].SetBitmap(m_LedOn); } else m_Led_Home[1].SetBitmap(m_LedOff); /////////////////////////////////// bit2<<=1; if(!(Home&bit2)) { m_Led_Home[2].SetBitmap(m_LedOn); } else { m_Led_Home[2].SetBitmap(m_LedOff); GT_ClrSts(3); // GT_LmtsOff(3); //GT_GetAtlPos(3,&CurPos.z); GT_ZeroPos(3); AxisRunT(3,5000,10,0.1); } bit2<<=1; if(!(Home&bit2)) { GT_ClrSts(4); // GT_LmtsOff(4); //GT_GetAtlPos(4,&CurPos.u); GT_ZeroPos(4); AxisRunT(4,200,1,0.02); } else m_Led_Home[3].SetBitmap(m_LedOff); /////////////////////////////////// short rtn=-1; unsigned long status; GT_GetSts(1,&status); if(status&0x2){ SetDlgItemText(IDC_ARM,"X轴伺服报警,请排除,重新启动电源");} GT_GetSts(2,&status); if(status&0x2){ SetDlgItemText(IDC_ARM,"Y轴伺服报警,请排除,重新启动电源");} /* {short rtn; unsigned short bit1; rtn=GT_ExInpt(&exInpt);//读取IO if(rtn!=0)return; bit1=exInpt&0x0001; if(bit1==0) {m_Led_Limit[0].SetBitmap(m_LedOn);} else {m_Led_Limit[0].SetBitmap(m_LedOff);} } GT_GetAtlPos(1,&CurPos.x); m_PosX.Format("%08.3f",CurPos.x/SportPara.XYpos); SetDlgItemText(IDC_STC_AXIS_X,m_PosX); GT_Update(1); //得到x轴当前位置值 GT_GetAtlPos(2,&CurPos.y); m_PosY.Format("%08.3f",CurPos.y/SportPara.XYpos); SetDlgItemText(IDC_STC_AXIS_Y,m_PosY); GT_Update(1); // ShowPosShowIO(); */ } // TODO: Add your message handler code here and/or call default CDialog::OnTimer(nIDEvent); } ...展开收缩
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 相关搜索: Auto.cpp
 输入关键字,在本站1000多万海量源码库中尽情搜索: