相关文章推荐
千杯不醉的西装  ·  installation - Visual ...·  1 年前    · 
很酷的橡皮擦  ·  COR_GC_REFERENCE ...·  1 年前    · 
痴情的雪糕  ·  OpenGL中的FBO_gl_aux0_pl ...·  1 年前    · 

废话不多说,直接上连接

一、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; Debug.Log("连接失败"); Sta_.text = "连接失败"; Sta_.color = Color.red; public bool Connect(string ip,int port) 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) 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 以上就是今天要讲的内容,希望能帮到大家,同时呢也不要吝啬手中的赞哦! unity Modbus Tcp/IP 通讯协议提示:unity通过Modbus Tcp协议进行工控软件的交互前段时间自己有个需求需要unity通过Modbus协议去读取数据,然后在网上找了很多博客实现方法很多,但是Modbus依赖的DLL文件再CSDN很多前辈都在收费搞得很是头大,进过几番周折才搞到Nmodbus4.dll这个程序集,为乐方便后来者直接给你们免费挂上,坚决杜绝那种有点东西就乱收费的现象。文章目录unity Modbus Tcp/IP 通讯协议一、NModbus4.DLL下载链接
1. Modbus Tcp 协议: ModBus Tcp 是基于 TCP /IP的报文协议,采用主\从方式通信,但是主从之间的端口是固定的:502 ModBus 地址:由5位数字组成(PS:40001-49999表示HoldingRegister),包括起始数据类型代号,以及后面的偏移地址 2. 实验目的: 实现主机\从机对任意线圈和寄存器的写入和读取。 这里用到的Dll名称是:N Modbus 4。 可以在NuGet或者GetHub上下载到。 3. 主机端:Slave // Modbus TCP
最近做个项目,需要PC机通过局域网读取多台威纶人机中的数据,于是找到了N modbus 库。 环境:win10+vs2010 C# N modbus 下载:这里 step 1.将N modbus 中的 modbus 作为一个类项目直接加载到我们自己的项目中,直接右键编译,会出错,因为它还关联了其它一些库。 step2 :将目录下tools复制到上面出错提示的路径,再次编译上面的类,编译成功。 2、开发平台采样STM32F103单片机 3、从机采样串口1接口,接收为串口完成中断+DMA,发送为DMA+发送完成中断 3、主机采样串口3接口,接收为串口完成中断+DMA,发送为DMA+发送完成中断 标准的 modbus RTU需要通过一个特殊定时器进行3.5us中断,实际使用时单边片会频繁中,极大的影响了单片机响应速度。 经测试。20个字节内的通讯115200bps下响应周期可以达到5ms,可以做高速开发 /**************************************************************** **模拟 modbus RTU 主从机支持 **支持读写保持寄存器数据 03 16 功能码 **支持读输入寄存器数据 04 功能码 **支持CRC校验 ******************************************************************/