在項目的實際應用中,通訊是必不可少,最近一直在測試modbus通訊,在網上找到一個合適的免費動態鏈接庫,已在項目上應用,非常穩定。
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"; } } } }
代碼有很多不規范的地方,由於剛剛入門C#,很多類庫第一次接觸,本想做的完善點但實屬心有余而語言不熟(正在不斷的學習中...),以上僅作參考。
完整代碼github地址: