Exemple #1
0
        void Run()
        {
            //将当前线程绑定到指定的cpu核心上
            GetCpu.SetThreadAffinityMask(GetCpu.GetCurrentThread(), new UIntPtr(0x4));

            kzwbmlt12 = DateTime.Now;
            string   path = PathHelper.currentProductPath + @"\" + "spd" + ".txt";
            DateTime t1;
            int      xjfwi;

            spd = Card.maxspeed;
            int preSignal = 0, signal = 0;

            PCI408.PCI408_counter_config(0, 3);
            PCI408.PCI408_set_encoder(0, 0);
            int  fwjs1 = 0, fwjs2 = 0; sdkz3 = 100000;
            long cuowu = PCI408.PCI408_compare_set_filter_Extern(Card.cardNo, 100000);

            PCI408.PCI408_config_latch_mode(Card.cardNo, Card.zhouhao);
            sdkzt1 = DateTime.Now; sdkzt2 = DateTime.Now; sdkzt4 = DateTime.Now; sdkzt3 = DateTime.Now;
            long jcqbz;

            for (int xjh = 0; xjh < PosArray.Count(); xjh++)
            {
                PCI408.PCI408_compare_clear_points_Extern(0, (ushort)(xjh + 1));
                PCI408.PCI408_compare_config_Extern(Card.cardNo, (ushort)(xjh + 1), 1, Card.zhouhao, IOs[xjh]);
                PCI408.PCI408_compare_set_pulsetimes_Extern(0, (ushort)(xjh + 1), 20);
            }
            PCI408.PCI408_reset_latch_flag(Card.cardNo);
            long s5;

            currentPos = (int)PCI408.PCI408_get_encoder(Card.cardNo);
            int[] sigci = new int[5], psigci = new int[5];
            for (int sigfi = 0; sigfi < 5; sigfi++)
            {
                sigci[sigfi]  = 0;
                psigci[sigfi] = 0;
            }

            //?
            int zdwy = 40000;

            while (isStart)
            {
                try
                {
                    //    for (xjfwi = 1; xjfwi < cameraCount + 1; xjfwi++)
                    //{
                    //    xjfws[xjfwi].t2 = DateTime.Now;
                    //    //if (xjfws[xjfwi].tsc() > 200000 && copos[xjfwi].pos.Count() > 0)
                    //   //if (xjfws[xjfwi].tsc() > 200000)
                    //   // {
                    //   //     //copos[0].pos.Clear();
                    //   //     //copos[1].pos.Clear();
                    //   //     //copos[2].pos.Clear();
                    //   //     //copos[3].pos.Clear();
                    //   //     //copos[4].pos.Clear();
                    //   //     //copos[5].pos.Clear();
                    //   //     //copos[6].pos.Clear();
                    //   //     nutqueue.Clear();
                    //   //     xjfws.Initialize();
                    //   //     copos.Initialize();
                    //   //     timers.Initialize();
                    //   //     xjfws = new xjfw[7];
                    //   //     //for (xjfwi = 0; xjfwi < 5; xjfwi++)
                    //   //     //{
                    //   //     //    xjfws[xjfwi] = new xjfw();
                    //   //     //    xjfws[xjfwi].ID = xjfwi;
                    //   //     //    xjfws[xjfwi].t1 = DateTime.Now;
                    //   //     //    xjfws[xjfwi].t2 = DateTime.Now;
                    //   //     //}
                    //   //     copos = new coppos[7];
                    //   //     //for (int k = 0; k < 5; k++)
                    //   //     //{
                    //   //     //    copos[k] = new coppos();
                    //   //     //    copos[k].ID = k;
                    //   //     //    //copos[k].onwrite = false;
                    //   //     //}
                    //   //     timers = new MyTimer[7];
                    //   //     //for (int i = 0; i < timers.Length; i++)
                    //   //     //{
                    //   //     //    timers[i] = new MyTimer();
                    //   //     //    timers[i].ID = i;
                    //   //     //    //timers[i].OnTimerEvent += new MyTimer.OnTimerEventHandler(OnTimer);
                    //   //     //}
                    //   //     //Thread.Sleep(1000);
                    //   // }
                    //}
                    //    t1 = DateTime.Now;
                    currentPos = (int)PCI408.PCI408_get_encoder(Card.zhouhao);
                    for (int sr = 0; sr < cameraCount; sr++)
                    {
                        sigci[sr] = PCI408.PCI408_read_inbit(Card.cardNo, (ushort)xjzsr[sr]);
                        if (sigci[sr] == 0 && psigci[sr] == 1)
                        {
                            int length1 = dpdl[sr].Count(), jr = 200, jr1 = 5000;
                            for (int listi = 0; listi < length1; listi++)
                            {
                                int ssg = Math.Abs(dpdl[sr][listi] + PosArray[sr] - currentPos);
                                if (ssg < jr1)
                                {
                                    jr1 = ssg;
                                    jr  = listi;
                                }
                            }
                            if (jr != 200 && jr1 < 5000)
                            {
                                copos[sr].pos.Add(dpdl[sr][jr]);
                                //   Console.WriteLine("trun " + DateTime.Now.ToString("mm-ss-fff"));
                                dpdl[sr].RemoveAt(jr);
                            }
                        }
                        psigci[sr] = sigci[sr];
                    }
                    for (int sr1 = 0; sr1 < cameraCount; sr1++)
                    {
                        int length2 = dpdl[sr1].Count();
                        for (int listi1 = 0; listi1 < length2; listi1++)
                        {
                            if (Math.Abs(currentPos - dpdl[sr1][listi1]) > zdwy)
                            {
                                dpdl[sr1].RemoveAt(listi1);
                            }
                        }
                    }
                    jcqbz = PCI408.PCI408_get_latch_flag(Card.cardNo);    //读取锁存器状态
                    if ((jcqbz & 0xf00) > 0)
                    {
                        PCI408.PCI408_reset_latch_flag(Card.cardNo);
                        signal = PCI408.PCI408_get_latch_value(Card.zhouhao);
                    }
                    if (fwjs1 - fwjs2 > 4000)
                    {
                        fwjs1 = fwjs2;
                        for (int sr = 0; sr < cameraCount; sr++)
                        {
                            sigci[sr]  = 0;
                            psigci[sr] = 0;
                            dpdl[sr].Clear();
                            copos[sr].pos.Clear();
                        }
                        for (int xjh = 0; xjh < PosArray.Count(); xjh++)
                        {
                            PCI408.PCI408_set_encoder(0, 0);
                            PCI408.PCI408_compare_clear_points_Extern(0, (ushort)(xjh + 1));
                            PCI408.PCI408_compare_config_Extern(Card.cardNo, (ushort)(xjh + 1), 1, Card.zhouhao, IOs[xjh]);
                            PCI408.PCI408_compare_set_pulsetimes_Extern(0, (ushort)(xjh + 1), 5);
                        }
                        for (int xjs = 0; xjs < cameraCount; xjs++)
                        {
                            jcqdls[xjs].clear();
                            //File.AppendAllText(path,signal.ToString()+"  ", Encoding.Default);
                        }
                        PCI408.PCI408_reset_latch_flag(Card.cardNo);
                        preSignal = signal;
                        if (isStart)
                        {
                            nutqueue.Clear();
                        }
                    }
                    if (signal != preSignal)
                    {
                        kzwbmlt12 = DateTime.Now;
                        s5        = PCI408.PCI408_compare_get_points_remained_Extern(0, 1);
                        if (s5 > cameraCount + 3)
                        {
                            Nut nut = new Nut();
                            fwjs1         += 1;
                            nut.initialPos = signal;
                            nutqueue.Add(nut);
                            nut.cas = cameraCount;
                            pn.totalCount++;
                            speedTemp++;
                            ss = pn.totalCount;
                            for (int xjs = 0; xjs < cameraCount; xjs++)
                            {
                                PCI408.PCI408_compare_config_Extern(Card.cardNo, (ushort)(xjs + 1), 1, Card.zhouhao, 1);
                                PCI408.PCI408_compare_add_point_Extern(Card.cardNo, (ushort)(xjs + 1), PosArray[xjs] + (int)signal, 1, 9, IOs[xjs]);

                                // copos[nut.posNo + 1].pos.Add (nut.initialPos);
                                // copos[nut.posNo + 1].onwrite = true;
                                dpdl[xjs].Add(signal);
                                // Console.WriteLine("ss" + signal);
                                //File.AppendAllText(path,signal.ToString()+"  ", Encoding.Default);
                            }
                        }
                    }
                    niuli();

                    for (int i = nutqueue.Count - 1; i > -1; i--)
                    {
                        Nut nutc = new Nut();
                        nutc = nutqueue[i];
                        //if (currentPos < nutc.initialPos)
                        //{
                        //    nutqueue.Remove(nutc);
                        //    break;
                        //}
                        if (currentPos - nutc.initialPos > PosArray[cameraCount] - 1000)
                        {
                            int a = nutc.getTheHole();
                            switch (a.ToString())
                            {
                            case "1":
                                //pn.mnum++;
                                //Console.WriteLine("cipin" + pn.mnum);
                                PCI408.PCI408_compare_config_Extern(Card.cardNo, (ushort)(cameraCount + 1), 1, Card.zhouhao, 1);
                                PCI408.PCI408_compare_set_pulsetimes_Extern(0, (ushort)(cameraCount + 1), 10);
                                PCI408.PCI408_compare_add_point_Extern(Card.cardNo, (ushort)(cameraCount + 1), PosArray[cameraCount] + (int)nutc.initialPos, 1, 9, IOs[cameraCount]);
                                break;

                            //case "3":
                            //pn.nnum++;
                            //Console.WriteLine("未识别" + pn.nnum);
                            //PCI408.PCI408_compare_config_Extern(Card.cardNo, (ushort)(cameraCount + 1), 1, Card.zhouhao, 1);
                            //PCI408.PCI408_compare_set_pulsetimes_Extern(0, (ushort)(cameraCount + 1), 20);
                            //PCI408.PCI408_compare_add_point_Extern(Card.cardNo, (ushort)(cameraCount + 1), PosArray[cameraCount] + (int)nutc.initialPos, 1, 9, IOs[cameraCount]);
                            //break;
                            case "2":
                                pn.goodNum++;
                                fwjs2 = fwjs1;
                                PCI408.PCI408_compare_config_Extern(Card.cardNo, (ushort)(cameraCount + 2), 1, Card.zhouhao, 1);
                                PCI408.PCI408_compare_set_pulsetimes_Extern(0, (ushort)(cameraCount + 2), 10);
                                PCI408.PCI408_compare_add_point_Extern(Card.cardNo, (ushort)(cameraCount + 2), PosArray[cameraCount + 1] + (int)nutc.initialPos, 1, 9, IOs[cameraCount + 1]);
                                break;
                            }
                            jcjg2 = nutc.jiance;
                            Thread ccjcjg = new Thread(cunchujiancejieguo);
                            ccjcjg.Start();
                            nutqueue.Remove(nutc);
                            pn.badNum = pn.totalCount - pn.goodNum;
                        }
                    }
                    preSignal = signal;
                    kongzhiwaibu();
                    sdkzt1 = DateTime.Now;
                    sdkzt4 = DateTime.Now;
                    if (Card.sjc(sdkzt3, sdkzt4) < sdkz3)
                    {
                        sdkz3 = Card.sjc(sdkzt3, sdkzt4);
                    }
                    sdkzt3 = DateTime.Now;
                    //if (Card.sjc(sdkzt2, sdkzt1) > 60000)
                    //{
                    //    sdkzt2 = DateTime.Now;
                    //    if (sdkz3 < 10)
                    //    {
                    //        spd = spd + 100;
                    //        PCI408.PCI408_change_speed(Card.cardNo, spd);
                    //        File.AppendAllText(path, spd.ToString() + "  ", Encoding.Default);
                    //    }
                    //}
                }
                catch (Exception e)
                {
                    MyDebug.ShowMessage(e, "运动控制出错");
                }
                finally
                {
                    //MyDebug.ShowMessage(nutqueue.Count.ToString());
                }
            }
        }