Program.cs 9.2 KB
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DoubleLine
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            Application.ThreadException += Application_ThreadException;
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            //程序只能运行一次
            Process current = Process.GetCurrentProcess();
            Process[] processes = Process.GetProcessesByName(current.ProcessName);
            foreach (Process process in processes)
            {
                if (process.Id == current.Id) continue;  //自己
                if (process.MainModule.FileName == current.MainModule.FileName)
                {
                    //显示已打开的程序
                    API.ShowWindow(process.MainWindowHandle, API.SW_RESTORE);
                    API.SwitchToThisWindow(process.MainWindowHandle, true);
                    return;
                }
            }

            string path = System.Configuration.ConfigurationManager.AppSettings["LogPath"];
            Common.log = new Asa.File.Log(path, "DoubleLine");
            Common.log.OutString("=====程序开始=====");
            Common.BenqAGV = new BLL.BenQ_AGV();
            Common.Web = new BLL.WebService();
            Common.ControlCenter = new BLL.ControlCenter();
            ReadConfig();

            Application.Run(new FrmMain());

            Common.ABB.Exit();
            Common.RFID.Close();
            Common.log.OutInfo("关闭RFID");
            Common.LineDouble.Exit();
            Common.LinePack.Exit();
            Common.LineDischarge.Exit();
            Common.ControlCenter.Exit();
            Common.BenqAGV.Close();
            if (Common.RFID != null) Common.RFID.Close();
            Common.log.OutString("=====程序结束=====\r\n");
            Common.log.Dispose();

            //System.Threading.Thread.Sleep(100);
            //Environment.Exit(0);
        }

        private static void ReadConfig()
        {
            Common.ABB = new BLL.ABB();
            Common.ABB_Info = new ABBInfo[3];
            for (int i = 0; i < Common.ABB_Info.Length; i++)
            {
                string dev = "ABB" + (i + 1);
                string ip = System.Configuration.ConfigurationManager.AppSettings[dev + "_IP"];
                string ss = System.Configuration.ConfigurationManager.AppSettings[dev + "_Speed"];
                int.TryParse(ss, out int speed);
                Common.ABB_Info[i] = new ABBInfo(dev, ip, speed);
            }

            Common.RFID = new Asa.RFID.ReaderAll();
            Common.RFID_Info = new RFIDInfo[7];
            for (int i = 0; i < Common.RFID_Info.Length; i++)
            {
                string dev = "RFID" + (i + 1);
                string name = System.Configuration.ConfigurationManager.AppSettings[dev + "_Name"];
                string ip = System.Configuration.ConfigurationManager.AppSettings[dev + "_IP"];
                Common.RFID_Info[i] = new RFIDInfo(name, ip);
            }

            Common.LineDouble = new BLL.LineDouble();
            Common.LinePack = new BLL.LinePack();
            Common.LineDischarge = new BLL.LineDischarge();
            Common.IO_Info = new IOInfo[6];
            for (int i = 0; i < Common.IO_Info.Length; i++)
            {
                string dev = "IO" + (i + 1);
                string name = System.Configuration.ConfigurationManager.AppSettings[dev + "_Name"];
                string ip = System.Configuration.ConfigurationManager.AppSettings[dev + "_IP"];
                Common.IO_Info[i] = new IOInfo(name, ip);
            }
            DoubleLineIO_ReadConfig();
            PackLineIO_ReadConfig();
            DischargeLineIO_ReadConfig();

        


        }

        private static void DoubleLineIO_ReadConfig()
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + "Explain\\DoubleIO.csv";
            try
            {
                Common.IO_Info[0].DI = new Register[16];
                Common.IO_Info[0].DO = new Register[16];
                Common.IO_Info[0].Box.SetInput(Asa.IOModule.Box_Type.DI, 16);
                Common.IO_Info[0].Box.SetOutput(Asa.IOModule.Box_Type.DO, 16);
                Common.IO_Info[0].Box.AutoReadInput(true, 100);

                Common.IO_Info[1].DI = new Register[16];
                Common.IO_Info[1].DO = new Register[16];
                Common.IO_Info[1].Box.SetInput(Asa.IOModule.Box_Type.DI, 16);
                Common.IO_Info[1].Box.SetOutput(Asa.IOModule.Box_Type.DO, 16);
                Common.IO_Info[1].Box.AutoReadInput(true, 100);

                Common.IO_Info[2].DI = new Register[8];
                Common.IO_Info[2].DO = new Register[24];
                Common.IO_Info[2].Box.SetInput(Asa.IOModule.Box_Type.DI, 8);
                Common.IO_Info[2].Box.SetOutput(Asa.IOModule.Box_Type.DO, 24);
                Common.IO_Info[2].Box.AutoReadInput(true, 100);

                string[] text = System.IO.File.ReadAllLines(path);
                for (int i = 0; i < text.Length; i++)
                {
                    if (i == 96) break;
                    string[] temp = text[i].Split(',');
                    if (temp.Length != 2) continue;

                    if (i < 16)
                        Common.IO_Info[0].DI[i] = new Register("DI_" + (i + 1), temp[0], temp[1]);
                    else if (i < 32)
                        Common.IO_Info[0].DO[i - 16] = new Register("DO_" + (i - 15), temp[0], temp[1]);
                    else if (i < 48)
                        Common.IO_Info[1].DI[i - 32] = new Register("DI_" + (i - 31), temp[0], temp[1]);
                    else if (i < 64)
                        Common.IO_Info[1].DO[i - 48] = new Register("DO_" + (i - 47), temp[0], temp[1]);
                    else if (i < 72)
                        Common.IO_Info[2].DI[i - 64] = new Register("DI_" + (i - 63), temp[0], temp[1]);
                    else if (i < 96)
                        Common.IO_Info[2].DO[i - 72] = new Register("DO_" + (i - 71), temp[0], temp[1]);

                }
                Common.log.OutInfo("读取DoubleLineIO配置文件完成");
            }
            catch (Exception ex)
            {
                Common.log.OutError(ex);
            }

        }

        private static void PackLineIO_ReadConfig()
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + "Explain\\PackingIO.csv";
            try
            {
                Common.IO_Info[3].DI = new Register[16];
                Common.IO_Info[3].DO = new Register[16];
                Common.IO_Info[3].Box.SetInput(Asa.IOModule.Box_Type.DI, 16);
                Common.IO_Info[3].Box.SetOutput(Asa.IOModule.Box_Type.DO, 16);
                Common.IO_Info[3].Box.AutoReadInput(true, 100);

                string[] text = System.IO.File.ReadAllLines(path);
                for (int i = 0; i < text.Length; i++)
                {
                    if (i == 32) break;
                    string[] temp = text[i].Split(',');
                    if (temp.Length != 2) continue;

                    if (i < 16)
                        Common.IO_Info[3].DI[i] = new Register("DI_" + (i + 1), temp[0], temp[1]);
                    else if (i < 32)
                        Common.IO_Info[3].DO[i - 16] = new Register("DO_" + (i - 15), temp[0], temp[1]);

                }
                Common.log.OutInfo("读取PackingIO配置文件完成");
            }
            catch (Exception ex)
            {
                Common.log.OutError(ex);
            }

        }

        private static void DischargeLineIO_ReadConfig()
        {
            try
            {
                Common.IO_Info[4].DI = new Register[8];
                Common.IO_Info[4].DO = new Register[8];
                Common.IO_Info[4].Box.SetInput(Asa.IOModule.Box_Type.DI, 8);
                Common.IO_Info[4].Box.SetOutput(Asa.IOModule.Box_Type.DO, 8);
                Common.IO_Info[4].Box.AutoReadInput(true, 100);

                Common.IO_Info[5].DI = new Register[8];
                Common.IO_Info[5].DO = new Register[8];
                Common.IO_Info[5].Box.SetInput(Asa.IOModule.Box_Type.DI, 8);
                Common.IO_Info[5].Box.SetOutput(Asa.IOModule.Box_Type.DO, 8);
                Common.IO_Info[5].Box.AutoReadInput(true, 100);

                Common.log.OutInfo("读取DischargeLineIO配置文件完成");
            }
            catch (Exception ex)
            {
                Common.log.OutError(ex);
            }

        }

        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Common.log.OutError((Exception)e.ExceptionObject);
        }

        private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            Common.log.OutError(e.Exception);
        }
    }
}