using System; using System.Collections.Generic; using System.Linq; using System.Text; 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 FLY.ModbusModule; using System.Net; using System.ComponentModel; namespace WpfApplication_ModbusClient { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window,INotifyPropertyChanged { private UInt16 addr01 = 0; public UInt16 Addr01 { get { return addr01; } set { if (addr01 != value) { addr01 = value; NotifyPropertyChanged("Addr01"); } } } private UInt16 addr02 = 0; public UInt16 Addr02 { get { return addr02; } set { if (addr02 != value) { addr02 = value; NotifyPropertyChanged("Addr02"); } } } private bool addr01_bit = false; public bool Addr01_bit { get { return addr01_bit; } set { if (addr01_bit != value) { addr01_bit = value; NotifyPropertyChanged("Addr01_bit"); } } } private bool addr02_bit = false; public bool Addr02_bit { get { return addr02_bit; } set { if (addr02_bit != value) { addr02_bit = value; NotifyPropertyChanged("Addr02_bit"); } } } ClientTCP mModbusClient; public MainWindow() { InitializeComponent(); FObjBase.PollModule.Current.Start(); mModbusClient = new ClientTCP(IPAddress.Parse("192.168.250.129")); this.textBlock2.DataContext = mModbusClient; this.textBlock1.DataContext = this; this.textBlock5.DataContext = this; } private void button2_Click(object sender, RoutedEventArgs e) { mModbusClient.Do_03(0x1, 2, delegate(UInt16[] datas, object asyncstate) { Addr01 = datas[0]; Addr02 = datas[1]; }, null); } protected void NotifyPropertyChanged(string propertyname) { if (PropertyChanged != null) { PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyname)); } } public event PropertyChangedEventHandler PropertyChanged; private void button1_Click(object sender, RoutedEventArgs e) { UInt16 addr01; if (!UInt16.TryParse(textBox1.Text, System.Globalization.NumberStyles.HexNumber, null, out addr01)) { MessageBox.Show("格式出错"); return; } UInt16 addr02; if (!UInt16.TryParse(textBox2.Text, System.Globalization.NumberStyles.HexNumber, null, out addr02)) { MessageBox.Show("格式出错"); return; } mModbusClient.Do_10(0x1, new UInt16[] { addr01,addr02 }); MessageBox.Show("设置成功"); } private void button4_Click(object sender, RoutedEventArgs e) { mModbusClient.Do_0F(0x1, new bool[] { (bool)(checkBox1.IsChecked), (bool)(checkBox2.IsChecked) }); MessageBox.Show("设置成功"); } private void button3_Click(object sender, RoutedEventArgs e) { mModbusClient.Do_01(0x1, 2, delegate(bool[] datas, object asyncstate) { Addr01_bit = datas[0]; Addr02_bit = datas[1]; }, null); } } }