• <blockquote id="eygoe"></blockquote>
  • <menu id="eygoe"></menu>
    <tt id="eygoe"></tt>
  • 1.打開visual studio 2012以及以上版本,在NuGet中搜NMobus程序包并安裝。

    2.在自己新建的C#項目中添加引用NModbus4.dll


    using System;

    using System.Collections.Generic;

    using System.IO.Ports;

    using System.Net.Sockets;

    using System.Text;

    using System.Threading.Tasks;

    using Modbus;

    using Modbus.Device;


    namespace MXModbus

    {

        public class ModbusTcpClient

        {

            private string serverIP = string.Empty;

            private int severPort = 0;

            private TcpClient tcpClient = null;

            private byte slaveID = 0;

            private ModbusIpMaster ipMaster = null;


            public ModbusTcpClient(string remoteServerIP, int remotePort)

            {

                serverIP = remoteServerIP;

                severPort = remotePort;

                tcpClient = new TcpClient();


                IAsyncResult asyncResult = tcpClient.BeginConnect(serverIP, severPort, null, null);

                asyncResult.AsyncWaitHandle.WaitOne(1000);

                if (!asyncResult.IsCompleted)

                {

                    Console.WriteLine("connect timeout in 1000ms");

                    return;

                }


                ipMaster = ModbusIpMaster.CreateIp(tcpClient);

                //ipMaster.Transport.ReadTimeout = 500;//讀取數據超時500ms

                //ipMaster.Transport.WriteTimeout = 500;//寫入數據超時500ms

                //ipMaster.Transport.Retries = 3;//重試次數

                //ipMaster.Transport.WaitToRetryMilliseconds = 1000;//重試間隔


            }



            public void ReadTempAndHum(int deviceID, out float temp, out float humidity)

            {

                slaveID = (byte)deviceID;

                ushort tempStartAddress = 0;

                ushort humStartAddress = 1;

                ushort[] holding_register1 = ipMaster.ReadHoldingRegisters((byte)slaveID, tempStartAddress, 1);

                ushort[] holding_register2 = ipMaster.ReadHoldingRegisters((byte)slaveID, humStartAddress, 1);


                temp = processTemp(holding_register1[0]);

                humidity = processHumidity(holding_register2[0]);


            }


            ~ModbusTcpClient()

            {

                ipMaster.Dispose();

                tcpClient.Close();

            }



            private float processTemp(ushort temp)

            {

                float temp1 = 0;

                if (temp >= 0 && temp <= 32767)

                {


                    temp1 = Convert.ToSingle(temp / 10.0);

                }

                //負數范圍0X8000-0XFFFF

                else if (temp >= 32768 && temp <= 65535)

                {

                    temp1 = Convert.ToSingle((temp- 65535) / 10.0);

                }

                return temp1;


            }

            private float processHumidity(ushort hum)

            {

                return Convert.ToSingle(hum / 10.0);


            }

        }

        

    }


    2018年12月29日

    溫濕度變送器 MODBUS RTU C# demo
    溫濕度變送器 MODBUS RTU java demo

    上一篇

    下一篇

    溫濕度變送器 MODBUS TCP client C# demo

    主要解決一些新手客戶詢問如何編程的問題。demo僅供學習參考用。如需商用,請參考相關的license。

    添加時間:

    米奇在线777在线精品视频,米奇影院888奇米色99在线,米奇777免费视频手机版