diff --git a/ModbusDemo/App.xaml.cs b/ModbusDemo/App.xaml.cs index 3361c7a..6dd09c6 100644 --- a/ModbusDemo/App.xaml.cs +++ b/ModbusDemo/App.xaml.cs @@ -12,6 +12,8 @@ using System.Data; using System.DirectoryServices; using System.IO; using System.IO.Ports; +using System.Security.Cryptography; +using System.Text; using System.Windows; namespace ModbusDemo @@ -48,7 +50,7 @@ namespace ModbusDemo //将读线圈注册 containerRegistry.Register(); //附加功能如何读取单数寄存器; - containerRegistry.RegisterForNavigation(); + //containerRegistry.RegisterForNavigation(); // 1. 加载配置文件 @@ -56,13 +58,14 @@ namespace ModbusDemo .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json") .Build(); - - // 2. 配置 DbContextOptions + // 2.解密连接字符串 + string decryptedConnection = Decrypt(configuration.GetConnectionString("ConnStr")); + // 3. 配置 DbContextOptions var options = new DbContextOptionsBuilder() - .UseSqlServer(configuration.GetConnectionString("ConnStr")) + .UseSqlServer(decryptedConnection) .Options; - // 3. 注册 DbContext + // 4. 注册 DbContext containerRegistry.Register(() => new ModbusDbContext(options)); } /// @@ -74,10 +77,40 @@ namespace ModbusDemo mainWindowViewModel!.DefultNaigation(); base.OnInitialized(); } + //硬编码 + private static readonly byte[] Key = Encoding.UTF8.GetBytes("8Jn3pQ7sV9y$B&E)"); // 16字节,AES-128 + private static readonly byte[] IV = Encoding.UTF8.GetBytes("2r5u8x/A?D*G-KaP"); // 16字节 + + /// + /// 解密字符串 + /// + /// 加密后的Base64字符串 + /// 解密后的原始字符串 + public static string Decrypt(string encryptedText) + { + if (string.IsNullOrEmpty(encryptedText)) + return string.Empty; + using (Aes aes = Aes.Create()) + { + aes.Key = Key; + aes.IV = IV; + // 创建解密器 + ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV); - + using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(encryptedText))) + { + using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read)) + { + using (StreamReader sr = new StreamReader(cs)) + { + return sr.ReadToEnd(); + } + } + } + } + } } } diff --git a/ModbusDemo/Device/ModbusRTU.cs b/ModbusDemo/Device/ModbusRTU.cs index ede4987..a1e6e6a 100644 --- a/ModbusDemo/Device/ModbusRTU.cs +++ b/ModbusDemo/Device/ModbusRTU.cs @@ -19,7 +19,7 @@ namespace ModbusDemo.Device //用于操作数据库 private ModbusDbContext _modbusDbContext; //TODO,修改 - private SerialPortAdapter _portAdapter; + //private SerialPortAdapter _portAdapter; public ModbusRTU(SerialPort serialPort, ModbusDbContext modbusDbContext) { @@ -72,8 +72,7 @@ namespace ModbusDemo.Device responseData.Clear(); // 清除输入缓冲区残留数据 _serialPort.DiscardInBuffer(); - } - + } try { _serialPort.Write(sendByte, 0, sendByte.Length); @@ -622,5 +621,7 @@ namespace ModbusDemo.Device return sb.ToString(); } + + } } diff --git a/ModbusDemo/ErrorCode/ErrorCode.cs b/ModbusDemo/ErrorCode/ErrorCode.cs index 0018412..919f480 100644 --- a/ModbusDemo/ErrorCode/ErrorCode.cs +++ b/ModbusDemo/ErrorCode/ErrorCode.cs @@ -31,8 +31,7 @@ namespace ModbusDemo.ErrorCode // 重写 ToString 方法 public override string ToString() => $"Error 0x{Code:X2}: {Description}"; - // 隐式转换为 byte - public static implicit operator byte(ErrorCode error) => error.Code; + // 根据错误码值查找错误码 public static ErrorCode FromByte(byte code) => AllErrors.FirstOrDefault(e => e.Code == code); diff --git a/ModbusDemo/MainWindow.xaml b/ModbusDemo/MainWindow.xaml index f38d325..803a2ce 100644 --- a/ModbusDemo/MainWindow.xaml +++ b/ModbusDemo/MainWindow.xaml @@ -10,8 +10,8 @@ xmlns:prism="http://prismlibrary.com/" xmlns:viewmodel="clr-namespace:ModbusDemo.VIewModel" Title="MainWindow" - Width="900" - Height="600" + Width="1000" + Height="800" d:DataContext="{d:DesignInstance Type=viewmodel:MainWindowViewModel, IsDesignTimeCreatable=True}" WindowStartupLocation="CenterScreen" diff --git a/ModbusDemo/ModbusDemo.csproj b/ModbusDemo/ModbusDemo.csproj index 841bdf7..2db4493 100644 --- a/ModbusDemo/ModbusDemo.csproj +++ b/ModbusDemo/ModbusDemo.csproj @@ -10,6 +10,8 @@ + + @@ -25,7 +27,6 @@ - diff --git a/ModbusDemo/Model/ErrorCode.cs b/ModbusDemo/Model/ErrorCode.cs deleted file mode 100644 index 340c416..0000000 --- a/ModbusDemo/Model/ErrorCode.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ModbusDemo.Model -{ - -} diff --git a/ModbusDemo/Uitls/CheckData.cs b/ModbusDemo/Uitls/CheckData.cs index 8c46307..2a20de3 100644 --- a/ModbusDemo/Uitls/CheckData.cs +++ b/ModbusDemo/Uitls/CheckData.cs @@ -22,7 +22,7 @@ namespace ModbusDemo.Uitls return false; } // 检查数组长度是否足够 - if (response.Length > 1) + if (response.Length > 3) { byte secondByte = response[1]; // 获取第二个字节(索引为1) diff --git a/ModbusDemo/VIew/CoilUC.xaml b/ModbusDemo/VIew/CoilUC.xaml index 30de8d1..6ac9787 100644 --- a/ModbusDemo/VIew/CoilUC.xaml +++ b/ModbusDemo/VIew/CoilUC.xaml @@ -10,8 +10,8 @@ d:DataContext="{d:DesignInstance Type=viewmodel:CoilUCViewModel, IsDesignTimeCreatable=True}" d:Background="White" - d:DesignHeight="450" - d:DesignWidth="800" + d:DesignHeight="800" + d:DesignWidth="1000" mc:Ignorable="d">