From c791e32d9ca57ac070cb4e38dfcc79fd84a75c35 Mon Sep 17 00:00:00 2001 From: ly Date: Wed, 5 Mar 2025 11:21:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=A1=B9=E7=9B=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ControlcBoardsTest.sln | 25 + ControlcBoardsTest/App.config | 6 + ControlcBoardsTest/App.xaml | 9 + ControlcBoardsTest/App.xaml.cs | 17 + ControlcBoardsTest/ControlcBoardsTest.csproj | 110 ++++ ControlcBoardsTest/MainWindow.xaml | 94 ++++ ControlcBoardsTest/MainWindow.xaml.cs | 524 ++++++++++++++++++ ControlcBoardsTest/ModbusHelper.cs | 84 +++ ControlcBoardsTest/Properties/AssemblyInfo.cs | 52 ++ .../Properties/Resources.Designer.cs | 63 +++ ControlcBoardsTest/Properties/Resources.resx | 117 ++++ .../Properties/Settings.Designer.cs | 26 + .../Properties/Settings.settings | 7 + ControlcBoardsTest/packages.config | 6 + 14 files changed, 1140 insertions(+) create mode 100644 ControlcBoardsTest.sln create mode 100644 ControlcBoardsTest/App.config create mode 100644 ControlcBoardsTest/App.xaml create mode 100644 ControlcBoardsTest/App.xaml.cs create mode 100644 ControlcBoardsTest/ControlcBoardsTest.csproj create mode 100644 ControlcBoardsTest/MainWindow.xaml create mode 100644 ControlcBoardsTest/MainWindow.xaml.cs create mode 100644 ControlcBoardsTest/ModbusHelper.cs create mode 100644 ControlcBoardsTest/Properties/AssemblyInfo.cs create mode 100644 ControlcBoardsTest/Properties/Resources.Designer.cs create mode 100644 ControlcBoardsTest/Properties/Resources.resx create mode 100644 ControlcBoardsTest/Properties/Settings.Designer.cs create mode 100644 ControlcBoardsTest/Properties/Settings.settings create mode 100644 ControlcBoardsTest/packages.config diff --git a/ControlcBoardsTest.sln b/ControlcBoardsTest.sln new file mode 100644 index 0000000..a5c4a26 --- /dev/null +++ b/ControlcBoardsTest.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.11.35222.181 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ControlcBoardsTest", "ControlcBoardsTest\ControlcBoardsTest.csproj", "{6FD3F202-2D0F-4501-8F7C-3DA7384B4B95}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {6FD3F202-2D0F-4501-8F7C-3DA7384B4B95}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6FD3F202-2D0F-4501-8F7C-3DA7384B4B95}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6FD3F202-2D0F-4501-8F7C-3DA7384B4B95}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6FD3F202-2D0F-4501-8F7C-3DA7384B4B95}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {EAECA0FF-43E9-43D2-8868-02CBD267158F} + EndGlobalSection +EndGlobal diff --git a/ControlcBoardsTest/App.config b/ControlcBoardsTest/App.config new file mode 100644 index 0000000..d1428ad --- /dev/null +++ b/ControlcBoardsTest/App.config @@ -0,0 +1,6 @@ + + + + + + diff --git a/ControlcBoardsTest/App.xaml b/ControlcBoardsTest/App.xaml new file mode 100644 index 0000000..937961e --- /dev/null +++ b/ControlcBoardsTest/App.xaml @@ -0,0 +1,9 @@ + + + + + diff --git a/ControlcBoardsTest/App.xaml.cs b/ControlcBoardsTest/App.xaml.cs new file mode 100644 index 0000000..080b956 --- /dev/null +++ b/ControlcBoardsTest/App.xaml.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Data; +using System.Linq; +using System.Threading.Tasks; +using System.Windows; + +namespace ControlcBoardsTest +{ + /// + /// App.xaml 的交互逻辑 + /// + public partial class App : Application + { + } +} diff --git a/ControlcBoardsTest/ControlcBoardsTest.csproj b/ControlcBoardsTest/ControlcBoardsTest.csproj new file mode 100644 index 0000000..70ef20a --- /dev/null +++ b/ControlcBoardsTest/ControlcBoardsTest.csproj @@ -0,0 +1,110 @@ + + + + + Debug + AnyCPU + {6FD3F202-2D0F-4501-8F7C-3DA7384B4B95} + WinExe + ControlcBoardsTest + ControlcBoardsTest + v4.5 + 512 + {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 4 + true + true + + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\HslCommunication.12.2.0\lib\net35\HslCommunication.dll + + + ..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll + + + ..\packages\NModbus4.2.1.0\lib\net40\NModbus4.dll + + + + + + + + + + + 4.0 + + + + + + + + MSBuild:Compile + Designer + + + MSBuild:Compile + Designer + + + App.xaml + Code + + + + MainWindow.xaml + Code + + + + + Code + + + True + True + Resources.resx + + + True + Settings.settings + True + + + ResXFileCodeGenerator + Resources.Designer.cs + + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + + + + + \ No newline at end of file diff --git a/ControlcBoardsTest/MainWindow.xaml b/ControlcBoardsTest/MainWindow.xaml new file mode 100644 index 0000000..82eabc1 --- /dev/null +++ b/ControlcBoardsTest/MainWindow.xaml @@ -0,0 +1,94 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ControlcBoardsTest/MainWindow.xaml.cs b/ControlcBoardsTest/MainWindow.xaml.cs new file mode 100644 index 0000000..9978ba5 --- /dev/null +++ b/ControlcBoardsTest/MainWindow.xaml.cs @@ -0,0 +1,524 @@ +using Modbus.Device; +using System; +using System.IO.Ports; +using System.Threading; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Media; + +namespace ControlcBoardsTest +{ + /// + /// MainWindow.xaml 的交互逻辑 + /// + public partial class MainWindow : Window + { + public MainWindow() + { + InitializeComponent(); + this.Loaded += MainWindow_Loaded; + this.Closing += MainWindow_Closing; + } + + private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e) + { + + //停止通道 + for (int i = 0; i < 24; i++) + { + + ModbusHelper.WriteSingleCoil(stationNo, (ushort)i, false); + + + } + + ModbusHelper.WriteSingleCoil(stationNo, 16, false); + ModbusHelper.WriteSingleCoil(stationNo, 18, false); + ModbusHelper.WriteSingleCoil(stationNo, 20, false); + ModbusHelper.WriteSingleCoil(stationNo, 22, false); + + ModbusHelper.WriteSingleCoil(stationNo, 17, false); + ModbusHelper.WriteSingleCoil(stationNo, 19, false); + ModbusHelper.WriteSingleCoil(stationNo, 21, false); + ModbusHelper.WriteSingleCoil(stationNo, 23, false); + ModbusHelper.master.Close(); + } + + private void MainWindow_Loaded(object sender, RoutedEventArgs e) + { + foreach (var item in SerialPort.GetPortNames()) + { + com_port.Items.Add(item); + if (com_port.Items.Count > 0) + com_port.SelectedIndex = 0; + } + + + } + private byte stationNo; + private void connButton_Click(object sender, RoutedEventArgs e) + { + //打开串口 + if (!ModbusHelper.master.IsOpen()) + { + + //创建ModbusRTU主站实例 + ModbusHelper.master.SerialPortInni(serialPort => + { + + //设定串口参数 + serialPort.PortName = com_port.SelectedItem.ToString(); + serialPort.BaudRate = Convert.ToInt32(txt_baudRate.Text.ToString()); + serialPort.Parity = 0; + serialPort.DataBits = Convert.ToInt32(txt_sataBits.Text.ToString()); + serialPort.StopBits = ModbusHelper.GetSelectedStopBits(Convert.ToDouble(txt_stopBits.Text.ToString())); + stationNo = Convert.ToByte(txt_stationNo.Text); + + }); + + ModbusHelper.master.Open(); + if (ModbusHelper.master.IsOpen()) + { + btn_conn.Content = "断开"; + + for (int i = 0; i < 24; i++) + { + + ModbusHelper.WriteSingleCoil(stationNo, (ushort)i, false); + + Thread.Sleep(100); + } + + var CoilsRes = ModbusHelper.ReadCoils(stationNo, 0, 24); + + + foreach (var item in warp_channel.Children) + { + if (item is Button) + { + var c = (Button)item; + c.Background = new SolidColorBrush(CoilsRes[Convert.ToUInt16(c.Tag)] ? Colors.Green : Colors.Red); + } + } + + btn_ElectricMotor1Front.Background = new SolidColorBrush(CoilsRes[Convert.ToUInt16(16)] ? Colors.Green : Colors.Red); + btn_ElectricMotor1Back.Background = new SolidColorBrush(CoilsRes[Convert.ToUInt16(17)] ? Colors.Green : Colors.Red); + btn_ElectricMotor2Front.Background = new SolidColorBrush(CoilsRes[Convert.ToUInt16(18)] ? Colors.Green : Colors.Red); + btn_ElectricMotor2Back.Background = new SolidColorBrush(CoilsRes[Convert.ToUInt16(19)] ? Colors.Green : Colors.Red); + btn_ElectricMotor3Front.Background = new SolidColorBrush(CoilsRes[Convert.ToUInt16(20)] ? Colors.Green : Colors.Red); + btn_ElectricMotor3Back.Background = new SolidColorBrush(CoilsRes[Convert.ToUInt16(21)] ? Colors.Green : Colors.Red); + btn_ElectricMotor4Front.Background = new SolidColorBrush(CoilsRes[Convert.ToUInt16(22)] ? Colors.Green : Colors.Red); + btn_ElectricMotor4Back.Background = new SolidColorBrush(CoilsRes[Convert.ToUInt16(23)] ? Colors.Green : Colors.Red); + + + } + else + { + btn_conn.Content = "连接"; + } + } + else + { + ModbusHelper.master.Close(); + if (ModbusHelper.master.IsOpen()) + { + btn_conn.Content = "断开"; + } + else + { + btn_conn.Content = "连接"; + } + } + + + + } + + private void Button_Click(object sender, RoutedEventArgs e) + { + var btn = sender as Button; + //读取当前状态 + DateTime dt = DateTime.Now; + var CoilsResBefor = ModbusHelper.ReadCoils(stationNo, Convert.ToUInt16(btn.Tag), 1); + Console.WriteLine("=================="+DateTime.Now.Subtract(dt).TotalMilliseconds); + Thread.Sleep(100); + //写入线圈 + ModbusHelper.WriteSingleCoil(stationNo, Convert.ToUInt16(btn.Tag), !CoilsResBefor[0]); + Thread.Sleep(100); + //读取写入后状态 + var CoilsRes = ModbusHelper.ReadCoils(stationNo, Convert.ToUInt16(btn.Tag), 1); + btn.Background = new SolidColorBrush(CoilsRes[0] ? Colors.Green : Colors.Red); + } + + /// + /// 电机1正传 + /// + /// + /// + private void Button_Click_1(object sender, RoutedEventArgs e) + { + ModbusHelper.WriteSingleCoil(stationNo, 17, false); + Thread.Sleep(100); + //读取当前状态 + var CoilsResBefor = ModbusHelper.ReadCoils(stationNo, 16, 1); + Thread.Sleep(100); + //写入线圈 + ModbusHelper.WriteSingleCoil(stationNo, 16, !CoilsResBefor[0]); + Thread.Sleep(100); + //读取写入后状态 + var CoilsRes = ModbusHelper.ReadCoils(stationNo, 16, 1); + btn_ElectricMotor1Front.Background = new SolidColorBrush(CoilsRes[0] ? Colors.Green : Colors.Red); + } + /// + /// 电机1反传 + /// + /// + /// + private void Button_Click_2(object sender, RoutedEventArgs e) + { + ModbusHelper.WriteSingleCoil(stationNo, 16, false); + Thread.Sleep(100); + //读取当前状态 + var CoilsResBefor = ModbusHelper.ReadCoils(stationNo, 17, 1); + Thread.Sleep(100); + //写入线圈 + ModbusHelper.WriteSingleCoil(stationNo, 17, !CoilsResBefor[0]); + Thread.Sleep(100); + //读取写入后状态 + var CoilsRes = ModbusHelper.ReadCoils(stationNo, 17, 1); + btn_ElectricMotor1Back.Background = new SolidColorBrush(CoilsRes[0] ? Colors.Green : Colors.Red); + } + + /// + /// 电机2正传 + /// + /// + /// + private void Button_Click_3(object sender, RoutedEventArgs e) + { + ModbusHelper.WriteSingleCoil(stationNo, 19, false); + Thread.Sleep(100); + //读取当前状态 + var CoilsResBefor = ModbusHelper.ReadCoils(stationNo, 18, 1); + Thread.Sleep(100); + //写入线圈 + ModbusHelper.WriteSingleCoil(stationNo, 18, !CoilsResBefor[0]); + Thread.Sleep(100); + //读取写入后状态 + var CoilsRes = ModbusHelper.ReadCoils(stationNo, 18, 1); + btn_ElectricMotor2Front.Background = new SolidColorBrush(CoilsRes[0] ? Colors.Green : Colors.Red); + } + + /// + /// 电机2反传 + /// + /// + /// + private void Button_Click_4(object sender, RoutedEventArgs e) + { + ModbusHelper.WriteSingleCoil(stationNo, 18, false); + Thread.Sleep(100); + //读取当前状态 + var CoilsResBefor = ModbusHelper.ReadCoils(stationNo, 19, 1); + Thread.Sleep(100); + //写入线圈 + ModbusHelper.WriteSingleCoil(stationNo, 19, !CoilsResBefor[0]); + Thread.Sleep(100); + //读取写入后状态 + var CoilsRes = ModbusHelper.ReadCoils(stationNo, 19, 1); + btn_ElectricMotor2Back.Background = new SolidColorBrush(CoilsRes[0] ? Colors.Green : Colors.Red); + } + /// + /// 电机3正传 + /// + /// + /// + private void Button_Click_5(object sender, RoutedEventArgs e) + { + ModbusHelper.WriteSingleCoil(stationNo, 21, false); + Thread.Sleep(100); + //读取当前状态 + var CoilsResBefor = ModbusHelper.ReadCoils(stationNo, 20, 1); + Thread.Sleep(100); + //写入线圈 + ModbusHelper.WriteSingleCoil(stationNo, 20, !CoilsResBefor[0]); + Thread.Sleep(100); + //读取写入后状态 + var CoilsRes = ModbusHelper.ReadCoils(stationNo, 20, 1); + btn_ElectricMotor3Front.Background = new SolidColorBrush(CoilsRes[0] ? Colors.Green : Colors.Red); + } + /// + /// 电机3反传 + /// + /// + /// + private void Button_Click_6(object sender, RoutedEventArgs e) + { + ModbusHelper.WriteSingleCoil(stationNo, 20, false); + Thread.Sleep(100); + //读取当前状态 + var CoilsResBefor = ModbusHelper.ReadCoils(stationNo, 21, 1); + Thread.Sleep(100); + //写入线圈 + ModbusHelper.WriteSingleCoil(stationNo, 21, !CoilsResBefor[0]); + Thread.Sleep(100); + //读取写入后状态 + var CoilsRes = ModbusHelper.ReadCoils(stationNo, 21, 1); + btn_ElectricMotor3Back.Background = new SolidColorBrush(CoilsRes[0] ? Colors.Green : Colors.Red); + } + /// + /// 电机4正传 + /// + /// + /// + private void Button_Click_7(object sender, RoutedEventArgs e) + { + ModbusHelper.WriteSingleCoil(stationNo, 23, false); + Thread.Sleep(100); + //读取当前状态 + var CoilsResBefor = ModbusHelper.ReadCoils(stationNo, 22, 1); + Thread.Sleep(100); + //写入线圈 + ModbusHelper.WriteSingleCoil(stationNo, 22, !CoilsResBefor[0]); + Thread.Sleep(100); + //读取写入后状态 + var CoilsRes = ModbusHelper.ReadCoils(stationNo, 22, 1); + btn_ElectricMotor4Front.Background = new SolidColorBrush(CoilsRes[0] ? Colors.Green : Colors.Red); + } + /// + /// 电机4反传 + /// + /// + /// + private void Button_Click_8(object sender, RoutedEventArgs e) + { + ModbusHelper.WriteSingleCoil(stationNo, 22, false); + Thread.Sleep(100); + //读取当前状态 + var CoilsResBefor = ModbusHelper.ReadCoils(stationNo, 23, 1); + Thread.Sleep(100); + //写入线圈 + ModbusHelper.WriteSingleCoil(stationNo, 23, !CoilsResBefor[0]); + Thread.Sleep(100); + //读取写入后状态 + var CoilsRes = ModbusHelper.ReadCoils(stationNo, 23, 1); + btn_ElectricMotor4Back.Background = new SolidColorBrush(CoilsRes[0] ? Colors.Green : Colors.Red); + } + + /// + /// 通道1速度 + /// + /// + /// + private void Button_Click_9(object sender, RoutedEventArgs e) + { + ModbusHelper.WriteSingleRegister(stationNo, 23, Convert.ToUInt16(txt_channel1Speed.Text)); + Thread.Sleep(100); + var res = ModbusHelper.ReadHoldingRegisters(stationNo, 23, 1); + txt_channel1Speed.Text = res[0].ToString(); + } + /// + /// 通道2速度 + /// + /// + /// + private void Button_Click_10(object sender, RoutedEventArgs e) + { + ModbusHelper.WriteSingleRegister(stationNo, 27, Convert.ToUInt16(txt_channel2Speed.Text)); + Thread.Sleep(100); + var res = ModbusHelper.ReadHoldingRegisters(stationNo, 27, 1); + txt_channel2Speed.Text = res[0].ToString(); + } + /// + /// 通道3速度 + /// + /// + /// + private void Button_Click_11(object sender, RoutedEventArgs e) + { + ModbusHelper.WriteSingleRegister(stationNo, 31, Convert.ToUInt16(txt_channel3Speed.Text)); + Thread.Sleep(100); + var res = ModbusHelper.ReadHoldingRegisters(stationNo, 31, 1); + txt_channel3Speed.Text = res[0].ToString(); + } + /// + /// 通道4速度 + /// + /// + /// + private void Button_Click_12(object sender, RoutedEventArgs e) + { + ModbusHelper.WriteSingleRegister(stationNo, 35, Convert.ToUInt16(txt_channel4Speed.Text)); + Thread.Sleep(100); + var res = ModbusHelper.ReadHoldingRegisters(stationNo, 35, 1); + txt_channel4Speed.Text = res[0].ToString(); + } + bool isStill = true; + Thread alwaysThread; + private void Button_Click_13(object sender, RoutedEventArgs e) + { + + if (btn_always.Content.ToString() == "循环测试开始") + { + isStill = true; + btn_always.Content = "循环测试停止"; + alwaysThread= new Thread(() => + { + + while (isStill) + { + for (int i = 0; i < 24; i++) + { + if (i == 17 || i == 19 || i == 21 || i == 23) + continue; + ModbusHelper.WriteSingleCoil(stationNo, (ushort)i, true); + Thread.Sleep(500); + } + Thread.Sleep(5000); + for (int i = 0; i < 24; i++) + { + ModbusHelper.WriteSingleCoil(stationNo, (ushort)i, false); + Thread.Sleep(500); + } + + Thread.Sleep(5000); + } + + }); + alwaysThread.Start(); + } + else + { + isStill = false; + btn_always.Content = "循环测试开始"; + alwaysThread.Abort(); + } + + } + + Thread FrontBackThread; + + private void btn_FrontBack_Click(object sender, RoutedEventArgs e) + { + if (btn_FrontBack.Content.ToString() == "正反转测试开始") + { + isStill = true; + btn_FrontBack.Content = "正反转测试停止"; + FrontBackThread= new Thread(() => + { + + while (isStill) + { + + + + //启动通道 + for (int i = 0; i < 16; i++) + { + + ModbusHelper.WriteSingleCoil(stationNo, (ushort)i, true); + + } + //正转测试 + ModbusHelper.WriteSingleRegister(stationNo, 23, 30); + Thread.Sleep(1000); + ModbusHelper.WriteSingleCoil(stationNo, 16,true); + ModbusHelper.WriteSingleRegister(stationNo, 23, 100); + + ModbusHelper.WriteSingleRegister(stationNo, 27, 30); + Thread.Sleep(1000); + ModbusHelper.WriteSingleCoil(stationNo, 18, true); + ModbusHelper.WriteSingleRegister(stationNo, 27, 100); + + ModbusHelper.WriteSingleRegister(stationNo, 31, 30); + Thread.Sleep(1000); + ModbusHelper.WriteSingleCoil(stationNo, 20, true); + ModbusHelper.WriteSingleRegister(stationNo, 31, 100); + + + ModbusHelper.WriteSingleRegister(stationNo, 35, 30); + Thread.Sleep(1000); + ModbusHelper.WriteSingleCoil(stationNo, 22, true); + ModbusHelper.WriteSingleRegister(stationNo, 35, 100); + Thread.Sleep(2000); + //关闭正转 + + ModbusHelper.WriteSingleCoil(stationNo, 16, false); + ModbusHelper.WriteSingleCoil(stationNo, 18, false); + ModbusHelper.WriteSingleCoil(stationNo, 20, false); + ModbusHelper.WriteSingleCoil(stationNo, 22, false); + Thread.Sleep(1000); + + //反转测试 + ModbusHelper.WriteSingleRegister(stationNo, 23, 30); + Thread.Sleep(1000); + ModbusHelper.WriteSingleCoil(stationNo, 17, true); + ModbusHelper.WriteSingleRegister(stationNo, 23, 100); + + ModbusHelper.WriteSingleRegister(stationNo, 27, 30); + Thread.Sleep(1000); + ModbusHelper.WriteSingleCoil(stationNo, 19, true); + ModbusHelper.WriteSingleRegister(stationNo, 27, 100); + + ModbusHelper.WriteSingleRegister(stationNo, 31, 30); + Thread.Sleep(1000); + ModbusHelper.WriteSingleCoil(stationNo, 21, true); + ModbusHelper.WriteSingleRegister(stationNo, 31, 100); + + + ModbusHelper.WriteSingleRegister(stationNo, 35, 30); + Thread.Sleep(1000); + ModbusHelper.WriteSingleCoil(stationNo, 23, true); + ModbusHelper.WriteSingleRegister(stationNo, 35, 100); + Thread.Sleep(2000); + //关闭通道 + for (int i = 0; i < 16; i++) + { + + ModbusHelper.WriteSingleCoil(stationNo, (ushort)i, false); + + } + + //关闭反转 + Thread.Sleep(2000); + ModbusHelper.WriteSingleCoil(stationNo, 17, false); + ModbusHelper.WriteSingleCoil(stationNo, 19, false); + ModbusHelper.WriteSingleCoil(stationNo, 21, false); + ModbusHelper.WriteSingleCoil(stationNo, 23, false); + Thread.Sleep(1000); + + } + + }); + FrontBackThread.Start(); + } + else + { + isStill = false; + btn_FrontBack.Content = "正反转测试开始"; + FrontBackThread.Abort(); + + //停止通道 + for (int i = 0; i < 24; i++) + { + + ModbusHelper.WriteSingleCoil(stationNo, (ushort)i, false); + + + } + + ModbusHelper.WriteSingleCoil(stationNo, 16, false); + ModbusHelper.WriteSingleCoil(stationNo, 18, false); + ModbusHelper.WriteSingleCoil(stationNo, 20, false); + ModbusHelper.WriteSingleCoil(stationNo, 22, false); + + ModbusHelper.WriteSingleCoil(stationNo, 17, false); + ModbusHelper.WriteSingleCoil(stationNo, 19, false); + ModbusHelper.WriteSingleCoil(stationNo, 21, false); + ModbusHelper.WriteSingleCoil(stationNo, 23, false); + + } + + } + } +} diff --git a/ControlcBoardsTest/ModbusHelper.cs b/ControlcBoardsTest/ModbusHelper.cs new file mode 100644 index 0000000..3eb83af --- /dev/null +++ b/ControlcBoardsTest/ModbusHelper.cs @@ -0,0 +1,84 @@ + +using System; +using System.Collections.Generic; +using System.IO.Ports; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.IO.Ports; +using Modbus.Device; +using HslCommunication.ModBus; // 引入 NModbus 库 + + +namespace ControlcBoardsTest +{ + internal class ModbusHelper + { + + + /// + /// 私有ModbusRTU主站字段 + /// + public static ModbusRtu master=new ModbusRtu(2); + + + /// + /// 写入单个线圈 + /// + public static void WriteSingleCoil(byte stationNo, ushort Adr, bool Value) + { + + master.Write(Adr.ToString(), Value); + } + + public static StopBits GetSelectedStopBits(double stopBits) + { + switch (stopBits) + { + case 1: + return StopBits.One; + case 1.5: + return StopBits.OnePointFive; + case 2: + return StopBits.Two; + default: + return StopBits.One; + } + } + /// + /// 写入单个寄存器 + /// + public static void WriteSingleRegister(byte stationNo, ushort Value, ushort result) + { + + master.Write(Value.ToString(), result); + } + + + /// + /// 读取输出线圈 + /// + /// + public static bool[] ReadCoils(byte stationNo, ushort Value, ushort result) + { + + var data = master.ReadCoil(Value.ToString(), result); + + + return master.ReadCoil(Value.ToString(), result).Content; + } + + + + /// + /// 读取保持型寄存器 + /// + /// + public static ushort[] ReadHoldingRegisters(byte stationNo, ushort Value, ushort result) + { + return master.ReadUInt16(Value.ToString(), result).Content; + } + + + } +} diff --git a/ControlcBoardsTest/Properties/AssemblyInfo.cs b/ControlcBoardsTest/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..b2dca50 --- /dev/null +++ b/ControlcBoardsTest/Properties/AssemblyInfo.cs @@ -0,0 +1,52 @@ +using System.Reflection; +using System.Resources; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Windows; + +// 有关程序集的一般信息由以下 +// 控制。更改这些特性值可修改 +// 与程序集关联的信息。 +[assembly: AssemblyTitle("ControlcBoardsTest")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("ControlcBoardsTest")] +[assembly: AssemblyCopyright("Copyright © 2025")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// 将 ComVisible 设置为 false 会使此程序集中的类型 +//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 +//请将此类型的 ComVisible 特性设置为 true。 +[assembly: ComVisible(false)] + +//若要开始生成可本地化的应用程序,请设置 +//.csproj 文件中的 CultureYouAreCodingWith +//在 中。例如,如果你使用的是美国英语。 +//使用的是美国英语,请将 设置为 en-US。 然后取消 +//对以下 NeutralResourceLanguage 特性的注释。 更新 +//以下行中的“en-US”以匹配项目文件中的 UICulture 设置。 + +//[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)] + + +[assembly: ThemeInfo( + ResourceDictionaryLocation.None, //主题特定资源词典所处位置 + //(未在页面中找到资源时使用, + //或应用程序资源字典中找到时使用) + ResourceDictionaryLocation.SourceAssembly //常规资源词典所处位置 + //(未在页面中找到资源时使用, + //、应用程序或任何主题专用资源字典中找到时使用) +)] + + +// 程序集的版本信息由下列四个值组成: +// +// 主版本 +// 次版本 +// 生成号 +// 修订号 +// +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/ControlcBoardsTest/Properties/Resources.Designer.cs b/ControlcBoardsTest/Properties/Resources.Designer.cs new file mode 100644 index 0000000..fd0132a --- /dev/null +++ b/ControlcBoardsTest/Properties/Resources.Designer.cs @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// 此代码由工具生成。 +// 运行时版本:4.0.30319.42000 +// +// 对此文件的更改可能会导致不正确的行为,并且如果 +// 重新生成代码,这些更改将会丢失。 +// +//------------------------------------------------------------------------------ + +namespace ControlcBoardsTest.Properties { + using System; + + + /// + /// 一个强类型的资源类,用于查找本地化的字符串等。 + /// + // 此类是由 StronglyTypedResourceBuilder + // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。 + // 若要添加或移除成员,请编辑 .ResX 文件,然后重新运行 ResGen + // (以 /str 作为命令选项),或重新生成 VS 项目。 + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() { + } + + /// + /// 返回此类使用的缓存的 ResourceManager 实例。 + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("ControlcBoardsTest.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// 重写当前线程的 CurrentUICulture 属性,对 + /// 使用此强类型资源类的所有资源查找执行重写。 + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + } +} diff --git a/ControlcBoardsTest/Properties/Resources.resx b/ControlcBoardsTest/Properties/Resources.resx new file mode 100644 index 0000000..af7dbeb --- /dev/null +++ b/ControlcBoardsTest/Properties/Resources.resx @@ -0,0 +1,117 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/ControlcBoardsTest/Properties/Settings.Designer.cs b/ControlcBoardsTest/Properties/Settings.Designer.cs new file mode 100644 index 0000000..71044c1 --- /dev/null +++ b/ControlcBoardsTest/Properties/Settings.Designer.cs @@ -0,0 +1,26 @@ +//------------------------------------------------------------------------------ +// +// 此代码由工具生成。 +// 运行时版本:4.0.30319.42000 +// +// 对此文件的更改可能会导致不正确的行为,并且如果 +// 重新生成代码,这些更改将会丢失。 +// +//------------------------------------------------------------------------------ + +namespace ControlcBoardsTest.Properties { + + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.11.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default { + get { + return defaultInstance; + } + } + } +} diff --git a/ControlcBoardsTest/Properties/Settings.settings b/ControlcBoardsTest/Properties/Settings.settings new file mode 100644 index 0000000..033d7a5 --- /dev/null +++ b/ControlcBoardsTest/Properties/Settings.settings @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/ControlcBoardsTest/packages.config b/ControlcBoardsTest/packages.config new file mode 100644 index 0000000..95e7aaf --- /dev/null +++ b/ControlcBoardsTest/packages.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file