Exemple #1
0
        private void Connect()
        {
            try
            {
                Serial?.Close();
                Thread.Sleep(50);

                Serial = new SerialPort(PortName)
                {
                    BaudRate = 9600,
                    DataBits = 8,
                    Parity   = Parity.None,
                    StopBits = StopBits.One
                };
                Serial.Open();

                Modbus = ModbusSerialMaster.CreateRtu(Serial);
                Modbus.Transport.Retries     = 2;
                Modbus.Transport.ReadTimeout = 100;
                Modbus.ReadInputRegisters(1, 0, 4);

                ModbusDeviceStates.Fire(TemperatureDeviceEdge.ConnectionValid);
            }
            catch
            {
                Serial?.Close();
                Thread.Sleep(50);
                ModbusDeviceStates.Fire(TemperatureDeviceEdge.ConnectionInvalid);
            }
        }
Exemple #2
0
        public bool StopRead()
        {
            if (!ModbusDeviceStates.CanFire(TemperatureDeviceEdge.ReadStop))
            {
                return(false);
            }

            IsReading = false;
            ModbusDeviceStates.Fire(TemperatureDeviceEdge.ReadStop);
            return(true);
        }
Exemple #3
0
        private async Task FindPortNameAsync()
        {
            PortName = string.Empty;
            PortName = await ModbusDetector.DetectAsync(new ModbusSettings { NumberOfPoints = 4,
                                                                             SlaveAdress    = 1,
                                                                             StartAdress    = 0 });

            if (PortName == string.Empty)
            {
                ModbusDeviceStates.Fire(TemperatureDeviceEdge.PortNameNotFounded);
            }
            else
            {
                ModbusDeviceStates.Fire(TemperatureDeviceEdge.PortNameFounded);
            }
        }
Exemple #4
0
        private async Task ReadAsync()
        {
            try
            {
                ushort[] data = await Modbus.ReadInputRegistersAsync(1, 0, 6);

                var temperature = BitConverter.ToSingle(BitConverter.GetBytes(data[0])
                                                        .Concat(BitConverter.GetBytes(data[1]))
                                                        .ToArray(), 0);
                var humidity = BitConverter.ToSingle(BitConverter.GetBytes(data[2])
                                                     .Concat(BitConverter.GetBytes(data[3]))
                                                     .ToArray(), 0);
                var pressure = BitConverter.ToSingle(BitConverter.GetBytes(data[4])
                                                     .Concat(BitConverter.GetBytes(data[5]))
                                                     .ToArray(), 0);
                await Task.Run(() =>
                {
                    OnTemperatureRead?.Invoke(this, new ReadPacket <float> {
                        Value = temperature, Time = DateTime.Now
                    });
                    OnHumidityRead?.Invoke(this, new ReadPacket <float> {
                        Value = humidity, Time = DateTime.Now
                    });
                    OnPressureRead?.Invoke(this, new ReadPacket <float> {
                        Value = (pressure * 133.322f) / 1000.0f, Time = DateTime.Now
                    });
                });

                if (IsReading && ModbusDeviceStates.CanFire(TemperatureDeviceEdge.ReadSuccess))
                {
                    ModbusDeviceStates.Fire(TemperatureDeviceEdge.ReadSuccess);
                }

                await Task.Run(() => Thread.Sleep(1000));
            }
            catch
            {
                if (ModbusDeviceStates.CanFire(TemperatureDeviceEdge.ReadError))
                {
                    ModbusDeviceStates.Fire(TemperatureDeviceEdge.ReadError);
                }
            }
        }
Exemple #5
0
 public void ToIdle()
 {
     ModbusDeviceStates.Fire(TemperatureDeviceEdge.ToIdle);
 }