ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C# Modbus_TCP通讯 dll库 测试TRIO运动控制器

2020-04-23 21:59:56  阅读:380  来源: 互联网

标签:TRIO C# label3 SolidColorBrush TCP Content Colors Background new


在项目的实际应用中,通讯是必不可少,最近一直在测试modbus通讯,在网上找到一个合适的免费动态链接库,已在项目上应用,非常稳定。

  网站地址:www.easymodbustcp.net   

 

1、动态链接库网盘地址,里面有dll文件及测试工具,例子。

  百度云盘:https://pan.baidu.com/s/1Uunp2CDB8Hz3uyOsWn_Umg       提取码:dac6

 

2、下面是我以TRIO为例测试 Modbus_TCP,此外可用西门子,三菱,欧姆龙测试。

     测试截图: 连接 ip: 192.168.0.250    prot: 502

      

 

   连接成功后即可测试

      

 

       

 

    代码:  

    
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;
using System.Threading;
using System.Text.RegularExpressions;

namespace Wpf_TRIO_Modbus_tcp
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }


        ModbusClient modbusClient = new ModbusClient("192.168.250",502);


        //点击连接
        private void b_connect_Click(object sender, RoutedEventArgs e)
        {
            modbusClient.Connect();
            Thread.Sleep(1000);
            int[] i = modbusClient.ReadInputRegisters(10, 1);
            foreach (int team in i)
            {
                if (team != 1)
                {
                    ConnectStatus.Content = "连接失败";
                }
                else
                {
                    ConnectStatus.Content = "连接成功";
                    ConnectStatus.Foreground = new SolidColorBrush(Colors.Green);
                }
            }
        }

        //点击断连接
        private void b_disconnect_Click(object sender, RoutedEventArgs e)
        {
            modbusClient.Disconnect();
            Thread.Sleep(1000);
            ConnectStatus.Content = "未连接...";
            ConnectStatus.Foreground = new SolidColorBrush(Colors.Red);
        }


        private void b_read_input_Click(object sender, RoutedEventArgs e)
        {
            //读取输入 input 信号 :  ReadDiscreteInputs
            bool[] InputStatus =  modbusClient.ReadDiscreteInputs(0, 3);
            if (InputStatus[0] == true)
            {
                label3_Copy0.Background = new SolidColorBrush(Colors.Green);
                label3_Copy0.Content = InputStatus[0].ToString();
            }
            else
            {
                label3_Copy0.Background = new SolidColorBrush(Colors.Red);
                label3_Copy0.Content = InputStatus[0].ToString();
            }

            if (InputStatus[1] == true)
            {
                label3_Copy1.Background = new SolidColorBrush(Colors.Green);
                label3_Copy1.Content = InputStatus[1].ToString();
            }
            else
            {
                label3_Copy1.Background = new SolidColorBrush(Colors.Red);
                label3_Copy1.Content = InputStatus[1].ToString();
            }

            if (InputStatus[2] == true)
            {
                label3_Copy2.Background = new SolidColorBrush(Colors.Green);
                label3_Copy2.Content = InputStatus[2].ToString();
            }
            else
            {
                label3_Copy2.Background = new SolidColorBrush(Colors.Red);
                label3_Copy2.Content = InputStatus[2].ToString();
            }

        }

        private void bReadOp_Click(object sender, RoutedEventArgs e)
        {
            //读取输出线圈信号
            bool[] OutputStatus = modbusClient.ReadCoils(8, 3);
           // MessageBox.Show(OutputStatus[0].ToString());
            ///
            if (OutputStatus[0] == true)
            {
                label3_Copy9.Background = new SolidColorBrush(Colors.Green);
                label3_Copy9.Content = OutputStatus[0].ToString();
            }
            else
            {
                label3_Copy9.Background = new SolidColorBrush(Colors.Red);
                label3_Copy9.Content = OutputStatus[0].ToString();
            }

            ///
            if (OutputStatus[1] == true)
            {
                label3_Copy10.Background = new SolidColorBrush(Colors.Green);
                label3_Copy10.Content = OutputStatus[1].ToString();
            }
            else
            {
                label3_Copy10.Background = new SolidColorBrush(Colors.Red);
                label3_Copy10.Content = OutputStatus[1].ToString();
            }

            ///
            if (OutputStatus[2] == true)
            {
                label3_Copy11.Background = new SolidColorBrush(Colors.Green);
                label3_Copy11.Content = OutputStatus[2].ToString();
            }
            else
            {
                label3_Copy11.Background = new SolidColorBrush(Colors.Red);
                label3_Copy11.Content = OutputStatus[2].ToString();
            }

        }

        private void bWriteOpT_Click(object sender, RoutedEventArgs e)
        {
            //批量写输出信号
            bool[] WriteCoilsT = { true,true,true };
            modbusClient.WriteMultipleCoils(8, WriteCoilsT);
            label3_Copy9.Background = new SolidColorBrush(Colors.Green);
            label3_Copy9.Content = "TRUE";
            label3_Copy10.Background = new SolidColorBrush(Colors.Green);
            label3_Copy10.Content = "TRUE";
            label3_Copy11.Background = new SolidColorBrush(Colors.Green);
            label3_Copy11.Content = "TRUE";
        }

        private void bWriteOp_Click(object sender, RoutedEventArgs e)
        {
            //批量写输出信号
            bool[] WriteCoilsT = { false, false, false };
            modbusClient.WriteMultipleCoils(8, WriteCoilsT);
            label3_Copy9.Background = new SolidColorBrush(Colors.Red);
            label3_Copy9.Content = "FALSE";
            label3_Copy10.Background = new SolidColorBrush(Colors.Red);
            label3_Copy10.Content = "FALSE";
            label3_Copy11.Background = new SolidColorBrush(Colors.Red);
            label3_Copy11.Content = "FALSE";
        }

        private void ReadVr_Click(object sender, RoutedEventArgs e)
        {
            //读取多个寄存器的值
           int[] ReadVrVal = modbusClient.ReadHoldingRegisters(0, 3);
            textBoxVr0.Text = ReadVrVal[0].ToString();
            textBoxVr1.Text = ReadVrVal[1].ToString();
            textBoxVr2.Text = ReadVrVal[2].ToString();
        }


        private void WriteVr_Click(object sender, RoutedEventArgs e)
        {
            if (textBoxVr0.Text != null && Regex.IsMatch(textBoxVr0.Text, @"^[+-]?\d*[.]?\d*$"))
            {
                //写多个寄存器的值
                int[] WriteVrVal = { int.Parse(textBoxVr0.Text.Trim()), int.Parse(textBoxVr1.Text.Trim()), int.Parse(textBoxVr2.Text.Trim()) };
                modbusClient.WriteMultipleRegisters(0, WriteVrVal);

                MessageBox.Show("写入成功");
            }
            else
            {
                MessageBox.Show("非法值");
                textBoxVr0.Text = "0";
                textBoxVr1.Text = "0";
                textBoxVr2.Text = "0";

            }

        }
    }
}
View Code

   代码有很多不规范的地方,由于刚刚入门C#,很多类库第一次接触,本想做的完善点但实属心有余而语言不熟(正在不断的学习中...),以上仅作参考。

完整代码github地址:

  https://github.com/httpcc/C_sharpTestModbus_tcp

标签:TRIO,C#,label3,SolidColorBrush,TCP,Content,Colors,Background,new
来源: https://www.cnblogs.com/httpcc/p/12753331.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有