unity连接modbus需要有NModbus4.dll文件

文件地址: (暂时还没上传完毕,上传完毕之后链接上,)

1.配置一个modbus主站,

2.unity用来连接

下面直接上代码

 

Unity连接ModbusTcp发送读取寄存器_进制Unity连接ModbusTcp发送读取寄存器_进制_02
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;


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 = false;
    private ushort[] udata = new ushort[]{0x03};
    private ushort star=1;
    Thread mythread;
    //Thread youthread;
    public bool isconect = false;
    // Start is called before the first frame update
    void Start()
    {
        if (Connect (address.ToString (), port))
        {
            Debug.Log("连接成功");
        }
        else
        {
            Debug.Log("连接失败");
        }
    }
    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)
                {
                 ushort [] msg=  modbusIpMaster.ReadHoldingRegisters(0x01,1, 0x01);                  
                }               
            }
            catch
            {
                break;
            }
        }
        tcpClient.Close();
    }
    public void Write_jiChunQi(ushort star,ushort[]data)
    {
        modbusIpMaster.WriteMultipleRegisters(1,star, data);
        
    }   
    /// 10进制转16进制 
    private byte GetHex(string msg)
    {
        byte hex = Convert.ToByte(msg);
        return hex;
    }
    ///16进制转10进制 
    public int GetDex(string msg)
    {
        int res = Convert.ToInt32(msg,16);
        return res;
    }
    //退出的时候关闭连接
    private void OnApplicationQuit()
    {
        tcpClient.Close();
    }
    public bool kuse = false;
    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            //WriteMultipleRegisters(设备地址,寄存器起始地址, 要写入的值)  
            modbusIpMaster.WriteMultipleRegisters(0x01,star,udata);
            Debug.Log("发送成功");
        }
        if (Input.GetKeyDown (KeyCode.Space))
        {
            //ReadHoldingRegisters(设备地址, 寄存器起始地址, 读取数量);  读取多个寄存器
            ushort[] msg = modbusIpMaster.ReadHoldingRegisters(0x01, 1, 0x06);
            foreach (var item in msg)
            {
                Debug.Log(item);
            }                                 
        }
    }
   
}
Modbus连接