获取药箱状态方法3次重试修改
This commit is contained in:
		
							parent
							
								
									c7d7a3b266
								
							
						
					
					
						commit
						e75f04beaf
					
				| 
						 | 
				
			
			@ -52,7 +52,7 @@ namespace DM_Weight.Port
 | 
			
		|||
        private void SetModusIpMaster()
 | 
			
		||||
        {
 | 
			
		||||
            logger.Info("SetModusIpMaster");
 | 
			
		||||
            socket =  KeepALiveSocket.MakeKeepALiveSocket(ip, port); 
 | 
			
		||||
            socket = KeepALiveSocket.MakeKeepALiveSocket(ip, port);
 | 
			
		||||
            client = new TcpClient();
 | 
			
		||||
            client.Client = socket;
 | 
			
		||||
            master = ModbusIpMaster.CreateIp(client);
 | 
			
		||||
| 
						 | 
				
			
			@ -61,58 +61,85 @@ namespace DM_Weight.Port
 | 
			
		|||
 | 
			
		||||
        public bool[] GetAllBoxState()
 | 
			
		||||
        {
 | 
			
		||||
            //bool[] bools = { true };
 | 
			
		||||
            //if (BoxOperate)
 | 
			
		||||
            //{
 | 
			
		||||
                //if (ConfigurationManager.AppSettings["test"] != null && ConfigurationManager.AppSettings["test"].ToString() == "Y")
 | 
			
		||||
                //{
 | 
			
		||||
                //    return new bool[] { false,false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false};
 | 
			
		||||
                //}
 | 
			
		||||
                bool[] bools = Policy.Handle<Exception>()
 | 
			
		||||
                    .Retry(3, (exception, retryCount) =>
 | 
			
		||||
            //bool[] bools = Policy.Handle<Exception>()
 | 
			
		||||
            //    .Retry(3, (exception, retryCount) =>
 | 
			
		||||
            //    {
 | 
			
		||||
            //        this.Dispose();
 | 
			
		||||
            //        //Debug.WriteLine($"获取所有箱子门状态出错,第{retryCount}次重试", exception);
 | 
			
		||||
            //        logger.Info($"获取所有箱子门状态出错,第{retryCount}次重试, 异常信息{exception}");
 | 
			
		||||
            //        Thread.Sleep(50);
 | 
			
		||||
            //        this.SetModusIpMaster();
 | 
			
		||||
            //        BoxOperate = false;
 | 
			
		||||
            //        // return TimeSpan.FromSeconds (1);
 | 
			
		||||
            //    }).Execute<bool[]>(() =>
 | 
			
		||||
            //    {
 | 
			
		||||
            //        bool[] flags = new bool[18];
 | 
			
		||||
            //        try
 | 
			
		||||
            //        {
 | 
			
		||||
            //            if (master == null)
 | 
			
		||||
            //            {
 | 
			
		||||
            //                this.SetModusIpMaster();
 | 
			
		||||
            //            }
 | 
			
		||||
            //            var result = master.ReadInputRegisters(1, 0x0033, 2);
 | 
			
		||||
            //            var r1 = Convert.ToString(((int)result[0] >> 14) | ((int)result[1] << 2), 2).PadLeft(18, '0');
 | 
			
		||||
            //            var r2 = r1.ToCharArray();
 | 
			
		||||
            //            logger.Info("r2=>" + string.Join(", ", r2));
 | 
			
		||||
            //            //var r2=new char[18] { '0','0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0' };
 | 
			
		||||
            //            for (int i = 0; i < 18; i++)
 | 
			
		||||
            //            {
 | 
			
		||||
            //                flags[i] = r2[17 - i] == '1' ? true : false;
 | 
			
		||||
            //            }
 | 
			
		||||
            //            logger.Info($"获取所有箱子门状态返回:{string.Join(',', flags)};");
 | 
			
		||||
            //        }
 | 
			
		||||
            //        catch (Exception)
 | 
			
		||||
            //        {
 | 
			
		||||
            //            this.Dispose();
 | 
			
		||||
            //            //Debug.WriteLine($"获取所有箱子门状态出错,第{retryCount}次重试", exception);
 | 
			
		||||
            //            logger.Info($"获取所有箱子门状态出错,第{retryCount}次重试, 异常信息{exception}");
 | 
			
		||||
            //            Thread.Sleep(50);
 | 
			
		||||
            //            this.SetModusIpMaster();
 | 
			
		||||
            //            BoxOperate = false;
 | 
			
		||||
            //        }
 | 
			
		||||
            //        return flags;
 | 
			
		||||
            //    });
 | 
			
		||||
            bool successFlag = true;
 | 
			
		||||
            bool[] bools = new bool[18];
 | 
			
		||||
            int iCount = 0;
 | 
			
		||||
            do
 | 
			
		||||
            {
 | 
			
		||||
                try
 | 
			
		||||
                {
 | 
			
		||||
                    bool[] flags = new bool[18];
 | 
			
		||||
                    if (master == null)
 | 
			
		||||
                    {
 | 
			
		||||
                        this.Dispose();
 | 
			
		||||
                        //Debug.WriteLine($"获取所有箱子门状态出错,第{retryCount}次重试", exception);
 | 
			
		||||
                        logger.Info($"获取所有箱子门状态出错,第{retryCount}次重试, 异常信息{exception}");
 | 
			
		||||
                        Thread.Sleep(50);
 | 
			
		||||
                        this.SetModusIpMaster();
 | 
			
		||||
                        BoxOperate = false;
 | 
			
		||||
                        // return TimeSpan.FromSeconds (1);
 | 
			
		||||
                    }).Execute<bool[]>(() =>
 | 
			
		||||
                    }
 | 
			
		||||
                    var result = master.ReadInputRegisters(1, 0x0033, 2);
 | 
			
		||||
                    var r1 = Convert.ToString(((int)result[0] >> 14) | ((int)result[1] << 2), 2).PadLeft(18, '0');
 | 
			
		||||
                    var r2 = r1.ToCharArray();
 | 
			
		||||
                    logger.Info("r2=>" + string.Join(", ", r2));
 | 
			
		||||
                    //var r2=new char[18] { '0','0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0' };
 | 
			
		||||
                    for (int i = 0; i < 18; i++)
 | 
			
		||||
                    {
 | 
			
		||||
                        bool[] flags = new bool[18];
 | 
			
		||||
                        if(master==null)
 | 
			
		||||
                        {
 | 
			
		||||
                            this.SetModusIpMaster();
 | 
			
		||||
                        }
 | 
			
		||||
                        var result = master.ReadInputRegisters(1, 0x0033, 2);
 | 
			
		||||
                        var r1 = Convert.ToString(((int)result[0] >> 14) | ((int)result[1] << 2), 2).PadLeft(18, '0');
 | 
			
		||||
                        var r2 = r1.ToCharArray();
 | 
			
		||||
                        logger.Info("r2=>" + string.Join(", ", r2));
 | 
			
		||||
                        //var r2=new char[18] { '0','0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0' };
 | 
			
		||||
                        for (int i = 0; i < 18; i++)
 | 
			
		||||
                        {
 | 
			
		||||
                            flags[i] = r2[17 - i] == '1' ? true : false;
 | 
			
		||||
                        }
 | 
			
		||||
                        logger.Info($"获取所有箱子门状态返回:{string.Join(',', flags)};");
 | 
			
		||||
                        //bool allFalse = Array.TrueForAll(flags, b => b == false);
 | 
			
		||||
                        //if (!allFalse)
 | 
			
		||||
                        //{
 | 
			
		||||
                        //    Task.Delay(500);
 | 
			
		||||
                        //    Task.Factory.StartNew(async () =>
 | 
			
		||||
                        //    {
 | 
			
		||||
                        //        SpeakAsync("药箱已打开,请及时关闭");
 | 
			
		||||
                        //        await Task.Delay(15000);
 | 
			
		||||
                        //    });
 | 
			
		||||
                        //}
 | 
			
		||||
                        //else
 | 
			
		||||
                        //{
 | 
			
		||||
                        //    BoxOperate = false;
 | 
			
		||||
                        //}
 | 
			
		||||
                        
 | 
			
		||||
                        return flags;
 | 
			
		||||
                    });
 | 
			
		||||
            //}
 | 
			
		||||
                        bools[i] = r2[17 - i] == '1' ? true : false;
 | 
			
		||||
                    }
 | 
			
		||||
                    logger.Info($"获取所有箱子门状态返回:{string.Join(',', bools)};");
 | 
			
		||||
                    successFlag = true;
 | 
			
		||||
                }
 | 
			
		||||
                catch (Exception exception)
 | 
			
		||||
                {
 | 
			
		||||
                    if (iCount >= 3)
 | 
			
		||||
                        break;
 | 
			
		||||
                    iCount++;
 | 
			
		||||
                    successFlag = false;
 | 
			
		||||
                    this.Dispose();
 | 
			
		||||
                    //Debug.WriteLine($"获取所有箱子门状态出错,第{retryCount}次重试", exception);
 | 
			
		||||
                    logger.Info($"获取所有箱子门状态出错,第{iCount}次重试,异常信息{exception}");
 | 
			
		||||
                    Thread.Sleep(500);
 | 
			
		||||
                    this.SetModusIpMaster();
 | 
			
		||||
                    BoxOperate = false;
 | 
			
		||||
                }
 | 
			
		||||
            } while (!successFlag);
 | 
			
		||||
            return bools;
 | 
			
		||||
        }
 | 
			
		||||
        public bool OpenBoxDoor(int boxNum)
 | 
			
		||||
