Exemplo n.º 1
0
 /// <summary>
 /// 执行Modbus请求&回去返回数据
 /// </summary>
 /// <param name="bs">Modbus命令</param>
 /// <param name="receiveDL">处理返回数据方法</param>
 /// <returns>返回操作结果数据</returns>
 public void Send(byte[] bs, ReturnReceive returnReceive)
 {
     if (Client != null && Client.Connected && IsSuccess)
     {
         //new Thread(async () =>
         //{
         try
         {
             Client.Send(bs);
             Client.Receive(ReviceByte);
             returnReceive?.Invoke(ReviceByte);
             //returnReceive?.Invoke(new CallbackData { Bytes = ReviceByte });
             //if (returnReceive != null && ReviceState)
             //{
             //    Client.Receive(ReviceByte);
             //}
             //Client.Send(bs);
             //if (returnReceive != null)
             //{
             //    Client.Receive(ReviceByte);
             //    //string receiveStr = Encoding.ASCII.GetString(ReviceByte, 0, bytesRec);
             //    returnReceive?.Invoke(new CallbackData { Bytes = ReviceByte });
             //    ReviceState = false;
             //}
             //else
             //{
             //    ReviceState = true;
             //}
         }
         catch (SocketException ex)
         {
             IsSuccess = false;
             ModbusLinkError?.Invoke(this.Name, $"{Name}请求错误--{ex.Message}");
         }
         //}).Start();
     }
     else
     {
         if (Client == null || !Client.Connected)
         {
             IsSuccess = false;
         }
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// 创建Modbus实例构造函数
        /// </summary>
        /// <param name="Ip">Modbus Ip</param>
        /// <param name="Port">端口号</param>
        /// <param name="Name">名称</param>
        public ModbusSocket(string Ip, int Port, string Name, Boolean initState)
        {
            this.Ip        = Ip;
            this.Port      = Port;
            this.Name      = Name;
            this.InitState = initState;
            Link();

            Task.Run(() =>
            {
                while (true)
                {
                    if (!IsSuccess)
                    {
                        Console.WriteLine(this.Ip);
                        Client?.Close();
                        Client = null;
                        ModbusLinkError?.Invoke(this.Name, $"{Name}--3秒钟后重新链接");
                        Thread.Sleep(3000);
                        ModbusLinkError?.Invoke(this.Name, $"{Name}--重新链接...");
                        Link();
                    }
                    else
                    {
                        if (listSands.Count > 0)
                        {
                            var item = listSands?[0];
                            if (item != null)
                            {
                                //ModbusLink(this.Name, $"{Name}--{item.CommandData.Str}--{listSands.Count}--{socketId}");
                                Send(item.CommandData.Bytes, item.RR);
                                listSands.Remove(item);
                            }
                        }
                    }
                }
            });
        }