public void AddReadOrder(FunctionCode functionCode, int quantity, int startingAddress, int cycleTime)
        {
            ReadOrder readOrder = new ReadOrder();

            readOrder.CylceTime       = cycleTime;
            readOrder.FunctionCode    = functionCode;
            readOrder.Quantity        = quantity;
            readOrder.StartingAddress = startingAddress;
            readOrder.Topic           = new string[quantity];
            readOrder.oldvalue        = new object[quantity];
            for (int i = 0; i < quantity; i++)
            {
                if (functionCode == FunctionCode.ReadCoils)
                {
                    readOrder.Topic[i] = mqttRootTopic + "/coils/" + (i + readOrder.StartingAddress).ToString();
                }
                if (functionCode == FunctionCode.ReadDiscreteInputs)
                {
                    readOrder.Topic[i] = mqttRootTopic + "/discreteinputs/" + (i + readOrder.StartingAddress).ToString();
                }
                if (functionCode == FunctionCode.ReadHoldingRegisters)
                {
                    readOrder.Topic[i] = mqttRootTopic + "/holdingregisters/" + (i + readOrder.StartingAddress).ToString();
                }
                if (functionCode == FunctionCode.ReadInputRegisters)
                {
                    readOrder.Topic[i] = mqttRootTopic + "/inputregisters/" + (i + readOrder.StartingAddress).ToString();
                }
            }
            readOrders.Add(readOrder);
        }
 public void AddReadOrder(ReadOrder readOrder)
 {
     if (readOrder.FunctionCode == 0)
     {
         throw new ArgumentOutOfRangeException("FunctionCode must be initialized");
     }
     if (readOrder.Quantity == 0)
     {
         throw new ArgumentOutOfRangeException("Quantity cannot be 0");
     }
     if (readOrder.CylceTime == 0)
     {
         readOrder.CylceTime = 500;
     }
     if (readOrder.Topic == null)
     {
         readOrder.Topic = new string[readOrder.Quantity];
         for (int i = 0; i < readOrder.Quantity; i++)
         {
             if (readOrder.FunctionCode == FunctionCode.ReadCoils)
             {
                 readOrder.Topic[i] = mqttRootTopic + "/coils/" + (i + readOrder.StartingAddress).ToString();
             }
             if (readOrder.FunctionCode == FunctionCode.ReadDiscreteInputs)
             {
                 readOrder.Topic[i] = mqttRootTopic + "/discreteinputs/" + (i + readOrder.StartingAddress).ToString();
             }
             if (readOrder.FunctionCode == FunctionCode.ReadHoldingRegisters)
             {
                 readOrder.Topic[i] = mqttRootTopic + "/holdingregisters/" + (i + readOrder.StartingAddress).ToString();
             }
             if (readOrder.FunctionCode == FunctionCode.ReadInputRegisters)
             {
                 readOrder.Topic[i] = mqttRootTopic + "/inputregisters/" + (i + readOrder.StartingAddress).ToString();
             }
         }
     }
     readOrders.Add(readOrder);
 }
        private void ProcessData(object param)
        {
            while (!shouldStop)
            {
                ReadOrder readOrder = (ReadOrder)param;
                if (readOrder.FunctionCode == FunctionCode.ReadCoils)
                {
                    bool[] value = this.ReadCoils(readOrder.StartingAddress, readOrder.Quantity);
                    for (int i = 0; i < value.Length; i++)
                    {
                        if (readOrder.oldvalue[i] == null)
                        {
                            mqttClient.Publish(readOrder.Topic[i], Encoding.UTF8.GetBytes(value[i].ToString()), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
                        }
                        else if ((bool)readOrder.oldvalue[i] != value[i])
                        {
                            mqttClient.Publish(readOrder.Topic[i], Encoding.UTF8.GetBytes(value[i].ToString()), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
                        }
                        readOrder.oldvalue[i] = value[i];
                    }
                }
                if (readOrder.FunctionCode == FunctionCode.ReadDiscreteInputs)
                {
                    bool[] value = this.ReadDiscreteInputs(readOrder.StartingAddress, readOrder.Quantity);
                    for (int i = 0; i < value.Length; i++)
                    {
                        if (readOrder.oldvalue[i] == null)
                        {
                            mqttClient.Publish(readOrder.Topic[i], Encoding.UTF8.GetBytes(value[i].ToString()), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
                        }
                        else if ((bool)readOrder.oldvalue[i] != value[i])
                        {
                            mqttClient.Publish(readOrder.Topic[i], Encoding.UTF8.GetBytes(value[i].ToString()), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
                        }
                        readOrder.oldvalue[i] = value[i];
                    }
                }
                if (readOrder.FunctionCode == FunctionCode.ReadHoldingRegisters)
                {
                    int[] value = this.ReadHoldingRegisters(readOrder.StartingAddress, readOrder.Quantity);
                    for (int i = 0; i < value.Length; i++)
                    {
                        if (readOrder.oldvalue[i] == null)
                        {
                            mqttClient.Publish(readOrder.Topic[i], Encoding.UTF8.GetBytes(value[i].ToString()), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
                        }
                        else if ((int)readOrder.oldvalue[i] != value[i])
                        {
                            mqttClient.Publish(readOrder.Topic[i], Encoding.UTF8.GetBytes(value[i].ToString()), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
                        }
                        readOrder.oldvalue[i] = value[i];
                    }
                }

                if (readOrder.FunctionCode == FunctionCode.ReadInputRegisters)
                {
                    int[] value = this.ReadInputRegisters(readOrder.StartingAddress, readOrder.Quantity);
                    for (int i = 0; i < value.Length; i++)
                    {
                        if (readOrder.oldvalue[i] == null)
                        {
                            mqttClient.Publish(readOrder.Topic[i], Encoding.UTF8.GetBytes(value[i].ToString()), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
                        }
                        else if ((int)readOrder.oldvalue[i] != value[i])
                        {
                            mqttClient.Publish(readOrder.Topic[i], Encoding.UTF8.GetBytes(value[i].ToString()), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
                        }
                        readOrder.oldvalue[i] = value[i];
                    }
                }
                System.Threading.Thread.Sleep(readOrder.CylceTime);
            }
        }