Nmodubs4怎么实现单寄存器和多寄存器AO的读写


Nmodubs4怎么实现单寄存器和多寄存器AO的读写,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。项目效果图一、创建plc连接 //用委托对象引用plc_connect方法。
Delegate_Connect connect1 = new Delegate_Connect(plc_connect);
a_status = 1;
connect1(); if (a_status == 0)
{
MessageBox.Show(“网关连接失败!”);
return;
}二、plc连接IP和端口,创建tcp客户端private void plc_connect()
{
wg_ip = INI.InitClass.ReadString(“application”, “WG_IP”, “”);
wg_port = INI.InitClass.ReadString(“application”, “WG_PORT”, “”); try
{
tcp_wg = new TcpClient(wg_ip, Convert.ToInt32(wg_port));
my_wg = ModbusIpMaster.CreateIp(tcp_wg);
a_status = 1;
}
catch (Exception exception)
{
a_status = 0;
}
}三、多线程处理timer//启动timer多线程,每隔1秒刷新在线测试数据
timer_plc_qry.Interval = 2000;
//timer_plc_qry.Enabled = true;
timer_plc_qry.Stop();
timer_plc_qry.Elapsed += TimersTimerHandler;
timer_plc_qry.Start(); //多线程timer事件 在线测试
private void TimersTimerHandler(object sender, EventArgs args)
{
if (a_status == 0) return;
if (Interlocked.Exchange(ref inTimer_plc_qry, 1) == 0 && Start)
{
//连接设备
read_plc();
read_plc_switch();
}
Interlocked.Exchange(ref inTimer_plc_qry, 0);
}四、单寄存器和多寄存器if (list_core[i].data_type == “word”)
{
try
{
read_result = my_wg.ReadHoldingRegisters(1, Convert.ToUInt16(list_core[i].address), 1); if (read_result != null)
{
read = read_result[0]; //读取单个寄存器 ushort类型
this.arcScaleComponent1.Value = read;//进度条复制
Set_main_UAB(read.ToString());//label标签复制
} }
catch (Exception e)
{

} }
else if (list_core[i] 香港云主机.data_type == “float”)
{
read_result = my_wg.ReadHoldingRegisters(1, Convert.ToUInt16(list_core[i].address), 2); if (read_result != null)
{
if (Islow)
{
read_float = ModbusUtility.GetSingle(read_result[1], read_result[0]); //读取多个寄存器,需用用函数 ModbusUtility 转换
this.arcScaleComponent1.Value = read_float;
Set_main_UAB(read_float.ToString()); //设置主窗体控件
}else
{
read_float = ModbusUtility.GetSingle(read_result[0], read_result[1]);
this.arcScaleComponent1.Value = read_float;
Set_main_UAB(read_float.ToString());
}
}
}五、Nmodbus4常用方法:方法名作用所需参数返回值对应功能码ReadCoils读取DO的状态从站地址(8位)byte slaveAddress起始地址(16位)ushort startAddress读取数量(16位)ushort numberOfPointsbool[]01ReadInputs读取DI的状态从站地址(8位)byte slaveAddress起始地址(16位)ushort startAddress读取数量(16位)ushort numberOfPointsbool[]02ReadHoldingRegisters读取AO的值从站地址(8位)byte slaveAddress起始地址(16位)ushort startAddress读取数量(16位)ushort numberOfPointsushort[]03ReadInputRegisters读取AI的值从站地址(8 位)byte slaveAddress起始地址(16位)ushort startAddress读取数量(16位)ushort numberOfPointsushort[]04WriteSingleCoil写入值到DO从站地址(8位)byte slaveAddress线圈地址(16位)ushort coilAddress写入值(布尔型)bool value无返回值05WriteSingleRegister写入值到AO从站地址(8位)byte slaveAddress寄存器地址(16位)ushort registerAddress写入值(16位)ushort value无返回值06WriteMultipleCoils写多线圈寄存器从站地址(8位)byte slaveAddress起始地址(16位)ushort startAddress写入值(布尔型数组)bool[] data无返回值15WriteMultipleRegisters写多个保持寄存器从站地址(8位)byte slaveAddress起始地址(16位)ushort startAddress,寄存器值(16位整型数组)ushort[] data无返回值16ReadWriteMultipleRegisters读写多个保持寄存器从站地址(8位)byte slaveAddress读起始地址(16位)ushort startReadAddress读取数量(16位)ushort numberOfPointsToRead,写入起始地址(16位)ushort startWriteAddress,写入值(16位整型数组)ushort[] writeDataushort[]23看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注开发云行业资讯频道,感谢您对开发云的支持。

相关推荐: ActiveMQ面试题及答案有哪些

这篇文章主要介绍“ActiveMQ面试题及答案有哪些”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“ActiveMQ面试题及答案有哪些”文章能帮助大家解决问题。  1、什么是 ActiveMQ?  activeMQ是一种…

免责声明:本站发布的图片视频文字,以转载和分享为主,文章观点不代表本站立场,本站不承担相关法律责任;如果涉及侵权请联系邮箱:360163164@qq.com举报,并提供相关证据,经查实将立刻删除涉嫌侵权内容。

(0)
打赏 微信扫一扫 微信扫一扫
上一篇 08/12 17:23
下一篇 08/12 17:23

相关推荐