@@ -1,11 +1,16 @@ | |||||
using ModbusDemo.Device; | |||||
using Microsoft.EntityFrameworkCore; | |||||
using Microsoft.Extensions.Configuration; | |||||
using ModbusDemo.Device; | |||||
using ModbusDemo.Model; | |||||
using ModbusDemo.VIew; | using ModbusDemo.VIew; | ||||
using ModbusDemo.VIewModel; | using ModbusDemo.VIewModel; | ||||
using Prism.DryIoc; | using Prism.DryIoc; | ||||
using Prism.Ioc; | using Prism.Ioc; | ||||
using System; | |||||
using System.Configuration; | using System.Configuration; | ||||
using System.Data; | using System.Data; | ||||
using System.DirectoryServices; | using System.DirectoryServices; | ||||
using System.IO; | |||||
using System.IO.Ports; | using System.IO.Ports; | ||||
using System.Windows; | using System.Windows; | ||||
@@ -38,12 +43,26 @@ namespace ModbusDemo | |||||
containerRegistry.RegisterForNavigation<RegisterUC, RegisterUCViewModel>(); | containerRegistry.RegisterForNavigation<RegisterUC, RegisterUCViewModel>(); | ||||
//将寄存器操作页面和寄存器操作页面的VM结合起来 | //将寄存器操作页面和寄存器操作页面的VM结合起来 | ||||
containerRegistry.RegisterForNavigation<SettingsUC, SettingsUCViewModel>(); | containerRegistry.RegisterForNavigation<SettingsUC, SettingsUCViewModel>(); | ||||
//将窗口注册为全局唯一的单例 | |||||
//将串口注册为全局唯一的单例 | |||||
containerRegistry.RegisterSingleton<SerialPort>(); | containerRegistry.RegisterSingleton<SerialPort>(); | ||||
//将读线圈注册 | //将读线圈注册 | ||||
containerRegistry.Register<ModbusRTU>(); | containerRegistry.Register<ModbusRTU>(); | ||||
// 1. 加载配置文件 | |||||
var configuration = new ConfigurationBuilder() | |||||
.SetBasePath(Directory.GetCurrentDirectory()) | |||||
.AddJsonFile("appsettings.json") | |||||
.Build(); | |||||
// 2. 配置 DbContextOptions | |||||
var options = new DbContextOptionsBuilder<ModbusDbContext>() | |||||
.UseSqlServer(configuration.GetConnectionString("ConnStr")) | |||||
.Options; | |||||
// 3. 注册 DbContext(作用域生命周期) | |||||
containerRegistry.Register<ModbusDbContext>(() => new ModbusDbContext(options)); | |||||
} | } | ||||
/// <summary> | /// <summary> | ||||
/// 程序打开默认是设置界面,来设置串口的各个东西 | /// 程序打开默认是设置界面,来设置串口的各个东西 | ||||
@@ -1,4 +1,5 @@ | |||||
using ModbusDemo.Uitls; | |||||
using ModbusDemo.Model; | |||||
using ModbusDemo.Uitls; | |||||
using System; | using System; | ||||
using System.Collections.Generic; | using System.Collections.Generic; | ||||
using System.IO.Ports; | using System.IO.Ports; | ||||
@@ -11,11 +12,14 @@ namespace ModbusDemo.Device | |||||
{ | { | ||||
public class ModbusRTU : IModbusRTU | public class ModbusRTU : IModbusRTU | ||||
{ | { | ||||
private SerialPort _serialPort; | |||||
public ModbusRTU(SerialPort serialPort) | |||||
private SerialPort _serialPort; | |||||
//用于操作数据库 | |||||
private ModbusDbContext _modbusDbContext; | |||||
public ModbusRTU(SerialPort serialPort,ModbusDbContext modbusDbContext) | |||||
{ | { | ||||
_serialPort = serialPort; | _serialPort = serialPort; | ||||
_modbusDbContext = modbusDbContext; | |||||
} | } | ||||
/// <summary> | /// <summary> | ||||
/// 用来发送读写线圈的指令 | /// 用来发送读写线圈的指令 | ||||
@@ -58,7 +62,8 @@ namespace ModbusDemo.Device | |||||
Thread.Sleep(300); | Thread.Sleep(300); | ||||
byte[] response = new byte[_serialPort.BytesToRead]; | byte[] response = new byte[_serialPort.BytesToRead]; | ||||
_serialPort.Read(response, 0, _serialPort.BytesToRead); | _serialPort.Read(response, 0, _serialPort.BytesToRead); | ||||
return ParseCoilresponse(response, numberOfPoints); | |||||
return ParseCoilresponse(sendByte, response, numberOfPoints); | |||||
} | } | ||||
@@ -66,8 +71,19 @@ namespace ModbusDemo.Device | |||||
} | } | ||||
public bool[] ParseCoilresponse(byte[] response, ushort numberOfPoints) | |||||
public bool[] ParseCoilresponse(byte[] sendByte, byte[] response, ushort numberOfPoints) | |||||
{ | { | ||||
//将操作的指令,插入数据库 | |||||
string RequestStr = ByteArrayToString(sendByte); | |||||
string responseStr = ByteArrayToString(response); | |||||
ModbusLog m = new ModbusLog(); | |||||
m.OperationType = "读线圈"; | |||||
m.ResponseData = responseStr; | |||||
m.RequestData = responseStr; | |||||
_modbusDbContext.Add(m); | |||||
_modbusDbContext.SaveChanges(); | |||||
//判断发送回来的数据是否正确 | //判断发送回来的数据是否正确 | ||||
CheckData.CheckResponse(response); | CheckData.CheckResponse(response); | ||||
@@ -164,6 +180,16 @@ namespace ModbusDemo.Device | |||||
byte[] response = new byte[_serialPort.BytesToRead]; | byte[] response = new byte[_serialPort.BytesToRead]; | ||||
_serialPort.Read(response, 0, _serialPort.BytesToRead); | _serialPort.Read(response, 0, _serialPort.BytesToRead); | ||||
//将操作的指令,插入数据库 | |||||
string RequestStr = ByteArrayToString(sendByte); | |||||
string responseStr = ByteArrayToString(response); | |||||
ModbusLog m = new ModbusLog(); | |||||
m.OperationType = "写线圈"; | |||||
m.ResponseData = responseStr; | |||||
m.RequestData = responseStr; | |||||
_modbusDbContext.Add(m); | |||||
_modbusDbContext.SaveChanges(); | |||||
//判断发送回来的数据是否正确 | //判断发送回来的数据是否正确 | ||||
CheckData.CheckResponse(response); | CheckData.CheckResponse(response); | ||||
@@ -212,13 +238,23 @@ namespace ModbusDemo.Device | |||||
Thread.Sleep(300); | Thread.Sleep(300); | ||||
byte[] response = new byte[_serialPort.BytesToRead]; | byte[] response = new byte[_serialPort.BytesToRead]; | ||||
_serialPort.Read(response, 0, _serialPort.BytesToRead); | _serialPort.Read(response, 0, _serialPort.BytesToRead); | ||||
return ParseRegistersresponse(response, numberOfPoints); | |||||
return ParseRegistersresponse(sendByte, response, numberOfPoints); | |||||
} | } | ||||
return null; | return null; | ||||
} | } | ||||
public ushort[] ParseRegistersresponse(byte[] response, ushort numberOfPoints) | |||||
public ushort[] ParseRegistersresponse(byte[] sendByte, byte[] response, ushort numberOfPoints) | |||||
{ | { | ||||
//将操作的指令,插入数据库 | |||||
string RequestStr = ByteArrayToString(sendByte); | |||||
string responseStr = ByteArrayToString(response); | |||||
ModbusLog m = new ModbusLog(); | |||||
m.OperationType = "读寄存器"; | |||||
m.ResponseData = responseStr; | |||||
m.RequestData = responseStr; | |||||
_modbusDbContext.Add(m); | |||||
_modbusDbContext.SaveChanges(); | |||||
//判断发送回来的数据是否正确 | //判断发送回来的数据是否正确 | ||||
CheckData.CheckResponse(response); | CheckData.CheckResponse(response); | ||||
@@ -283,6 +319,15 @@ namespace ModbusDemo.Device | |||||
byte[] response = new byte[_serialPort.BytesToRead]; | byte[] response = new byte[_serialPort.BytesToRead]; | ||||
_serialPort.Read(response, 0, _serialPort.BytesToRead); | _serialPort.Read(response, 0, _serialPort.BytesToRead); | ||||
//将操作的指令,插入数据库 | |||||
string RequestStr = ByteArrayToString(sendByte); | |||||
string responseStr = ByteArrayToString(response); | |||||
ModbusLog m = new ModbusLog(); | |||||
m.OperationType = "写寄存器"; | |||||
m.ResponseData = responseStr; | |||||
m.RequestData = responseStr; | |||||
_modbusDbContext.Add(m); | |||||
_modbusDbContext.SaveChanges(); | |||||
//判断发送回来的数据是否正确 | //判断发送回来的数据是否正确 | ||||
CheckData.CheckResponse(response); | CheckData.CheckResponse(response); | ||||
@@ -293,5 +338,25 @@ namespace ModbusDemo.Device | |||||
} | } | ||||
} | } | ||||
} | } | ||||
static string ByteArrayToString(byte[] byteArray) | |||||
{ | |||||
if (byteArray == null || byteArray.Length == 0) | |||||
return string.Empty; | |||||
System.Text.StringBuilder sb = new System.Text.StringBuilder(); | |||||
// 处理第一个元素(无前导空格) | |||||
sb.Append(byteArray[0].ToString("X2")); | |||||
// 处理后续元素(每个前面加空格) | |||||
for (int i = 1; i < byteArray.Length; i++) | |||||
{ | |||||
sb.Append(' '); | |||||
sb.Append(byteArray[i].ToString("X2")); | |||||
} | |||||
return sb.ToString(); | |||||
} | |||||
} | } | ||||
} | } |
@@ -10,8 +10,17 @@ | |||||
<ItemGroup> | <ItemGroup> | ||||
<PackageReference Include="MaterialDesignThemes" Version="5.2.1" /> | <PackageReference Include="MaterialDesignThemes" Version="5.2.1" /> | ||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.27" /> | |||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.7" /> | |||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.7" /> | |||||
<PackageReference Include="Prism.DryIoc" Version="8.1.97" /> | <PackageReference Include="Prism.DryIoc" Version="8.1.97" /> | ||||
<PackageReference Include="System.IO.Ports" Version="9.0.7" /> | <PackageReference Include="System.IO.Ports" Version="9.0.7" /> | ||||
</ItemGroup> | </ItemGroup> | ||||
<ItemGroup> | |||||
<None Update="appsettings.json"> | |||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | |||||
</None> | |||||
</ItemGroup> | |||||
</Project> | </Project> |
@@ -0,0 +1,20 @@ | |||||
using Microsoft.EntityFrameworkCore; | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Linq; | |||||
using System.Text; | |||||
using System.Threading.Tasks; | |||||
namespace ModbusDemo.Model | |||||
{ | |||||
public class ModbusDbContext:DbContext | |||||
{ | |||||
public ModbusDbContext(DbContextOptions<ModbusDbContext> options) :base(options) | |||||
{ | |||||
} | |||||
public DbSet<ModbusLog> ModbusLog { get; set; } | |||||
} | |||||
} |
@@ -0,0 +1,26 @@ | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.ComponentModel.DataAnnotations; | |||||
using System.Linq; | |||||
using System.Text; | |||||
using System.Threading.Tasks; | |||||
namespace ModbusDemo.Model | |||||
{ | |||||
public class ModbusLog | |||||
{ | |||||
//数据库主键 | |||||
[Key] | |||||
public int Id { get; set; } | |||||
//操作的类型是读还是写 | |||||
public string OperationType { get; set; } | |||||
//请求的字节信息 | |||||
public string RequestData { get; set; } | |||||
//返回的字节信息 | |||||
public string ResponseData { get; set; } | |||||
//操作的时间 | |||||
public DateTime Time { get; set; } = DateTime.Now; | |||||
} | |||||
} |
@@ -149,7 +149,9 @@ | |||||
</Grid> | </Grid> | ||||
<Grid Grid.Row="3"> | <Grid Grid.Row="3"> | ||||
<DataGrid ItemsSource="{Binding ModbusLogList}"> | |||||
</DataGrid> | |||||
</Grid> | </Grid> | ||||
</Grid> | </Grid> | ||||
</UserControl> | </UserControl> |
@@ -31,6 +31,7 @@ | |||||
<RowDefinition> | <RowDefinition> | ||||
</RowDefinition> | </RowDefinition> | ||||
</Grid.RowDefinitions> | </Grid.RowDefinitions> | ||||
<Button Height="30" Width="80" Click="Button_Click">连接</Button> | |||||
<Grid Grid.Row="1"> | <Grid Grid.Row="1"> | ||||
<Grid.ColumnDefinitions> | <Grid.ColumnDefinitions> | ||||
<ColumnDefinition> | <ColumnDefinition> | ||||
@@ -1,4 +1,5 @@ | |||||
using System; | |||||
using ModbusDemo.Model; | |||||
using System; | |||||
using System.Collections.Generic; | using System.Collections.Generic; | ||||
using System.Linq; | using System.Linq; | ||||
using System.Text; | using System.Text; | ||||
@@ -20,9 +21,27 @@ namespace ModbusDemo.VIew | |||||
/// </summary> | /// </summary> | ||||
public partial class SettingsUC : UserControl | public partial class SettingsUC : UserControl | ||||
{ | { | ||||
public SettingsUC() | |||||
private ModbusDbContext _ModbusDbContext; | |||||
public SettingsUC(ModbusDbContext ModbusDbContext) | |||||
{ | { | ||||
InitializeComponent(); | InitializeComponent(); | ||||
_ModbusDbContext = ModbusDbContext; | |||||
} | |||||
private void Button_Click(object sender, RoutedEventArgs e) | |||||
{ | |||||
ModbusLog m = new ModbusLog(); | |||||
m.OperationType = "read"; | |||||
m.ResponseData = "0120"; | |||||
m.RequestData = "8888"; | |||||
_ModbusDbContext.Add(m); | |||||
_ModbusDbContext.SaveChanges(); | |||||
} | } | ||||
} | } | ||||
} | } |
@@ -1,4 +1,5 @@ | |||||
using ModbusDemo.Device; | using ModbusDemo.Device; | ||||
using ModbusDemo.Model; | |||||
using Prism.Commands; | using Prism.Commands; | ||||
using Prism.Mvvm; | using Prism.Mvvm; | ||||
using System; | using System; | ||||
@@ -132,6 +133,20 @@ namespace ModbusDemo.VIewModel | |||||
#endregion | #endregion | ||||
private List<ModbusLog> _modbusLogList; | |||||
public List<ModbusLog> ModbusLogList | |||||
{ | |||||
get { return _modbusLogList; } | |||||
set | |||||
{ | |||||
_modbusLogList = value; | |||||
RaisePropertyChanged(); | |||||
} | |||||
} | |||||
public CoilUCViewModel() | public CoilUCViewModel() | ||||
{ | { | ||||
@@ -143,9 +158,65 @@ namespace ModbusDemo.VIewModel | |||||
ReadCoilCmm = new DelegateCommand(ReadCoil); | ReadCoilCmm = new DelegateCommand(ReadCoil); | ||||
_modbusRTU = modbusRTU; | _modbusRTU = modbusRTU; | ||||
WriteCoilCmm = new DelegateCommand(WriteCoil); | WriteCoilCmm = new DelegateCommand(WriteCoil); | ||||
} | |||||
ModbusLogList = new List<ModbusLog>(); | |||||
ModbusLogList.Add(new ModbusLog | |||||
{ | |||||
OperationType = "读取", | |||||
RequestData = "01 03 00 00 00 02", | |||||
ResponseData = "01 03 04 00 0A 00 0B", | |||||
Time = DateTime.Now.AddMinutes(-5) | |||||
}); | |||||
ModbusLogList.Add(new ModbusLog | |||||
{ | |||||
OperationType = "写入", | |||||
RequestData = "01 06 00 01 00 03", | |||||
ResponseData = "01 06 00 01 00 03", | |||||
Time = DateTime.Now.AddMinutes(-2) | |||||
}); | |||||
ModbusLogList.Add(new ModbusLog | |||||
{ | |||||
OperationType = "读取", | |||||
RequestData = "01 03 00 00 00 02", | |||||
ResponseData = "01 03 04 00 0A 00 0B", | |||||
Time = DateTime.Now.AddMinutes(-5) | |||||
}); | |||||
ModbusLogList.Add(new ModbusLog | |||||
{ | |||||
OperationType = "写入", | |||||
RequestData = "01 06 00 01 00 03", | |||||
ResponseData = "01 06 00 01 00 03", | |||||
Time = DateTime.Now.AddMinutes(-2) | |||||
}); | |||||
ModbusLogList.Add(new ModbusLog | |||||
{ | |||||
OperationType = "读取", | |||||
RequestData = "01 03 00 00 00 02", | |||||
ResponseData = "01 03 04 00 0A 00 0B", | |||||
Time = DateTime.Now.AddMinutes(-5) | |||||
}); | |||||
ModbusLogList.Add(new ModbusLog | |||||
{ | |||||
OperationType = "写入", | |||||
RequestData = "01 06 00 01 00 03", | |||||
ResponseData = "01 06 00 01 00 03", | |||||
Time = DateTime.Now.AddMinutes(-2) | |||||
}); | |||||
} | |||||
/// <summary> | /// <summary> | ||||
/// 线圈读取操作 | /// 线圈读取操作 | ||||
@@ -178,9 +249,9 @@ namespace ModbusDemo.VIewModel | |||||
private void WriteCoil() | private void WriteCoil() | ||||
{ | { | ||||
//去除字符串中的空格 | //去除字符串中的空格 | ||||
WriteData = WriteData.Replace(" ", ""); | |||||
WriteData = WriteData.Replace(" ", ""); | |||||
//转换为布尔数组 | //转换为布尔数组 | ||||
bool[] data = WriteData.Select(m => m == '1').ToArray(); | |||||
bool[] data = WriteData.Select(m => m == '1').ToArray(); | |||||
try | try | ||||
{ | { | ||||
@@ -193,7 +264,7 @@ namespace ModbusDemo.VIewModel | |||||
MessageBox.Show("信息配置错误"); | MessageBox.Show("信息配置错误"); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
} | } |
@@ -0,0 +1,14 @@ | |||||
{ | |||||
"compilerOptions": { | |||||
"noImplicitAny": false, | |||||
"noEmitOnError": true, | |||||
"removeComments": false, | |||||
"sourceMap": true, | |||||
"target": "es5" | |||||
}, | |||||
"exclude": [ | |||||
"node_modules", | |||||
"wwwroot" | |||||
], | |||||
"ConnectionStrings": { "ConnStr": "server=.;uid=sa;pwd=123456;database=Modbus;TrustServerCertificate=true" } | |||||
} |