public void GetValues(ConnectionProperties connectionProperties)
        {
            modbusClient = connectionProperties.modbusClient;
            if (!modbusClient.Connected)
            {
                modbusClient.IPAddress = connectionProperties.ModbusTCPAddress;
                modbusClient.Port      = connectionProperties.Port;
                modbusClient.Connect();
            }
            foreach (FunctionProperties functionProperty in connectionProperties.FunctionPropertiesList)
            {
                switch (functionProperty.FunctionCodeRead)
                {
                case FunctionCodeRd.ReadCoils:
                    functionProperty.values = modbusClient.ReadCoils(functionProperty.StartingAdress, functionProperty.Quantity);
                    break;

                case FunctionCodeRd.ReadDiscreteInputs:
                    functionProperty.values = modbusClient.ReadDiscreteInputs(functionProperty.StartingAdress, functionProperty.Quantity);
                    break;

                case FunctionCodeRd.ReadHoldingRegisters:
                    functionProperty.values = modbusClient.ReadHoldingRegisters(functionProperty.StartingAdress, functionProperty.Quantity);
                    break;

                case FunctionCodeRd.ReadInputRegisters:
                    functionProperty.values = modbusClient.ReadInputRegisters(functionProperty.StartingAdress, functionProperty.Quantity);
                    break;

                default: break;
                }
            }
            if (valuesChanged != null)
            {
                valuesChanged(this);
            }
        }
Example #2
0
        private void ProcessData(object param)
        {
            while (!shouldStop)
            {
                ReadOrder readOrder = (ReadOrder)param;
                lock (lockProcessData)
                {
                    if (readOrder.FunctionCode == FunctionCode.ReadCoils)
                    {
                        bool[] value = modbusClient.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 = modbusClient.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 = modbusClient.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 = modbusClient.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);
            }
        }
Example #3
0
        private void ProcessData(object param)
        {
            while (!shouldStop)
            {
                try
                {
                    if (!mqttClient.IsConnected)
                    {
                        mqttClient = new uPLibrary.Networking.M2Mqtt.MqttClient(mqttBrokerAddress, mqttBrokerPort, false, null, null, uPLibrary.Networking.M2Mqtt.MqttSslProtocols.None);
                        string clientID = Guid.NewGuid().ToString();
                        if (MqttUserName == null || MqttPassword == null)
                        {
                            mqttClient.Connect(clientID);
                        }
                        else
                        {
                            mqttClient.Connect(clientID, MqttUserName, MqttPassword);
                        }
                    }
                }
                catch (Exception exc)
                {
                    if (!this.AutomaticReconnect)
                    {
                        throw exc;
                    }
                }
                ReadOrder readOrder = (ReadOrder)param;
                lock (lockProcessData)
                {
                    try
                    {
                        if (readOrder.FunctionCode == FunctionCode.ReadCoils)
                        {
                            bool[] value = modbusClient.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, readOrder.Retain[i]);
                                }
                                else if ((bool)readOrder.oldvalue[i] != value[i])
                                {
                                    mqttClient.Publish(readOrder.Topic[i], Encoding.UTF8.GetBytes(value[i].ToString()), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, readOrder.Retain[i]);
                                }
                                readOrder.oldvalue[i] = value[i];
                            }
                        }
                        if (readOrder.FunctionCode == FunctionCode.ReadDiscreteInputs)
                        {
                            bool[] value = modbusClient.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, readOrder.Retain[i]);
                                }
                                else if ((bool)readOrder.oldvalue[i] != value[i])
                                {
                                    mqttClient.Publish(readOrder.Topic[i], Encoding.UTF8.GetBytes(value[i].ToString()), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, readOrder.Retain[i]);
                                }
                                readOrder.oldvalue[i] = value[i];
                            }
                        }
                        if (readOrder.FunctionCode == FunctionCode.ReadHoldingRegisters)
                        {
                            int[] value = modbusClient.ReadHoldingRegisters(readOrder.StartingAddress, readOrder.Quantity);
                            for (int i = 0; i < value.Length; i++)
                            {
                                float scale = readOrder.Scale != null ? (readOrder.Scale[i] == 0) ? 1 : readOrder.Scale[i] : 1;
                                if (readOrder.oldvalue[i] == null)
                                {
                                    mqttClient.Publish(readOrder.Topic[i], (readOrder.Unit == null ? Encoding.UTF8.GetBytes(((float)value[i] * scale).ToString()) : Encoding.UTF8.GetBytes(((float)value[i] * scale) + " " + readOrder.Unit[i])), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, readOrder.Retain[i]);
                                    readOrder.oldvalue[i] = value[i];
                                }
                                else if (((int)readOrder.oldvalue[i] != value[i]) && (readOrder.Hysteresis != null ? ((value[i] < (int)readOrder.oldvalue[i] - (int)readOrder.Hysteresis[i]) | (value[i] > (int)readOrder.oldvalue[i] + (int)readOrder.Hysteresis[i])) : true))
                                {
                                    mqttClient.Publish(readOrder.Topic[i], (readOrder.Unit == null ? Encoding.UTF8.GetBytes(((float)value[i] * scale).ToString()) : Encoding.UTF8.GetBytes(((float)value[i] * scale).ToString() + " " + readOrder.Unit[i])), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, readOrder.Retain[i]);
                                    readOrder.oldvalue[i] = value[i];
                                }
                            }
                        }

                        if (readOrder.FunctionCode == FunctionCode.ReadInputRegisters)
                        {
                            int[] value = modbusClient.ReadInputRegisters(readOrder.StartingAddress, readOrder.Quantity);
                            for (int i = 0; i < value.Length; i++)
                            {
                                float scale = readOrder.Scale != null ? (readOrder.Scale[i] == 0) ? 1 : readOrder.Scale[i] : 1;
                                if (readOrder.oldvalue[i] == null)
                                {
                                    mqttClient.Publish(readOrder.Topic[i], (readOrder.Unit == null ? Encoding.UTF8.GetBytes(((float)value[i] * scale).ToString()) : Encoding.UTF8.GetBytes(((float)value[i] * scale).ToString() + " " + readOrder.Unit[i])), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, readOrder.Retain[i]);
                                    readOrder.oldvalue[i] = value[i];
                                }
                                else if (((int)readOrder.oldvalue[i] != value[i]) && (readOrder.Hysteresis != null ? ((value[i] < (int)readOrder.oldvalue[i] - (int)readOrder.Hysteresis[i]) | (value[i] > (int)readOrder.oldvalue[i] + (int)readOrder.Hysteresis[i])) : true))
                                {
                                    mqttClient.Publish(readOrder.Topic[i], (readOrder.Unit == null ? Encoding.UTF8.GetBytes(((float)value[i] * scale).ToString()) : Encoding.UTF8.GetBytes(((float)value[i] * scale).ToString() + " " + readOrder.Unit[i])), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, readOrder.Retain[i]);
                                    readOrder.oldvalue[i] = value[i];
                                }
                            }
                        }
                    }
                    catch (Exception exc)
                    {
                        modbusClient.Disconnect();
                        Thread.Sleep(2000);
                        if (!AutomaticReconnect)
                        {
                            throw exc;
                        }
                        if (!modbusClient.Connected)
                        {
                            try
                            {
                                modbusClient.Connect();
                            }
                            catch (Exception) { }
                        }
                    }
                }
                System.Threading.Thread.Sleep(readOrder.CylceTime);
            }
        }