public DataResult <ModbusSerialInfo> InitSerialInfo()
        {
            DataResult <ModbusSerialInfo> result = new DataResult <ModbusSerialInfo>();

            result.State = false;

            try
            {
                ModbusSerialInfo SerialInfo = new ModbusSerialInfo();
                SerialInfo.PortName = ConfigurationManager.AppSettings["port"];
                SerialInfo.BaudRate = int.Parse(ConfigurationManager.AppSettings["baud"].ToString());
                SerialInfo.DataBit  = int.Parse(ConfigurationManager.AppSettings["data_bit"].ToString());
                SerialInfo.Parity   = (System.IO.Ports.Parity)Enum.Parse(typeof(System.IO.Ports.Parity), ConfigurationManager.AppSettings["parity"].ToString(), true);
                SerialInfo.StopBits = (System.IO.Ports.StopBits)Enum.Parse(typeof(System.IO.Ports.StopBits), ConfigurationManager.AppSettings["stop_bit"].ToString(), true);

                result.State = true;
                result.Data  = SerialInfo;
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }

            return(result);
        }
        public static void Start(Action successAction, Action <string> faultAction)
        {
            mainTask = Task.Run(new Action(async() =>
            {
                IndustrialBLL bll = new IndustrialBLL();
                /// 获取串口配置信息
                var si = bll.InitSerialInfo();
                if (si.State)
                {
                    SerialInfo = si.Data;
                }
                else
                {
                    faultAction.Invoke(si.Message); return;
                }

                /// 初始化存储区
                var sa = bll.InitStorageArea();
                if (sa.State)
                {
                    StorageList = sa.Data;
                }
                else
                {
                    faultAction.Invoke(sa.Message); return;
                }

                /// 初始化设备变量集合以及警戒值
                var dr = bll.InitDevices();
                if (dr.State)
                {
                    foreach (var item in dr.Data)
                    {
                        DeviceList.Add(item);
                    }
                }
                else
                {
                    faultAction.Invoke(dr.Message); return;
                }


                /// 初始化ModbusRTU串口通信
                var rtu       = RTU.GetInstance(SerialInfo);
                rtu.Responsed = new Action <int, List <byte> >(ParsingData);
                CommList.Add(rtu);

                /// 连接串口
                if (rtu.Connection())
                {
                    successAction.Invoke();

                    while (isRunning)
                    {
                        int startAddress = 0;
                        //int errorCount = 0;

                        foreach (var item in StorageList)
                        {
                            if (item.Length > 100)
                            {
                                startAddress  = item.StartAddress;
                                int readCount = item.Length / 100;
                                for (int i = 0; i < readCount; i++)
                                {
                                    int readLength = i == readCount ? item.Length - 100 * i : 100;

                                    await rtu.Send(item.SlaveAddress, (byte)int.Parse(item.FuncCode), startAddress + 100 * i, readLength);
                                }
                            }
                            if (item.Length % 100 > 0)
                            {
                                await rtu.Send(item.SlaveAddress, (byte)int.Parse(item.FuncCode), startAddress + 100 * (item.Length / 100), item.Length % 100);
                            }
                            //if (item.StorageName == "01 Coil Status(0x)")
                            //{
                            //}
                            //else if (item.StorageName == "03 Holding Register(4x)")
                            //{
                            //    List<byte> byteList = new List<byte>();
                            //    //如果长度过大,分批次去读,最大不要超过125
                            //    if (item.Length > 100)
                            //    {
                            //        startAddress = item.StartAddress;
                            //        int readCount = item.Length / 100;
                            //        for (int i = 0; i < readCount; i++)
                            //        {
                            //            int readLength = i == readCount ? item.Length - 100 * i : 100;
                            //            List<byte> resp = await rtu.RequestKeepReg(item.SlaveAddress, startAddress + 100 * i, readLength);
                            //            if (resp != null)
                            //            {
                            //                //errorCount = 0;
                            //                byteList.AddRange(resp);
                            //            }
                            //            //else
                            //            //{
                            //            //    errorCount += 1;
                            //            //}

                            //        }
                            //    }

                            //    if (item.Length % 100 > 0)
                            //    {
                            //        List<byte> resp = await rtu.RequestKeepReg(SerialInfo.Address, startAddress, item.Length % 100);
                            //        if (resp != null)
                            //        {
                            //            //errorCount = 0;
                            //            byteList.AddRange(resp);
                            //        }
                            //        //else
                            //        //{
                            //        //    errorCount += 1;
                            //        //}
                            //    }

                            //    //解析
                            //    if (byteList.Count == item.Length * 2)
                            //    {
                            //        ParsingData4x(byteList);
                            //    }
                            //}
                        }
                    }
                }
                else
                {
                    faultAction.Invoke("程序无法启动,串口连接初始化失败!请检查设备是否连接正常。");
                }
            }));
        }