Ejemplo n.º 1
0
        public void ss()
        {
            var s;
            var ss1 = modbusFactory.CreateRtuMaster(s);

            ss1.rea
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 写线圈
        /// </summary>
        /// <param name="slaveAddress">从站地址</param>
        /// <param name="registerAddress">寄存器地址</param>
        /// <param name="value">数据</param>
        public void WriteCoil(byte slaveAddress, ushort registerAddress, bool value)
        {
            using (SerialPort port = new SerialPort(SerialPortName))
            {
                //配置串口
                port.BaudRate = BaudRate;
                port.DataBits = 8;
                port.Parity   = Parity.Even;
                port.StopBits = StopBits.One;
                port.Open();

                //创建Modbus主机
                var adapter = new SerialPortAdapter(port);
                adapter.ReadTimeout  = ReadTimeout;
                adapter.WriteTimeout = WriteTimeout;
                var           factory = new ModbusFactory();
                IModbusMaster master  = factory.CreateRtuMaster(adapter);

                lock (_modbusLock)
                {
                    //写到寄存器
                    master.WriteSingleCoil(slaveAddress, registerAddress, value);
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 读寄存器(多个数据)
        /// </summary>
        /// <param name="slaveAddress">从站地址</param>
        /// <param name="registerAddress">寄存器地址</param>
        /// <param name="numberOfPoints">读取的数量</param>
        /// <param name="values">数据</param>
        public void ReadRegister(byte slaveAddress, ushort registerAddress, ushort numberOfPoints, out ushort[] values)
        {
            using (SerialPort port = new SerialPort(SerialPortName))
            {
                //配置串口
                port.BaudRate = BaudRate;
                port.DataBits = 8;
                port.Parity   = Parity.Even;
                port.StopBits = StopBits.One;
                port.Open();

                //创建Modbus主机
                var adapter = new SerialPortAdapter(port);
                adapter.ReadTimeout  = ReadTimeout;
                adapter.WriteTimeout = WriteTimeout;
                var           factory = new ModbusFactory();
                IModbusMaster master  = factory.CreateRtuMaster(adapter);

                lock (_modbusLock)
                {
                    //读寄存器
                    values = master.ReadHoldingRegisters(slaveAddress, registerAddress, numberOfPoints);
                }
            }
        }
Ejemplo n.º 4
0
        public static async Task SendStart(SerialPort port, ushort address, int value)
        {
            var adapter = new SerialPortAdapter(port);
            // create modbus master
            var factory = new ModbusFactory();

            IModbusMaster master = factory.CreateRtuMaster(adapter);

            byte slaveId = 1;

            ushort[] result = new ushort[] {
                BitConverter.ToUInt16(BitConverter.GetBytes(value), 0),
                BitConverter.ToUInt16(BitConverter.GetBytes(value), 2)
            };
            // write three registers
            await master.WriteMultipleRegistersAsync(slaveId, address, result);

            // 写入距离二
            await master.WriteSingleCoilAsync(slaveId, 2054, true);

            await master.WriteSingleCoilAsync(slaveId, 2054, false);

            // 启动
            await master.WriteSingleCoilAsync(slaveId, 2078, true);

            // read registers
            //var x = await master.ReadHoldingRegistersAsync(slaveId, address, 2);
            //System.Diagnostics.Debug.WriteLine(BitConverter.ToInt32(BitConverter.GetBytes(x[0]).Concat(BitConverter.GetBytes(x[1])).ToArray(), 0));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 读单个寄存器
        /// </summary>
        /// <param name="slaveAddress">从站地址</param>
        /// <param name="registerAddress">寄存器地址</param>
        /// <param name="value">数据</param>
        private void ModbusSerialRtuMasterReadRegister(byte slaveAddress, ushort registerAddress, out ushort value)
        {
            value = 0xFFFF;
            using (SerialPort port = new SerialPort(PrimarySerialPortName))
            {
                //配置串口
                port.BaudRate = SerialPortBaudRate;
                port.DataBits = 8;
                port.Parity   = Parity.None;
                port.StopBits = StopBits.One;
                port.Open();

                //创建Modbus主机
                var adapter = new SerialPortAdapter(port);
                adapter.ReadTimeout  = ReadTimeout;
                adapter.WriteTimeout = WriteTimeout;
                var           factory = new ModbusFactory();
                IModbusMaster master  = factory.CreateRtuMaster(adapter);

                lock (powerLock)
                {
                    //读寄存器
                    var values = master.ReadHoldingRegisters(slaveAddress, registerAddress, 1);
                    if (values?.Length >= 1)
                    {
                        value = values[0];
                    }
                }
            }
        }
Ejemplo n.º 6
0
        void MakeSureClientIsConnected()
        {
            if (_client != null && !_client.Connected)
            {
                _client.Dispose();
                _client = null;
                _adapter.Dispose();
                _adapter = null;
                _master.Dispose();
                _master = null;
            }

            if (_client == null)
            {
                _client  = new TcpClient(_configuration.Ip, _configuration.Port);
                _adapter = new TcpClientAdapter(_client);
                var factory = new ModbusFactory();
                if (_configuration.UseASCII)
                {
                    _master = factory.CreateAsciiMaster(_adapter);
                }
                else if (_configuration.Protocol == Protocol.Tcp)
                {
                    _master = factory.CreateMaster(_client);
                }
                else
                {
                    _master = factory.CreateRtuMaster(_adapter);
                }
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        ///     Simple Modbus serial RTU master write holding registers example.
        /// </summary>
        public static void ModbusSerialRtuMasterWriteRegisters()
        {
            using (SerialPort port = new SerialPort("COM1"))
            {
                // configure serial port
                port.BaudRate = 9600;
                port.DataBits = 8;
                port.Parity   = Parity.None;
                port.StopBits = StopBits.One;
                port.Open();

                var adapter = new SerialPortAdapter(port);
                // create modbus master
                var factory = new ModbusFactory();

                IModbusMaster master = factory.CreateRtuMaster(adapter);

                byte     slaveId      = 1;
                ushort   startAddress = 100;
                ushort[] registers    = new ushort[] { 1, 2, 3 };

                // write three registers
                master.WriteMultipleRegisters(slaveId, startAddress, registers);
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 写多个寄存器
        /// </summary>
        /// <param name="slaveAddress">从站地址</param>
        /// <param name="registerAddress">寄存器地址</param>
        /// <param name="data">数据</param>
        private void ModbusSerialRtuMasterWriteRegister(byte slaveAddress, ushort registerAddress, ushort[] data)
        {
            using (SerialPort port = new SerialPort(PrimarySerialPortName))
            {
                //配置串口
                port.BaudRate = SerialPortBaudRate;
                port.DataBits = 8;
                port.Parity   = Parity.None;
                port.StopBits = StopBits.One;
                port.Open();

                //创建Modbus主机
                var adapter = new SerialPortAdapter(port);
                adapter.ReadTimeout  = ReadTimeout;
                adapter.WriteTimeout = WriteTimeout;
                var           factory = new ModbusFactory();
                IModbusMaster master  = factory.CreateRtuMaster(adapter);

                lock (powerLock)
                {
                    //写到寄存器
                    master.WriteMultipleRegisters(slaveAddress, registerAddress, data);
                }
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// 读取线圈
        /// </summary>
        /// <param name="slaveAddress">从站地址</param>
        /// <param name="registerAddress">寄存器地址</param>
        /// <param name="value">数据</param>
        public void ReadCoil(byte slaveAddress, ushort registerAddress, out bool value)
        {
            value = false;
            using (SerialPort port = new SerialPort(SerialPortName))
            {
                //配置串口
                port.BaudRate = BaudRate;
                port.DataBits = 8;
                port.Parity   = Parity.Even;
                port.StopBits = StopBits.One;
                port.Open();

                //创建Modbus主机
                var adapter = new SerialPortAdapter(port);
                adapter.ReadTimeout  = ReadTimeout;
                adapter.WriteTimeout = WriteTimeout;
                var           factory = new ModbusFactory();
                IModbusMaster master  = factory.CreateRtuMaster(adapter);

                lock (_modbusLock)
                {
                    //读寄存器
                    var values = master.ReadCoils(slaveAddress, registerAddress, 1);
                    if (values?.Length >= 1)
                    {
                        value = values[0];
                    }
                }
            }
        }
Ejemplo n.º 10
0
        private void WriteRegisters(string portName)
        {
            try
            {
                using (var port = GetSerialPort(portName))
                {
                    var factory = new ModbusFactory();
                    IModbusSerialMaster master = factory.CreateRtuMaster(port);

                    master.WriteMultipleRegisters(_slaveID, 0, new ushort[]
                    {
                        (ushort)'M',
                        (ushort)'M',
                        (ushort)'r',
                        (ushort)DateTime.Now.Year,
                        (ushort)DateTime.Now.Month,
                        (ushort)DateTime.Now.Day,
                        (ushort)DateTime.Now.Hour,
                        (ushort)DateTime.Now.Minute,
                        (ushort)DateTime.Now.Second,
                        (ushort)CPU,
                        (ushort)GPU
                    });
                }
            }
            catch { }
        }
Ejemplo n.º 11
0
        private IModbusMaster CreateTcpMaster(ModbusFactory factory)
        {
            _tcpClient = new TcpClient(_configurationSettings.IpAddress, _configurationSettings.IpPort);
            IModbusMaster master = null;

            if (_configurationSettings.ConnectionType.CurrentType == ConnectionType.TcpIp)
            {
                master = factory.CreateMaster(_tcpClient);
            }
            if (_configurationSettings.ConnectionType.CurrentType == ConnectionType.RtuAsciiOverTcpIp)
            {
                var adapter = new TcpClientAdapter(_tcpClient);
                if (_configurationSettings.UseRtu)
                {
                    master = factory.CreateRtuMaster(adapter);
                }
                if (_configurationSettings.UseAscii)
                {
                    master = factory.CreateAsciiMaster(adapter);
                }
            }

            master.Transport.ReadTimeout = _configurationSettings.Timeout;
            return(master);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// 读多个寄存器
        /// </summary>
        /// <param name="slaveAddress">从站地址</param>
        /// <param name="registerAddress">寄存器地址</param>
        /// <param name="value">数据</param>
        private void ModbusSerialRtuMasterReadRegister(byte slaveAddress, ushort registerAddress, ushort numberOfPoints, out ushort[] data)
        {
            data = null;
            using (SerialPort port = new SerialPort(PrimarySerialPortName))
            {
                //配置串口
                port.BaudRate = SerialPortBaudRate;
                port.DataBits = 8;
                port.Parity   = Parity.None;
                port.StopBits = StopBits.One;
                port.Open();

                //创建Modbus主机
                var adapter = new SerialPortAdapter(port);
                adapter.ReadTimeout  = ReadTimeout;
                adapter.WriteTimeout = WriteTimeout;
                var           factory = new ModbusFactory();
                IModbusMaster master  = factory.CreateRtuMaster(adapter);

                lock (powerLock)
                {
                    //读寄存器
                    data = master.ReadHoldingRegisters(slaveAddress, registerAddress, numberOfPoints);
                }
            }
        }
Ejemplo n.º 13
0
        private IModbusMaster CreateSerialPortMaster(ModbusFactory factory)
        {
            IModbusMaster master = null;

            _serialPort = new SerialPort
            {
                PortName = _configurationSettings.SerialPort,
                BaudRate = _configurationSettings.BaudRate.Current,
                DataBits = _configurationSettings.DataBits,
                Parity   = _configurationSettings.Parity,
                StopBits = _configurationSettings.StopBits
            };
            _serialPort.Open();
            if (_configurationSettings.UseRtu)
            {
                master = factory.CreateRtuMaster(new SerialPortAdapter(_serialPort));
            }

            if (_configurationSettings.UseAscii)
            {
                master = factory.CreateAsciiMaster(new SerialPortAdapter(_serialPort));
            }

            master.Transport.ReadTimeout = _configurationSettings.Timeout;

            return(master);
        }
Ejemplo n.º 14
0
        static void Main(string[] args)
        {
            int port    = 502;
            var address = new IPAddress(new byte[] { 127, 0, 0, 1 });

            var factory = new ModbusFactory();

            using (var serialPort = new SerialPort("COM1"))
            {
                var adapter          = new SerialPortAdapter(serialPort);
                var master           = factory.CreateRtuMaster(adapter);
                var slaveTcpListener = new TcpListener(address, port);
                slaveTcpListener.Start();

                var tcpNetwork = factory.CreateSlaveNetwork(slaveTcpListener);
                var proxy1     = factory.CreateSlaveProxy(1, master);
                var proxy2     = factory.CreateSlaveProxy(2, master);
                tcpNetwork.AddSlave(proxy1);
                tcpNetwork.AddSlave(proxy2);

                var tcpClient    = new TcpClient(address.ToString(), port);
                var secondMaster = factory.CreateMaster(tcpClient);

                ushort[] holdingRegs = secondMaster.ReadHoldingRegisters(1, 1, 2);
            }
        }
Ejemplo n.º 15
0
        public ushort ModbusSerialRtuMasterReadRegisters(ushort startAddress = 1, ushort numRegisters = 5, byte slaveId = 1)
        {
            using (SerialPort port = new SerialPort(PrimarySerialPortName))
            {
                // configure serial port
                port.BaudRate = _baudRate;
                port.DataBits = _dataBits;
                port.Parity   = _parity;
                port.StopBits = _stopBits;
                port.Open();
                var t = port.ReadTimeout;

                var factory = new ModbusFactory();
                IModbusSerialMaster master = factory.CreateRtuMaster(port);
                master.Transport.ReadTimeout = 1000;

                byte   _slaveId      = slaveId;
                ushort _startAddress = startAddress;//1;
                ushort _numRegisters = numRegisters;

                // read five registers
                ushort[] registers = master.ReadHoldingRegisters(_slaveId, _startAddress, _numRegisters);

                var currentThread = Thread.CurrentThread.ManagedThreadId;
                port.Close();
                return(registers[0]);
            }

            // output:
            // Register 1=0
            // Register 2=0
            // Register 3=0
            // Register 4=0
            // Register 5=0
        }
Ejemplo n.º 16
0
        /// <summary>
        ///      Modbus serial RTU master write holding registers
        /// </summary>
        public void ModbusSerialRtuMasterWriteRegisters(byte slaveId, ushort startAddress, ushort registerValue)
        {
            lock (_locker)
            {
                using (SerialPort port = new SerialPort(PrimarySerialPortName))
                {
                    // configure serial port
                    port.BaudRate = _baudRate;
                    port.DataBits = _dataBits;
                    port.Parity   = _parity;
                    port.StopBits = _stopBits;
                    port.Open();

                    var           factory = new ModbusFactory();
                    IModbusMaster master  = factory.CreateRtuMaster(port);

                    byte   _slaveId      = slaveId;
                    ushort _startAddress = startAddress;

                    // write three registers

                    master.WriteSingleRegister(_slaveId, _startAddress, registerValue);
                }
            }
        }
Ejemplo n.º 17
0
        public IModbusMaster Create()
        {
            var factory = new ModbusFactory();

            switch (_connection.Type)
            {
            case ConnectionType.Rtu:
            {
                var serialPort = CreateAndOpenSerialPort(_connection);

                var transport = new SerialPortAdapter(serialPort);

                return(factory.CreateRtuMaster(transport));
            }

            case ConnectionType.Ascii:
            {
                var serialPort = CreateAndOpenSerialPort(_connection);

                var transport = new SerialPortAdapter(serialPort);

                return(factory.CreateRtuMaster(transport));
            }

            case ConnectionType.Tcp:
            {
                var tcpClient = new TcpClient();

                if (!tcpClient.ConnectAsync(_connection.HostName, _connection.Port).Wait(TcpConnectionTimeoutMilliseconds))
                {
                    throw new TimeoutException($"Timed out trying to connect to TCP Modbus device at {_connection.HostName}:{_connection.Port}");
                }

                return(factory.CreateMaster(tcpClient));
            }

            case ConnectionType.Udp:
            {
                var udpClient = new UdpClient();

                return(factory.CreateMaster(udpClient));
            }

            default:
                throw new ArgumentException($"{nameof(_connection.Type)} had an unepected value '{_connection.Type}'.");
            }
        }
Ejemplo n.º 18
0
        public ModbusRtuMaster(IStreamResource streamResource, int writeTimeout, int readTimeout, int retries)
        {
            var modbusFactory = new ModbusFactory();

            ResetNextAllowedSendTime();

            master = modbusFactory.CreateRtuMaster(streamResource);
            master.Transport.WriteTimeout = writeTimeout;
            master.Transport.ReadTimeout  = readTimeout;
            master.Transport.Retries      = retries;
        }
Ejemplo n.º 19
0
 public void Port_choised(string arg)
 {
     if (port.PortName != null)
     {
         try
         {
             if (port.PortName == arg)
             {
                 if (!port.IsOpen)
                 {
                     port.Open();
                 }
                 output += "Port " + port.PortName + " opened" + "\n";
                 RaisePropertyChanged("Terminal");
             }
             else
             {
                 if (port.IsOpen)
                 {
                     port.Close();
                 }
                 port.PortName = arg;
                 port.Open();
             }
             _port_state = port.PortName + " opened";
             if (baudrate != 0)
             {
                 port.BaudRate = baudrate;
             }
             port.Handshake = Handshake.None;
             adapter        = new SerialPortAdapter(port);
             factory        = new ModbusFactory();
             master         = factory.CreateRtuMaster(adapter);
             master.Transport.ReadTimeout  = 300;
             master.Transport.WriteTimeout = 1000;
             output += "Master created " + port.BaudRate.ToString() + "\n";
             RaisePropertyChanged("Terminal");
         }
         catch (System.IO.IOException)
         {
             Refresh_List();
             _port_state = arg + " was lost";
             output     += "Master create error: " + "\n";
             RaisePropertyChanged("Terminal");
         }
     }
     else
     {
         output += "Port not choised" + "\n";
         RaisePropertyChanged("Terminal");
     }
     RaisePropertyChanged("Port_state");
 }
Ejemplo n.º 20
0
        internal SerialMasterContext(SerialPort serialPort, int readTimeout, int writeTimeout)
        {
            var factory = new ModbusFactory();

            var adapter = new SerialPortAdapter(serialPort);

            _serialPort = serialPort;
            _master     = factory.CreateRtuMaster(adapter);
            _master.Transport.ReadTimeout  = readTimeout;
            _master.Transport.WriteTimeout = writeTimeout;
            _master.Transport.Retries      = 0;
        }
Ejemplo n.º 21
0
        public static async Task SendBack(SerialPort port, ushort address)
        {
            var adapter = new SerialPortAdapter(port);
            // create modbus master
            var factory = new ModbusFactory();

            IModbusMaster master = factory.CreateRtuMaster(adapter);

            byte slaveId = 1;
            await master.WriteSingleCoilAsync(slaveId, address, true);

            await master.WriteSingleCoilAsync(slaveId, address, false);
        }
Ejemplo n.º 22
0
        private ushort[] ReadGadgetName(string portName)
        {
            try
            {
                using (var port = GetSerialPort(portName))
                {
                    var factory = new ModbusFactory();
                    IModbusSerialMaster master = factory.CreateRtuMaster(port);

                    return(master.ReadHoldingRegisters(_slaveID, 0, 3));
                }
            }
            catch
            {
                return(new ushort[3]);
            }
        }
Ejemplo n.º 23
0
    /// <summary>
    /// 打開指定的 Serial Port,並實例一個 RTU Master
    /// </summary>
    public void OpenSerialPort()
    {
        try
        {
            _port.Open();
            Debug.Log("<color=green>Connect to </color>" + _portName);
        }
        catch (Exception e)
        {
            Debug.Log(e.ToString());
        }


        var factory = new ModbusFactory();

        master = factory.CreateRtuMaster(_port);
    }
Ejemplo n.º 24
0
        private async void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            if (_master == null)
            {
                var port = await GetModBusUwP();

                if (port != null)
                {
                    var modbusFactory = new ModbusFactory();
                    _master = modbusFactory.CreateRtuMaster(port);
                    _master.Transport.ReadTimeout             = 1000;
                    _master.Transport.WriteTimeout            = 1000;
                    _master.Transport.Retries                 = 3;
                    _master.Transport.WaitToRetryMilliseconds = 250;
                }
            }

            if (_master == null)
            {
                InfoTxt.Text = "No ModBus!";
                return;
            }

            var r1 = await _master.ReadInputRegistersAsync(1, 35061, 2);

            try
            {
                var r = await _master.ReadInputRegistersAsync(9, 35061, 2);

                UInt32 value = (UInt32)r[0] << 16 | r[1];
                Debug.WriteLine($"WeatherStation Operating time: {value}s");
            }
            catch (Exception exception)
            {
                ErrorTxt.Text = "Timeout!";
            }

            for (int i = 0; i < 10000; i++)
            {
                var ra = await _master.ReadInputRegistersAsync(1, 35001, 64);

                Debug.WriteLine($"{i+1} Bytes Read: {ra.Length}");
                InfoTxt.Text = $"{i + 1} Bytes Read: {ra.Length}";
            }
        }
Ejemplo n.º 25
0
        static void Main(string[] args)
        {
            using (SerialPort port = new SerialPort("COM4"))
            {
                // configure serial port
                port.BaudRate = 9600;
                port.DataBits = 8;
                port.Parity   = Parity.None;
                port.StopBits = StopBits.One;
                port.Open();

                var           factory = new ModbusFactory();
                IModbusMaster master  = factory.CreateRtuMaster(port);

                byte   slaveId      = 4;
                ushort startAddress = 48;
                bool[] registers    = new bool[] { true, true, false, true };

                var state    = master.ReadHoldingRegisters(slaveId, 7996, 1);
                var commando = master.ReadHoldingRegisters(slaveId, 2006, 1);
                master.WriteSingleRegister(slaveId, 2006, 0);


                var data = master.ReadHoldingRegisters(slaveId, 8000, 1);

                do
                {
                    data = master.ReadHoldingRegisters(slaveId, 8000, 1);
                    Console.WriteLine(data[0].ToString("X"));
                    Thread.Sleep(1000);
                    if (Console.KeyAvailable)
                    {
                        break;
                    }
                    //if (data[0] > 5)
                    //    master.WriteSingleCoil(slaveId, 48, false);
                    //else
                    //    master.WriteSingleCoil(slaveId, 48, true);
                } while (true);
            }

            Console.WriteLine("Any key to exit!");
            Console.ReadKey();
        }
Ejemplo n.º 26
0
        public static void ModbusSerialRtuMasterWriteRegisters(byte slaveID, ushort slaveaddress, int result)
        {
            using (SerialPort port = new SerialPort(Linktoarduino))
            {
                port.BaudRate    = 9600;
                port.DataBits    = 8;
                port.Parity      = Parity.None;
                port.StopBits    = StopBits.One;
                port.ReadTimeout = 500;
                port.Open();

                var           factory = new ModbusFactory();
                IModbusMaster master  = factory.CreateRtuMaster(port);

                ushort[] value = { 1 };
                value[0] = (ushort)result;
                master.WriteMultipleRegisters(slaveID, slaveaddress, value);
            }
        }
Ejemplo n.º 27
0
        private void OpenComport_Aduino()
        {
            if (cbxComport.Text == "" || cbxbaurate.Text == "")
            {
                MessageBox.Show("select");
            }
            else
            {
                serialPort1.PortName = cbxComport.Text;
                serialPort1.BaudRate = Convert.ToInt32(cbxbaurate.Text);
                serialPort1.DataBits = Convert.ToInt32(cbxdatabit.Text);
                serialPort1.StopBits = (StopBits)Enum.Parse(typeof(StopBits), cbxstopbit.Text);
                serialPort1.Parity   = (Parity)Enum.Parse(typeof(Parity), cbxparity.Text);
                //serialPort1.ReadTimeout = 1000;

                serialPort1.Open();
                var factory = new ModbusFactory();
                master = factory.CreateRtuMaster(serialPort1);
            }
        }
Ejemplo n.º 28
0
        internal async Task ConnectBySerialAsync(string comPort)
        {
            serialPort = new SerialPort()
            {
                PortName     = comPort,
                BaudRate     = 115200,
                DataBits     = 8,
                Parity       = Parity.Odd,
                StopBits     = StopBits.One,
                ReadTimeout  = 500,
                WriteTimeout = 500
            };
            serialPort.Open();

            var factory = new ModbusFactory();

            master = factory.CreateRtuMaster(serialPort);

            await master.ReadInputRegistersAsync(SLAVE_ADDRESS, 0, 1);
        }
Ejemplo n.º 29
0
        private void WriteRegisters(Dictionary <string, float> sensorData)
        {
            try
            {
                using (var port = GetSerialPort())
                {
                    var factory = new ModbusFactory();
                    IModbusSerialMaster master = factory.CreateRtuMaster(port);

                    master.WriteMultipleRegisters(_slaveID, 0, new ushort[]
                    {
                        (ushort)'M',
                        (ushort)'M',
                        (ushort)'r',
                        (ushort)DateTime.Now.Year,
                        (ushort)DateTime.Now.Month,
                        (ushort)DateTime.Now.Day,
                        (ushort)DateTime.Now.Hour,
                        (ushort)DateTime.Now.Minute,
                        (ushort)DateTime.Now.Second,
                        (ushort)(sensorData.TryGetValue(_cpuSensorName, out var cpuValue)?cpuValue: 0f),
                        (ushort)(sensorData.TryGetValue(_gpuSensorName, out var gpuValue)?gpuValue: 0f)
                    });
                }
Ejemplo n.º 30
0
        public mbMaster(SerialPort port)
        {
            var factory = new ModbusFactory();

            mMaster = factory.CreateRtuMaster(port);
        }