当前位置:网站首页>Lire Modbus TCP avec nmodbus

Lire Modbus TCP avec nmodbus

2022-01-15 02:10:07 La lave de Rodinia

Écris un NmodbusHelper

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
using Modbus.Device;
using thinger.cn;

namespace NmodbusHelper
{
    
    public class Modbus4TCP
    {
    
        private TcpClient tcpClient = null;
        private ModbusIpMaster master;

        #region Ouverture et fermetureSocket 
        public bool Connect(string ip, int port)
        {
    
            try
            {
    
                tcpClient = new TcpClient();
                tcpClient.Connect(IPAddress.Parse(ip), port);
                //IPEndPoint ie = new IPEndPoint(IPAddress.Parse(ip), port);
                //tcpClient.Connect(ie);
                master = ModbusIpMaster.CreateIp(tcpClient);
            }
            catch (Exception)
            {
    
                return false;
            }
            return true;
        }

        public bool Disconnect()
        {
    
            if (tcpClient != null)
            {
    
                tcpClient.Close();
                return true;
            }
            else
            {
    
                return false;
            }
        }
        #endregion

        public byte[] ReadKeepReg(string iAddress, int iLength)
        {
    
            ushort[] des = master.ReadHoldingRegisters(Convert.ToUInt16(iAddress), Convert.ToUInt16(iLength));// Interception automatique des bits de données 
            byte[] res = thinger.cn.DataConvertHelper.ByteArrayLib.GetByteArrayFromUShortArray(des);
            return res;
        }


    }
}

InwinformRéalisation intermédiaire Utiliser le Code de fonction 3Lire,Utilisation concomitanteUnsignedAvecfloat 2 Format de sortie

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using Modbus.Device;
using NmodbusHelper;
using thinger.cn;


namespace NmodbusPractice
{
    
    public partial class Form1 : Form
    {
    
        public Form1()
        {
    
            InitializeComponent();
        }
        string ip = "127.0.0.1";
        int port = 502;
        Modbus4TCP objTcp = new Modbus4TCP();
        private bool IsConnected = false;
        //private TcpClient tcpClient;
        
        private void btnConnect_Click(object sender, EventArgs e)
        {
    
            IsConnected = objTcp.Connect(ip, port);
            //IsConnected = Connect("127.0.0.1", port);
            Addinfo(IsConnected ? "Connexion réussie" : "La connexion a échoué");
        }

        private void btnRead_Click(object sender, EventArgs e)
        {
    
            byte[] res = objTcp.ReadKeepReg(this.txtVarAdd.Text, Convert.ToUInt16(this.numCount.Text));
            if (res != null)
            {
    
                //Produits Entier
                //ushort[] Res = thinger.cn.DataConvertHelper.UShortLib.GetUShortArrayFromByteArray(res); //UnsignedProduits
                
                //this.isInfo.Items.Insert(0, string.Join(" ",Res));


                //Produits Nombre de points flottants

                //float Dres1 = thinger.cn.DataConvertHelper.FloatLib.GetFloatFromByteArray(res);//FloatProduits
                //float Dres2 = thinger.cn.DataConvertHelper.FloatLib.GetFloatFromByteArray(res, 4);
                //string s1 = Convert.ToDouble(Dres1).ToString("f1");
                //string s2 = Convert.ToDouble(Dres2).ToString("f1");
                //this.isInfo.Items.Insert(0, s1+" "+s2);
                List<float> arrf = new List<float>();
                for (int i = 0; i < res.Length; i+=4)
                {
    
                    arrf.Add(thinger.cn.DataConvertHelper.FloatLib.GetFloatFromByteArray(res,i));
                }

                foreach (var item in arrf)
                {
    
                    Console.WriteLine(item);
                }
                Console.ReadKey();

            }
            else
            {
    
                Addinfo("La connexion a échoué");
            }
            
        }

        private void btnDisconnect_Click(object sender, EventArgs e)
        {
    
            objTcp.Disconnect();
        }

        private void Addinfo(string info)
        {
    
            this.isInfo.Items.Insert(
                0, DateTime.Now.ToString("HH:mm:ss") + " " + info + Environment.NewLine);
        }

        //public string AnalyseData_4xUnsigned(byte[] res)
        //{
    
        // ushort[] Res = thinger.cn.DataConvertHelper.UShortLib.GetUShortArrayFromByteArray(res);
        // return thinger.cn.DataConvertHelper.StringLib.GetStringFromByteArray()
        //}
    }
}

版权声明
本文为[La lave de Rodinia]所创,转载请带上原文链接,感谢
https://chowdera.com/2022/01/202201080558307174.html

随机推荐