コード例 #1
0
        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("程序无法启动,串口连接初始化失败!请检查设备是否连接正常。");
                }
            }));
        }
コード例 #2
0
        public static void Start(Action successAction, Action <string> faultAction)
        {
            mainTask = Task.Run(async() =>
            {
                IndustrialBLL bll = new IndustrialBLL();
                //获取串口配置信息
                var si = bll.InitSerialInfo();
                if (si.State)
                {
                    SerialInfo = si.Data;
                }
                else
                {
                    faultAction(si.Message);
                    return;
                }

                //获取存储区信息
                var sa = bll.InitStorageArea();
                if (sa.State)
                {
                    StorageList = sa.Data;
                }
                else
                {
                    faultAction(sa.Message);
                    return;
                }

                //初始化设备变量及警戒值
                var dr = bll.InitDevices();
                if (dr.State)
                {
                    DeviceList = dr.Data;
                }
                else
                {
                    faultAction(dr.Message);
                    return;
                }

                rtuInstance = RTU.GetInstance(SerialInfo);
                if (rtuInstance.Connection())
                {
                    successAction();

                    int startAddr = 0;
                    while (isRunning)
                    {
                        foreach (var item in StorageList)
                        {
                            if (item.Length > 100)
                            {
                                startAddr     = item.StartAddress;
                                int readCount = item.Length / 100;
                                for (int i = 0; i < readCount; i++)
                                {
                                    int readLen = i == readCount ? item.Length - 100 * i : 100;
                                    await rtuInstance.Send(item.SlaveAdress, (byte)Convert.ToInt32(item.FuncCode), startAddr + 100 * (item.Length / 100), item.Length % 100);
                                }
                            }
                            if (item.Length % 100 > 0)
                            {
                                await rtuInstance.Send(item.SlaveAdress, (byte)Convert.ToInt32(item.FuncCode), startAddr + 100 * (item.Length / 100), item.Length % 100);
                            }
                        }
                    }
                }
                else
                {
                    faultAction("串口连接初始化失败,请检查设备!");
                }
            });
        }