Ejemplo n.º 1
0
        async void PlcRun()
        {
            short QuestCycle = 100;
            bool  state1     = false;
            bool  first      = true;

            AlarmTuple[] AlarmTupleArray = new AlarmTuple[200];
            int          alramItemsCount = 0;
            string       alarmconfigfile = System.Environment.CurrentDirectory + "\\CA9报警.xlsx";

            try
            {
                alramItemsCount = UpdateAlarmFromExcel(alarmconfigfile, AlarmTupleArray);
                GlobalVar.AddMessage("加载报警项:" + alramItemsCount.ToString());
            }
            catch (Exception ex)
            {
                GlobalVar.AddMessage(ex.Message);
            }


            while (true)
            {
                await Task.Delay(QuestCycle);

                if (DateTime.Now.Hour % 2 == 0)
                {
                    if (DateTime.Now.Hour != ReUpdateHour)
                    {
                        ReUpdateHour = DateTime.Now.Hour;
                        Inifile.INIWriteValue(iniParameterPath, "System", "ReUpdateHour", ReUpdateHour.ToString());
                        GlobalVar.plc.vision.ReUploadAction();
                    }
                }
                Task task = Task.Run(() =>
                {
                    try
                    {
                        state1 = plc.ReadM(STATE, "M1000");
                        if (state1)
                        {
                            //拍照1
                            System.Threading.Thread.Sleep(20);
                            if (plc.ReadM(STATE, "M3000"))
                            {
                                plc.PLCWrite(STATE, "M3100", "0000");
                                GlobalVar.AddMessage("触发拍照1");
                                System.Threading.Thread.Sleep(20);
                                plc.PLCWrite(STATE, "M3000", "0000");
                                System.Threading.Thread.Sleep(20);
                                plc.PLCWrite(STATE, "M3002", "0000");
                                GlobalVar.plc.vision.GetImage1();
                                plc.PLCWrite(STATE, "M3002", "FF00");
                            }
                            //拍照2
                            System.Threading.Thread.Sleep(20);
                            if (plc.ReadM(STATE, "M3001"))
                            {
                                GlobalVar.AddMessage("触发拍照2");
                                System.Threading.Thread.Sleep(20);
                                plc.PLCWrite(STATE, "M3001", "0000");
                                System.Threading.Thread.Sleep(20);

                                plc.PLCWrite(STATE, "M3003", "0000");
                                GlobalVar.plc.vision.GetImage2();
                                plc.PLCWrite(STATE, "M3003", "FF00");
                                GlobalVar.plc.vision.ProcessImage();
                                string Str_Result_etch = GetCoilStr(GlobalVar.plc.vision.Result_etch);
                                System.Threading.Thread.Sleep(20);
                                plc.PLCWriteBit(STATE, "M3101", "0006", Str_Result_etch);
                                GlobalVar.AddMessage("蚀刻信息已写入PLC");
                                //string Str_Result_blue = GetCoilStr(GlobalVar.plc.vision.Result_blue);
                                //System.Threading.Thread.Sleep(20);
                                //plc.PLCWriteBit(STATE, "M121", "000A", Str_Result_blue);


                                string Str_Result_barcode = GetCoilStr(GlobalVar.plc.vision.Result_barcode);
                                System.Threading.Thread.Sleep(20);
                                plc.PLCWriteBit(STATE, "M3111", "0006", Str_Result_barcode);
                                GlobalVar.AddMessage("扫码信息已写入PLC");



                                plc.PLCWrite(STATE, "M3100", "FF00");
                                GlobalVar.AddMessage("本次处理完成");
                            }
                            //if (plc.ReadM(STATE, "M260"))
                            //{
                            //    GlobalVar.AddMessage("触发扫码");
                            //    System.Threading.Thread.Sleep(20);
                            //    plc.PLCWrite(STATE, "M260", "0000");
                            //    System.Threading.Thread.Sleep(20);
                            //    plc.PLCWrite(STATE, "M262", "0000");
                            //    System.Threading.Thread.Sleep(20);
                            //    plc.PLCWrite(STATE, "M264", "0000");
                            //    ScanC.GetBarCode(PLCScanBCallback);
                            //}
                            //报警
                            for (int i = 0; i < alramItemsCount; i++)
                            {
                                AlarmTupleArray[i].CoilStatus = plc.ReadM(STATE, AlarmTupleArray[i].CoilName);
                                if (AlarmTupleArray[i].LastCoilStatus != AlarmTupleArray[i].CoilStatus)
                                {
                                    AlarmTupleArray[i].LastCoilStatus = AlarmTupleArray[i].CoilStatus;
                                    if (AlarmTupleArray[i].CoilStatus && !first)
                                    {
                                        AlarmTableItem _alarmTableItem = new AlarmTableItem();
                                        _alarmTableItem.AlarmDate      = DateTime.Now.ToString();
                                        _alarmTableItem.AlarmMessage   = AlarmTupleArray[i].AlarmContent;
                                        _alarmTableItem.MachineID      = GlobalVar.MachineID;
                                        _alarmTableItem.UserID         = GlobalVar.UserID;

                                        lock (GlobalVar.obj)
                                            GlobalVar.AlarmRecordQueue.Enqueue(_alarmTableItem);


                                        SaveCSVfileAlarm(_alarmTableItem.AlarmMessage);

                                        //记录报警
                                    }
                                }
                            }

                            first = false;
                        }
                        QuestCycle = 100;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        state1     = false;
                        QuestCycle = 1000;
                    }
                });
                await task;
                Connect = state1;
            }
        }