C# WPF上位机开发(利用tcp/ip网络访问plc)

【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:feixiaoxing @163.com】

c# wpf如果是用来开发非标上位机的,那么和plc的通信肯定是少不了的。而且,大部分plc都支持modbus协议,所以这个时候如果有一个库可以帮助我们和plc设备进行modbus通信,那就非常方便的。目前NuGet下关于modbus的函数库还是有一些的,比如EasyModbus。

1、用NuGet下载EasyModbus

对应库的名字叫EasyModbusTCP,虽然名称当中包含了TCP,其实本身也是支持UDP的。这点大家注意下。下载的方法和之前的下载也是类似的,用NuGet搜索完成后,直接下载即可,

2、准备测试xaml界面

测试界面的话,也不用特别复杂,主要就是两个按钮。这两个按钮就是负责设备的读写功能。除了这两个按钮之外,还有一个textbox,负责数据的显示。

界面本身不复杂,这里也给出xaml代码,供大家参考,

复制代码
<Window x:Class="WpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="600">
    <Grid>
        <StackPanel Margin="10,10,10,10">
            <Button Content="Read from PLC" Height="80" Click="ReadFromPLC_Click"/>
            <Label Height="20"/>
            <Button Content="Write to PLC" Height="80" Click="WriteToPLC_Click"/>
            <Label Height="20"/>
            <TextBox x:Name="plcValueTextBox" Height="60" Text=""/>
        </StackPanel>
    </Grid>
</Window>

3、编写和准备测试代码

代码部分主要还是围绕着控件的回调功能进行的。首先两个按钮肯定各有一个回调函数。而且函数实现的功能也肯定和modbus相关。此外既然是网络,那么有必要在程序开始启动的时候,就准备好对应的设备套接字,这样才能在回调函数中完成对应的读写操作。

整个代码本身没有什么理解难度,大家简单浏览下就会掌握它的用法的。

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using EasyModbus;

namespace WpfApp
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {

        private ModbusClient modbusClient;

        public MainWindow()
        {
            InitializeComponent();
            ConnectToPLC();
        }

        private void ConnectToPLC()
        {
            modbusClient = new ModbusClient("10.0.0.10", 502); // Replace with your PLC's IP address
            modbusClient.Connect();
        }

        private void ReadFromPLC_Click(object sender, RoutedEventArgs e)
        {
            ushort startAddress = 0; // Replace with the starting address in your PLC
            int num = 2;
            int[] data = modbusClient.ReadHoldingRegisters(startAddress, num);
            plcValueTextBox.Text = data[0].ToString();
        }

        private void WriteToPLC_Click(object sender, RoutedEventArgs e)
        {
            ushort startAddress = 0; // Replace with the starting address in your PLC
            int valueToWrite = Convert.ToInt32(plcValueTextBox.Text);
            modbusClient.WriteSingleRegister(startAddress, valueToWrite);
        }
    }
}
相关推荐
sali-tec1 分钟前
C# 基于halcon的视觉工作流-章33-矩状测量
开发语言·人工智能·算法·计算机视觉·c#
王维志36 分钟前
Unity Embedded Browser文档翻译
unity·c#
FlYFlOWERANDLEAF41 分钟前
DevExpress中Word Processing Document API学习记录
学习·c#·word
酷炫码神1 小时前
第 2 篇:Java 入门实战(JDK8 版)—— 编写第一个 Java 程序,理解基础运行逻辑
java·开发语言·策略模式
像风一样自由20201 小时前
Go语言详细指南:特点、应用场景与开发工具
开发语言·后端·golang
半夏知半秋1 小时前
基于跳跃表的zset实现解析(lua版)
服务器·开发语言·redis·学习·lua
Wyc724091 小时前
Lua语言基础笔记
开发语言·笔记·lua
棉晗榜1 小时前
C#写字符串到Modbus中
c#·modbus
编码浪子6 小时前
趣味学RUST基础篇(智能指针_结束)
开发语言·算法·rust
CVer儿7 小时前
qt资料2025
开发语言·qt