C# 使用modbus 读取PLC 寄存器地址的方法
程序员文章站
2022-03-10 14:47:31
使用的组件nmodbus定义参数,全局变量://创建modbus实体对象 private static modbusfactory modbusfactory; priva...
使用的组件nmodbus
定义参数,全局变量:
//创建modbus实体对象 private static modbusfactory modbusfactory; private static imodbusmaster master; //写线圈或写寄存器数组 bool[] coilsbuffer; ushort[] registerbuffer; //功能码 string functioncode; //参数(分别为站号,起始地址,长度) byte slaveaddress; ushort startaddress; ushort numberofpoints; //ip 地址与端口号 string ipaddress = string.empty; int visitport = 502;
初始化参数,创建连接:
private void connection(object sender, eventargs e) { try { //plc连接配置信息,plc ip地址 ipaddress =127.0.0.1; //端口,modbus 默认502,根据实际情况填写 visitport =502; //初始化modbusmaster modbusfactory = new modbusfactory(); //在本地测试 所以使用回环地址,modbus协议规定端口号 502 master = modbusfactory.createmaster(new tcpclient(ipaddress, visitport)); //设置读取超时时间 master.transport.readtimeout = 10000; master.transport.retries = 2000; } catch (exception ex) { messagebox.show(ex.message); } }
初始化读写参数
/// <summary> /// 初始化读参数 /// </summary> private void setreadparameters(int slaveaddress=1,int startaddress=200,int numberofpoints=1) { //deviceid slaveaddress = byte.parse(slaveaddress); //起始地址, startaddress = ushort.parse(startaddress); //数据长度 numberofpoints = ushort.parse(numberofpoints); }
运行读取方法
private async void executefunction() { try { //moddbus共有这些功能码 //01 read coils //02 read discrete inputs //03 read holding registers //04 read input registers //05 write single coil //06 write single registers //0f write multiple coils //10 write multiple registers functioncode="01 read coils";//根据实际点号类型选择读写功能码 //重新实例化是为了 modbus slave更换连接时不报错 //master = modbusfactory.createmaster(new tcpclient(ipaddress, visitport)); if (functioncode != null) { switch (functioncode) { case "01 read coils"://读取单个线圈 setreadparameters(); coilsbuffer = master.readcoils(slaveaddress, startaddress, numberofpoints); for (int i = 0; i < coilsbuffer.length; i++) { setmsg(coilsbuffer[i] + " "); } setmsg("\r\n"); break; case "02 read discrete inputs"://读取输入线圈/离散量线圈 setreadparameters(); coilsbuffer = master.readinputs(slaveaddress, startaddress, numberofpoints); for (int i = 0; i < coilsbuffer.length; i++) { setmsg(coilsbuffer[i] + " "); } setmsg("\r\n"); break; case "03 read holding registers"://读取保持寄存器 setreadparameters(); registerbuffer = master.readholdingregisters(slaveaddress, startaddress, numberofpoints); for (int i = 0; i < registerbuffer.length; i++) { setmsg(registerbuffer[i] + " "); } setmsg("\r\n"); break; case "04 read input registers"://读取输入寄存器 setreadparameters(); registerbuffer = master.readinputregisters(slaveaddress, startaddress, numberofpoints); for (int i = 0; i < registerbuffer.length; i++) { setmsg(registerbuffer[i] + " "); } setmsg("\r\n"); break; case "05 write single coil"://写单个线圈 setwriteparametes(); await master.writesinglecoilasync(slaveaddress, startaddress, coilsbuffer[0]); break; case "06 write single registers"://写单个输入线圈/离散量线圈 setwriteparametes(); await master.writesingleregisterasync(slaveaddress, startaddress, registerbuffer[0]); break; case "0f write multiple coils"://写一组线圈 setwriteparametes(); await master.writemultiplecoilsasync(slaveaddress, startaddress, coilsbuffer); break; case "10 write multiple registers"://写一组保持寄存器 setwriteparametes(); await master.writemultipleregistersasync(slaveaddress, startaddress, registerbuffer); break; default: break; } } else { messagebox.show("请选择功能码!"); } master.dispose(); } catch (exception ex) { messagebox.show(ex.message); } }
//展示消息
/// <summary> /// setmessage /// </summary> /// <param name="msg"></param> public void setmsg(string msg) { messagebox.show(msg); }
到此这篇关于c# 使用modbus 读取plc 寄存器地址的文章就介绍到这了,更多相关c# 读取plc 寄存器地址内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: C# 递归算法详解
下一篇: ASP 百度主动推送代码范例