Esempio n. 1
0
        protected override void ProcDataDeal()
        {
            while (isLink)
            {
                // 状态读取
                try
                {
                    commWaitResetEvent.WaitOne();

                    #region INT读取
                    short rtn;
                    int   ofst;
                    ReadInput();

                    #endregion
                    Thread.Sleep(3);

                    #region OUT写入
                    //// 锁定,从共享存储器获取要写入plc的数据
                    //mutexShMem.WaitOne();
                    //short data = new short();
                    //ofst = DMO_OFST;

                    //data = Marshal.ReadInt16(mapAddrPtr, ofst);
                    //// 解除
                    //mutexShMem.ReleaseMutex();
                    ////状态取反后输出
                    //ushort ioOutput = (ushort)(~data);
                    //lock (cardActLock)
                    //{
                    //    GT400.GT_ExOpt(ioOutput);
                    //}
                    #endregion

                    commWaitResetEvent.WaitOne();

                    #region 轴状态获取

                    ushort[] status = new ushort[axisNum];
                    int[]    pos    = new int[axisNum];
                    for (int i = 0; i < axisNum; i++)
                    {
                        lock (cardActLock)
                        {
                            rtn = GT400.GT_Axis((ushort)(i + 1));
                            runningError("当前轴设置", rtn);

                            rtn = GT400.GT_GetSts(out status[i]);
                            runningError("当前轴状态读取", rtn);

                            rtn = GT400.GT_GetPos(out pos[i]);
                            runningError("当前轴位置信息读取", rtn);
                        }
                        Thread.Sleep(3);
                    }

                    int count = 0;
                    for (int i = 0; i < axisNum; i++)
                    {
                        mutexShMem.WaitOne();

                        // 共有存储在存储器
                        ofst = DMI_OFST + (1 + i) * Marshal.SizeOf(typeof(short));
                        //将读取到的PLC数据写入到相应的内存区域
                        Marshal.WriteInt16(mapAddrPtr, ofst, (short)status[i]);


                        //位置信息
                        ofst = DMI_OFST + (DMO_A_START + count) * Marshal.SizeOf(typeof(int));

                        count++;
                        Marshal.WriteInt32(mapAddrPtr, ofst, pos[i]);
                        ofst = DMI_OFST + (DMO_A_START + count) * Marshal.SizeOf(typeof(int));
                        count++;
                        // 解除
                        mutexShMem.ReleaseMutex();
                    }
                    #endregion
                    Thread.Sleep(3);

                    commWaitResetEvent.WaitOne();
                    ////Measurement.MeasurementManager measureManger = Measurement.MeasurementManager.Instance;
                    ////int left = GetDi(Define.DiLeftButton);
                    ////int es = GetDi(Define.DiEmergencyStop);

                    //if (left == 1 && leftButtonBefore == 0 &&
                    //    measureManger.InMeasurement == false&&
                    //    es==0)
                    //{

                    //    try
                    //    {
                    //        source = new CancellationTokenSource();
                    //        Task.Run(() =>
                    //        {
                    //            measureManger.RunMeasurementLeft();
                    //        }, source.Token);
                    //    }
                    //    catch (AggregateException)//捕获紧急停止按钮
                    //    {
                    //        //MessageBox.Show("停止按钮被按下", "异常", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    //    }
                    //}
                    //leftButtonBefore = left;

                    //int right = GetDi(Define.DiRightButton);
                    //if (right == 1 &&
                    //    rightButtonBefore == 0 &&
                    //    measureManger.InMeasurement == false&&
                    //    es==0)
                    //{

                    //    try
                    //    {
                    //        source = new CancellationTokenSource();
                    //        Task.Run(() =>
                    //        {
                    //            measureManger.RunMeasurementRight();
                    //        }, source.Token);
                    //    }
                    //    catch (AggregateException)//捕获紧急停止按钮
                    //    {
                    //        //MessageBox.Show("停止按钮被按下", "异常", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    //    }

                    //}
                    //rightButtonBefore = right;

                    //if (es==1&&esButtonBefore==0)
                    //{
                    //    measureManger.InMeasurement = false;
                    //    //MessageBox.Show("停止按钮被按下", "异常", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    //}
                    //esButtonBefore = es;
                }
                catch (Exception)
                {
                }
                Thread.Sleep(3);
            }
            CloseCom();
        }