From cc5c5cfa42aee60d0837a0c8eecc1f6338450d30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=94=84=E5=A4=B4?= Date: Mon, 19 Aug 2024 15:14:32 +0800 Subject: [PATCH] =?UTF-8?q?=E7=9E=8E=E6=90=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NDSD-Screwdriver/AppTool.cs | 7 ++++++- NDSD-Screwdriver/MainForm.cs | 13 +++++++++++-- NDSD-Screwdriver/NDSD_Screwdriver.csproj | 2 +- .../{SerialPortFactory.cs => SerialPortFactory1.cs} | 4 ++-- 4 files changed, 20 insertions(+), 6 deletions(-) rename NDSD-Screwdriver/Tool/{SerialPortFactory.cs => SerialPortFactory1.cs} (95%) diff --git a/NDSD-Screwdriver/AppTool.cs b/NDSD-Screwdriver/AppTool.cs index 02d3071..3345b40 100644 --- a/NDSD-Screwdriver/AppTool.cs +++ b/NDSD-Screwdriver/AppTool.cs @@ -1,4 +1,6 @@ -namespace NDSD_Screwdriver +using System; + +namespace NDSD_Screwdriver { using System.Configuration; public class AppTool @@ -12,5 +14,8 @@ { return ConfigurationManager.AppSettings["db"].ToString(); } + + public static String GetCom=> ConfigurationManager.AppSettings["com"].ToString(); + } } \ No newline at end of file diff --git a/NDSD-Screwdriver/MainForm.cs b/NDSD-Screwdriver/MainForm.cs index a9b474c..bf60452 100644 --- a/NDSD-Screwdriver/MainForm.cs +++ b/NDSD-Screwdriver/MainForm.cs @@ -87,8 +87,17 @@ namespace NDSD_Screwdriver MessageBox.Show("服务端打开失败!"); FormUtils.LogInsert(LogContext, "服务端打开失败"); } - serialPort = new SerialPortFactory(); - StartLongRunningTask(); + + try + { + serialPort = new SerialPortFactory(AppTool.GetCom); + StartLongRunningTask(); + } + catch (Exception e) + { + MessageBox.Show(e.Message); + FormUtils.LogInsert(LogContext, e.Message); + } } private void MainForm_FormClosed(object sender, FormClosedEventArgs e) diff --git a/NDSD-Screwdriver/NDSD_Screwdriver.csproj b/NDSD-Screwdriver/NDSD_Screwdriver.csproj index 7b06ded..fa75ce9 100644 --- a/NDSD-Screwdriver/NDSD_Screwdriver.csproj +++ b/NDSD-Screwdriver/NDSD_Screwdriver.csproj @@ -123,7 +123,7 @@ - + FrmSetting.cs diff --git a/NDSD-Screwdriver/Tool/SerialPortFactory.cs b/NDSD-Screwdriver/Tool/SerialPortFactory1.cs similarity index 95% rename from NDSD-Screwdriver/Tool/SerialPortFactory.cs rename to NDSD-Screwdriver/Tool/SerialPortFactory1.cs index 29503c7..bd97c8b 100644 --- a/NDSD-Screwdriver/Tool/SerialPortFactory.cs +++ b/NDSD-Screwdriver/Tool/SerialPortFactory1.cs @@ -15,7 +15,7 @@ namespace NDSD_Screwdriver.Tool { SerialPortClient clientSerialPortClient = new SerialPortClient(); private IWaitingClient waitClient; - public SerialPortFactory(string portName="COM13") + public SerialPortFactory(string portName) { clientSerialPortClient.Connecting = (client, e) => EasyTask.CompletedTask;//即将连接到端口 clientSerialPortClient.Connected = (client, e) => { return EasyTask.CompletedTask; };//成功连接到端口 @@ -25,7 +25,7 @@ namespace NDSD_Screwdriver.Tool BaudRate = 115200, //波特率 DataBits = 8, //数据位 Parity = System.IO.Ports.Parity.None, //校验位 - PortName = "COM13", //COM + PortName =portName, StopBits = System.IO.Ports.StopBits.One //停止位 }).SetSerialDataHandlingAdapter(() => new MyFixedHeaderCustomDataHandlingAdapter()));