项目效果图
一、创建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 numberOfPoints |
bool[] |
01 |
ReadInputs |
读取DI的状态 |
从站地址(8位) byte slaveAddress 起始地址(16位)
ushort startAddress 读取数量(16位) ushort numberOfPoints |
bool[] |
02 |
ReadHoldingRegisters |
读取AO的值 |
从站地址(8位) byte slaveAddress 起始地址(16位) ushort startAddress 读取数量(16位) ushort numberOfPoints |
ushort[] |
03 |
ReadInputRegisters |
读取AI的值 |
从站地址(8 位) byte slaveAddress 起始地址(16位)
ushort startAddress 读取数量(16位) ushort numberOfPoints |
ushort[] |
04 |
WriteSingleCoil |
写入值到DO |
从站地址(8位) byte slaveAddress 线圈地址(16位) ushort coilAddress 写入值(布尔型) bool value |
无返回值 |
05 |
WriteSingleRegister |
写入值到AO |
从站地址(8位) byte slaveAddress 寄存器地址(16位) ushort registerAddress 写入值(16位) ushort value |
无返回值 |
06 |
WriteMultipleCoils |
写多线圈寄存器 |
从站地址(8位) byte slaveAddress 起始地址(16位) ushort startAddress 写入值(布尔型数组) bool[] data |
无返回值 |
15 |
WriteMultipleRegisters |
写多个保持寄存器 |
从站地址(8位) byte slaveAddress
起始地址(16位) ushort startAddress, 寄存器值(16位整型数组) ushort[] data |
无返回值 |
16 |
ReadWriteMultipleRegisters |
读写多个保持寄存器 |
从站地址(8位) byte slaveAddress 读起始地址(16位) ushort startReadAddress
读取数量(16位) ushort numberOfPointsToRead, 写入起始地址(16位) ushort startWriteAddress, 写入值(16位整型数组) ushort[] writeData |
ushort[] |
23 |