| 
						 | 
				
			
			@ -129,8 +156,8 @@ namespace DM_Weight.Port
 | 
			
		|||
                this.Dispose();
 | 
			
		||||
                //Debug.WriteLine($"打开箱子出错,第{retryCount}次重试", exception);
 | 
			
		||||
                logger.Info($"打开箱子出错,第{retryCount}次重试,异常信息{exception}");
 | 
			
		||||
                SpeakAsync("打开药箱网中连接失败,正在尝试重新打开");
 | 
			
		||||
                Thread.Sleep(50);
 | 
			
		||||
                //SpeakAsync("药箱连接失败,正在尝试重新打开");
 | 
			
		||||
                Thread.Sleep(500);
 | 
			
		||||
                this.SetModusIpMaster();
 | 
			
		||||
                //return TimeSpan.FromSeconds (1);
 | 
			
		||||
            })).Execute(() =>
 | 
			
		||||
| 
						 | 
				
			
			@ -170,7 +197,7 @@ namespace DM_Weight.Port
 | 
			
		|||
 | 
			
		||||
            if (clientSocket.Connected)
 | 
			
		||||
            {
 | 
			
		||||
                
 | 
			
		||||
 | 
			
		||||
                // 发送数据
 | 
			
		||||
                //byte[] msg = Encoding.ASCII.GetBytes("This is a test<EOF>");
 | 
			
		||||
                //clientSocket.Send(msg);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue