示例#1
0
        /// <summary>
        /// Return portName with arduino Modbus slave
        /// </summary>
        /// <returns></returns>
        private static string portFinder()
        {
            var modbusProveder = new ModbusProvider();

            foreach (var __portName in SerialPort.GetPortNames())
            {
                ///Create config for check register
                ///reg 0 always return value = 200
                var regCheck = new ModbusConfig
                {
                    baudRate              = 9600,
                    device_address        = 1,
                    portName              = __portName,
                    register_read_address = 0
                };

                try
                {
                    if (modbusProveder.getValue <UInt16>(regCheck) == 200)
                    {
                        return(__portName);
                    }
                }
                catch (Exception)
                {
                    //Bad way
                }
            }
            return(null);
        }
示例#2
0
        private static async void updater()
        {
            var regLux = new ModbusConfig
            {
                baudRate              = 9600,
                device_address        = 1,
                portName              = _portName,
                register_read_address = 1
            };

            var modbusProveder = new ModbusProvider();

            //TODO: add cancelation Token
            while (true)
            {
                try
                {
                    var lux_mVoltage = modbusProveder.getValue <UInt16>(regLux);
                    var lux          = lux_mVoltage.ConvertToLux();

                    var bright = LuxToBright(lux);
                    var result = Brightness.SetBrightness(bright);

                    Console.SetCursorPosition(0, 0);
                    Console.WriteLine("{0}  lux  from {1} mV     ", lux, lux_mVoltage);
                    Console.WriteLine("{0}  brightness set", bright);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("No connect " + ex.Message);
                    await Task.Delay(1000);

                    Console.Clear();
                }
                await Task.Delay(1000);
            }
        }