C# 使用modbus 读取PLC 寄存器地址的方法
短信预约 -IT技能 免费直播动态提醒
使用的组件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 寄存器地址内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341