1.ModbTCP主站
csinternal class ModbusRTUServer { private IModbusSerialMaster RTUMaster; private static ModbusRTUServer _Instance =null; public static ModbusRTUServer Instance { get { if (_Instance == null) _Instance = new ModbusRTUServer(); _Instance.ModbusServerInit(); return _Instance; } } public void ModbusServerInit() { try { string[] portNames = SerialPort.GetPortNames(); if (portNames.Length == 0) { Debug.WriteLine("未检测到可用串口"); } else { Debug.WriteLine("可用串口列表:"); foreach (string port in portNames) { Debug.WriteLine(port); // 输出示例:COM1、COM3 } } // 配置串口参数 string portName = "COM3"; // 串口号 int baudRate = 9600; // 波特率 Parity parity = Parity.None; // 校验位(None/Even/Odd) int dataBits = 8; // 数据位 StopBits stopBits = StopBits.One;// 停止位 // 初始化串口 SerialPort serialPort = new SerialPort(portName, baudRate, parity, dataBits, stopBits); serialPort.Open(); //2.实例化RTUMaster对象 RTUMaster = ModbusSerialMaster.CreateRtu(serialPort); //3.读取数据,通讯(读取存储区,起始地址,读取数量) //ReadData("保持寄存器"); //4.写入数据,通讯(写入存储区,起始地址) WriteData(); //循环读取数据 while (true) { Thread.Sleep(3000); ReadData("保持寄存器"); } } catch (Exception e) { AddLog("连接失败:" + e.Message); ModbusLog.m.ModbusOpen().Info("连接失败:" + e.Message); ModbusServerInit(); } } private void ReadData(string StoreArea) { //3.读取数据,通讯(存储区,起始地址,读取数量) ushort startAddress = Convert.ToUInt16("0");//起始地址 ushort numberOfPoints = Convert.ToUInt16("10");//读取数量 byte slaveAddress = 1;//设备地址 //IModbusSerialMaster RTUMaster,根据不同的存储区调用对应的方法 //定义读取存储区 //string StoreArea = "输入寄存器";//输入线圈,输出线圈,输入寄存器,输出寄存器 bool[] res; ushort[] var; switch (StoreArea) { case "离散输入寄存器": res = RTUMaster.ReadInputs(slaveAddress, startAddress, numberOfPoints); ModbusLog.m.ModbusOpen().Info(string.Join("-", res)); break; case "线圈": res = RTUMaster.ReadCoils(slaveAddress, startAddress, numberOfPoints); ModbusLog.m.ModbusOpen().Info(string.Join("-", res)); break; case "输入寄存器": var = RTUMaster.ReadInputRegisters(slaveAddress, startAddress, numberOfPoints); ModbusLog.m.ModbusOpen().Info(string.Join("-", var)); break; case "保持寄存器": var = RTUMaster.ReadHoldingRegisters(slaveAddress, startAddress, numberOfPoints); AddLog("读取保持寄存器:" + string.Join("-", var)); ModbusLog.m.ModbusOpen().Info(string.Join("-", var)); break; } } private void WriteData() { byte slaveAddress = 1;//设备地址 ushort start1 = Convert.ToUInt16("8");//起始地址 ushort start2 = Convert.ToUInt16("9");//起始地址 //ModbusIpMaster RTUMaster,根据不同的存储区调用对应的方法 //定义写入存储区 string StoreArea = "保持寄存器";//输入线圈,输出线圈,输入寄存器,输出寄存器 bool[] res; ushort[] var; switch (StoreArea) { case "输入线圈": RTUMaster.WriteSingleCoil(slaveAddress, start1, true);//写入输出线圈 break; case "保持寄存器": RTUMaster.WriteSingleRegister(slaveAddress, start1, Convert.ToUInt16("111"));//写入输出寄存器 RTUMaster.WriteSingleRegister(slaveAddress, start2, Convert.ToUInt16("111"));//写入输出寄存器 break; } } private void AddLog(string info) { Debug.WriteLine(info); } }
ModbRTU主站和从站通讯
2.ModbTCP主站
csinternal class ModbusTCPServer { private TcpClient tcpClient; private ModbusIpMaster ipMaster; private static ModbusTCPServer _Instance =null; public static ModbusTCPServer Instance { get { if (_Instance == null) _Instance = new ModbusTCPServer(); _Instance.ModbusServerInit(); return _Instance; } } public void ModbusServerInit() { try { tcpClient = new TcpClient(); //1.创建一个ModbusTCP连接,ModbusTCP连接本质就是socket的TCP连接(三菱的默认是TCP连接) tcpClient.Connect(IPAddress.Parse("192.168.134.143"), int.Parse("4979")); //2.实例化ipMaster对象 ipMaster = ModbusIpMaster.CreateIp(tcpClient); //3.读取数据,通讯(读取存储区,起始地址,读取数量) //ReadData("保持寄存器"); //4.写入数据,通讯(写入存储区,起始地址) WriteData(); //循环读取数据 while (true) { Thread.Sleep(3000); ReadData("保持寄存器"); } } catch (Exception e) { AddLog("连接失败:" + e.Message); ModbusLog.m.ModbusOpen().Info("连接失败:" + e.Message); ModbusServerInit(); } } private void ReadData(string StoreArea) { //3.读取数据,通讯(存储区,起始地址,读取数量) ushort startAddress = Convert.ToUInt16("0");//起始地址 ushort numberOfPoints = Convert.ToUInt16("10");//读取数量 byte slaveAddress = 1;//设备地址 //ModbusIpMaster ipMaster,根据不同的存储区调用对应的方法 //定义读取存储区 //string StoreArea = "输入寄存器";//输入线圈,输出线圈,输入寄存器,输出寄存器 bool[] res; ushort[] var; switch (StoreArea) { case "离散输入寄存器": res = ipMaster.ReadInputs(slaveAddress, startAddress, numberOfPoints); ModbusLog.m.ModbusOpen().Info(string.Join("-", res)); break; case "线圈": res = ipMaster.ReadCoils(slaveAddress, startAddress, numberOfPoints); ModbusLog.m.ModbusOpen().Info(string.Join("-", res)); break; case "输入寄存器": var = ipMaster.ReadInputRegisters(slaveAddress, startAddress, numberOfPoints); ModbusLog.m.ModbusOpen().Info(string.Join("-", var)); break; case "保持寄存器": var = ipMaster.ReadHoldingRegisters(slaveAddress, startAddress, numberOfPoints); AddLog("读取保持寄存器:" + string.Join("-", var)); ModbusLog.m.ModbusOpen().Info(string.Join("-", var)); break; } } private void WriteData() { byte slaveAddress = 1;//设备地址 ushort start1 = Convert.ToUInt16("8");//起始地址 ushort start2 = Convert.ToUInt16("9");//起始地址 //ModbusIpMaster ipMaster,根据不同的存储区调用对应的方法 //定义写入存储区 string StoreArea = "保持寄存器";//输入线圈,输出线圈,输入寄存器,输出寄存器 bool[] res; ushort[] var; switch (StoreArea) { case "输入线圈": ipMaster.WriteSingleCoil(slaveAddress, start1, true);//写入输出线圈 break; case "保持寄存器": ipMaster.WriteSingleRegister(slaveAddress,start1, Convert.ToUInt16("111"));//写入输出寄存器 ipMaster.WriteSingleRegister(slaveAddress, start2, Convert.ToUInt16("111"));//写入输出寄存器 break; } } private void AddLog(string info) { Debug.WriteLine(info); } }
ModbusTCP主站和ModbusTCP从站通讯
C#,ModbTCP和ModbRTU通讯
技术支持者python,php2025-08-29 9:16
相关推荐
开开心心就好1 分钟前
文档格式转换软件 一键Word转PDF从零开始学习人工智能2 分钟前
PDFMathTranslate:让科学PDF翻译不再难——技术原理与实践指南深盾科技6 分钟前
探索Swift Package Manager:全面指南拾忆,想起1 小时前
Redis发布订阅:实时消息系统的极简解决方案AllyLi02241 小时前
CondaError: Run ‘conda init‘ before ‘conda activate‘羑悻的小杀马特1 小时前
【C++高并发内存池篇】ThreadCache 极速引擎:C++ 高并发内存池的纳秒级无锁革命!布朗克1682 小时前
OpenTelemetry 在 Spring Boot 项目中的3种集成方式jingfeng5142 小时前
线程池及线程池单例模式指针刺客2 小时前
嵌入式筑基之设计模式小码编匠3 小时前
WinForm 中集成 NLog 实现全局异常处理