Unity Modbus Tcp 通讯协议

unity Modbus Tcp/IP 通讯协议

提示:unity通过Modbus Tcp协议进行工控软件的交互
前段时间自己有个需求需要unity通过Modbus协议去读取数据,然后在网上找了很多博客实现方法很多,但是Modbus依赖的DLL文件再CSDN很多前辈都在收费搞得很是头大,进过几番周折才搞到Nmodbus4.dll这个程序集,为乐方便后来者直接给你们免费挂上,坚决杜绝那种有点东西就乱收费的现象。




废话不多说,直接上连接

一、NModbus4.DLL下载链接

链接:https://pan.baidu.com/s/1sRUmpzfzYlbHERdE8VW-Lg?pwd=zll8
提取码:zll8

二、讲dll导入Unity中

1.引入库 放置Assets下创建Plugins文件夹下

在这里插入图片描述

2.脚本中引用 Modbus.Device;

在这里插入图片描述


实现代码如下:

代码如下(示例):

using Modbus.Device;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// Modbus Tcp/IP
/// </summary>
public class Concent_ : MonoBehaviour
{
    public ModbusMaster modbusIpMaster;
    public TcpClient tcpClient;
    IPAddress Address = new IPAddress(new byte[] { 127, 0, 0, 1 }); 
    public int Port = 502;
    public bool Conen = false;
    public bool Reda_White;
    private ushort[] Udata = new ushort[] {0x03};
    private ushort star = 1;
    Thread mythread;
    public bool isconect = false;


    public Button Connect_Bt;
    public Button Read_Bt;
    public InputField Ip_;
    public InputField Port_;
    public Text Sta_;
    public Text Data_;
    // Start is called before the first frame update
    void Start()
    {
        Connect_Bt.onClick.AddListener(() =>
        {
            if (Ip_.textComponent.text=="")
            {
                Debug.Log(Ip_.textComponent.text + "     " + Port_.textComponent.text);
            }
            OpenConnect_(Ip_.textComponent.text, int.Parse(Port_.textComponent.text));
        });
        Read_Bt.onClick.AddListener(()=> {
            Data_.text = null;
            ushort[] AoData = modbusIpMaster.ReadHoldingRegisters(0x00, 0, 0x14);
            //string str = AoData.ToString();
            //Debug.Log(AoData.Length);
            foreach (var item in AoData)
            {
                //Debug.Log();
                Data_.text += "数据:" + item;
            }
        });
    }


    public void OpenConnect_(string ip,int port)
    {
        
        if (Connect(ip, port))
        {
            Debug.Log("连接成功");
            Sta_.text = "连接成功";
            Sta_.color = Color.green;
            Read_Bt.interactable = true;
        }
        else
        {
            Debug.Log("连接失败");
            Sta_.text = "连接失败";
            Sta_.color = Color.red;
        }
    }
    public bool Connect(string ip,int port)
    {
        try
        {
            tcpClient = new TcpClient(ip, port);

            tcpClient.SendTimeout = 1;
            modbusIpMaster = ModbusIpMaster.CreateIp(tcpClient);

            mythread = new Thread(WriteMessageFromClient);

            mythread.Start();
            Conen = true;
            return true;
        }
        catch(Exception ex)
        {
            tcpClient.Close();
            Debug.LogError(ex.Message);
            return false;
        }
    }
    public void WriteMessageFromClient()
    {
        while (Conen)
        {
            try
            {
                if (Reda_White)
                {
                    Write_jiChunQi(star,Udata);
                    Debug.Log("发送成功");
                }
                if (kuse)
                {
                    //READ HOLDING REGISTER
                    ushort[] msg = modbusIpMaster.ReadHoldingRegisters(0x01, 1, 0x01);
                }
            }
            catch
            {
                break;
            }
        }
        tcpClient.Close();
    }
    public void Write_jiChunQi(ushort star, ushort[] data)
    {
        modbusIpMaster.WriteMultipleRegisters(1, star, data);
    }
    private byte GetHex(string msg)
    {
        byte hex = Convert.ToByte(msg);
        return hex;
    }
    public int GetDexx(string msg)
    {
        int res = Convert.ToInt32(msg, 16);
        return res;
    }
    private void OnApplicationQuit()
    {
        tcpClient.Close();
    }
    public bool kuse = false;
    // Update is called once per frame
    void Update()
    {
        //if (Input.GetMouseButtonDown(0))
        //{
        //    modbusIpMaster.WriteMultipleRegisters(0x01, star, Udata);
        //    Debug.Log("发送成功");
        //}
        //if (Input.GetKeyDown(KeyCode.Space))
        //{
        //    ushort[] msg = modbusIpMaster.ReadInputRegisters(0x01, 1, 0x06);
        //    foreach (var item in msg)
        //    {
        //        Debug.Log(item);
        //    }
        //}
        //if (Input.GetKeyDown(KeyCode.W))
        //{
        //    ushort[] AoData = modbusIpMaster.ReadHoldingRegisters(0x00, 0, 0x14);
        //    //string str = AoData.ToString();
        //    //Debug.Log(AoData.Length);
        //    foreach (var item in AoData)
        //    {
        //        Debug.Log("读取到的数据:"+item);
        //    }
        //}
    }
}


后期调试工具Modbus Slave 以及Modbus Poll下载链接:

链接:https://pan.baidu.com/s/1RwLHzXKKxpgPAHm5O0So6Q?pwd=16t1 
提取码:16t1

   

以上就是今天要讲的内容,希望能帮到大家,同时呢也不要吝啬手中的赞哦!

猜你喜欢

转载自blog.csdn.net/cq786/article/details/124769619