一、PLC设置
1.PLC版本
2.模拟PLC
PLC模拟时默认IP地址为:127.0.0.1
二、C#
1.安装依赖
2.C#建立PL连接
using引用
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using EasyModbus;
1.UI页面
2.C#程序
#region 验证IP地址格式的辅助方法
private bool IsValidIPAddress(string ipAddress)
{
try
{
var ip = IPAddress.Parse(ipAddress);
return ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork;
}
catch
{
return false;
}
}
#endregion
#region 建立连接
private void btnConnect_Click(object sender, EventArgs e)
{
// 验证IP地址格式
if (!IsValidIPAddress(txtIpAddress.Text))
{
MessageBox.Show("请输入有效的IP地址。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
// 使用用户输入的IP地址和预设的端口号创建ModbusClient实例
_modbusClient = new ModbusClient(txtIpAddress.Text, DefaultModbusPort);
_modbusClient.Connect();
// 检查连接状态并更新UI
if (_modbusClient.Connected)
{
MessageBox.Show("成功连接到PLC");
}
else
{
MessageBox.Show("连接失败。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (SocketException ex)
{
// 处理网络异常
MessageBox.Show($"网络错误: {ex.Message}", "网络错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (ArgumentNullException ex)
{
// 处理空引用异常
MessageBox.Show($"参数错误: {ex.Message}", "参数错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception ex)
{
// 捕获其他所有异常
MessageBox.Show($"连接过程中发生未知错误: {ex.Message}", "未知错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
#endregion
#region 断开连接
private void btnDisconnect_Click(object sender, EventArgs e)
{
if (_modbusClient != null && _modbusClient.Connected)
{
_modbusClient.Disconnect();
MessageBox.Show("已断开与PLC的连接。");
}
else
{
MessageBox.Show("尚未连接到任何设备。", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
#endregion
3.写入读取D100
1.UI页面
2.C#程序
#region 写D100入值
private void BtnWriteToD100_Click(object sender, EventArgs e)
{
if (_modbusClient == null || !_modbusClient.Connected)
{
MessageBox.Show("请先连接到PLC。", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
try
{
int valueToWrite = Convert.ToInt32(txtValueToWrite.Text);
_modbusClient.WriteSingleRegister(100, valueToWrite); // 注意:100 是 Modbus 地址,对应 D100 寄存器
MessageBox.Show("成功写入值到D100寄存器。");
}
catch (FormatException)
{
MessageBox.Show("输入的值无效,请输入一个整数。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception ex)
{
MessageBox.Show($"写入过程中发生未知错误: {ex.Message}", "未知错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
#endregion
#region 读D100取值
private void BtnReadFromD100_Click(object sender, EventArgs e)
{
if (_modbusClient == null || !_modbusClient.Connected)
{
MessageBox.Show("请先连接到PLC。", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
try
{
int[] registers = _modbusClient.ReadHoldingRegisters(100, 1);
if (registers.Length > 0)
{
int value = registers[0];
lblD100Value.Text = $"D100 的值: {value}";
}
else
{
MessageBox.Show("未能读取到寄存器的值。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception ex)
{
MessageBox.Show($"读取过程中发生未知错误: {ex.Message}", "未知错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
#endregion
4.写入读取M100
1.UI页面
2.C#程序
#region 写M100入值
private void BtnWriteToM100_Click(object sender, EventArgs e)
{
if (_modbusClient == null || !_modbusClient.Connected)
{
MessageBox.Show("请先连接到PLC。", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
try
{
bool valueToWrite = Convert.ToBoolean(txtValueToWrite1.Text);
_modbusClient.WriteSingleCoil(100, valueToWrite); // 注意:100 是 Modbus 地址,对应 M100 线圈
MessageBox.Show("成功写入值到M100线圈。");
}
catch (FormatException)
{
MessageBox.Show("输入的值无效,请输入一个布尔值(true 或 false)。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception ex)
{
MessageBox.Show($"写入过程中发生未知错误: {ex.Message}", "未知错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
#endregion
#region 读M100入值
private void BtnReadFromM100_Click(object sender, EventArgs e)
{
if (_modbusClient == null || !_modbusClient.Connected)
{
MessageBox.Show("请先连接到PLC。", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
try
{
bool[] coils = _modbusClient.ReadCoils(100, 1); // 100 是 Modbus 地址,对应 M100 寄存器
if (coils.Length > 0)
{
bool value = coils[0];
txtValueFromM100.Text = $"M100 的值: {value}";
MessageBox.Show($"成功读取M100寄存器的值: {value}。");
}
else
{
MessageBox.Show("没有读取到有效的寄存器值。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception ex)
{
MessageBox.Show($"读取过程中发生未知错误: {ex.Message}", "未知错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
#endregion