@@ -328,6 +328,7 @@ namespace ModbusDemo.Device | |||
m.RequestData = responseStr; | |||
_modbusDbContext.Add(m); | |||
_modbusDbContext.SaveChanges(); | |||
//判断发送回来的数据是否正确 | |||
CheckData.CheckResponse(response); | |||
@@ -149,8 +149,20 @@ | |||
</Grid> | |||
<Grid Grid.Row="3"> | |||
<DataGrid ItemsSource="{Binding ModbusLogList}"> | |||
<DataGrid ItemsSource="{Binding ModbusLogList}" AutoGenerateColumns="False" IsReadOnly="True" ColumnWidth="*"> | |||
<DataGrid.Columns> | |||
<!-- 操作类型列 --> | |||
<DataGridTextColumn Header="操作类型" Binding="{Binding OperationType}" Width="100" /> | |||
<!-- 请求数据列 --> | |||
<DataGridTextColumn Header="请求数据" Binding="{Binding RequestData}" /> | |||
<!-- 响应数据列 --> | |||
<DataGridTextColumn Header="响应数据" Binding="{Binding ResponseData}" /> | |||
<!-- 操作时间列 --> | |||
<DataGridTextColumn Header="操作时间" Binding="{Binding Time, StringFormat='yyyy-MM-dd HH:mm:ss'}" Width="200"/> | |||
</DataGrid.Columns> | |||
</DataGrid> | |||
</Grid> | |||
</Grid> | |||
@@ -147,5 +147,22 @@ | |||
Style="{StaticResource MaterialDesignRaisedDarkButton}"> | |||
</Button> | |||
</Grid> | |||
<Grid Grid.Row="3"> | |||
<DataGrid ItemsSource="{Binding ModbusLogList}" AutoGenerateColumns="False" IsReadOnly="True" ColumnWidth="*"> | |||
<DataGrid.Columns> | |||
<!-- 操作类型列 --> | |||
<DataGridTextColumn Header="操作类型" Binding="{Binding OperationType}" Width="100" /> | |||
<!-- 请求数据列 --> | |||
<DataGridTextColumn Header="请求数据" Binding="{Binding RequestData}" /> | |||
<!-- 响应数据列 --> | |||
<DataGridTextColumn Header="响应数据" Binding="{Binding ResponseData}" /> | |||
<!-- 操作时间列 --> | |||
<DataGridTextColumn Header="操作时间" Binding="{Binding Time, StringFormat='yyyy-MM-dd HH:mm:ss'}" Width="200"/> | |||
</DataGrid.Columns> | |||
</DataGrid> | |||
</Grid> | |||
</Grid> | |||
</UserControl> |
@@ -31,7 +31,7 @@ | |||
<RowDefinition> | |||
</RowDefinition> | |||
</Grid.RowDefinitions> | |||
<Button Height="30" Width="80" Click="Button_Click">连接</Button> | |||
<Button Width="80" Height="30" Click="Button_Click">连接</Button> | |||
<Grid Grid.Row="1"> | |||
<Grid.ColumnDefinitions> | |||
<ColumnDefinition> | |||
@@ -17,6 +17,8 @@ namespace ModbusDemo.VIewModel | |||
/// </summary> | |||
public class CoilUCViewModel : BindableBase | |||
{ | |||
//定义数据库操作 | |||
private ModbusDbContext _modbusDbContext; | |||
//获取读线圈的类 | |||
private IModbusRTU _modbusRTU; | |||
//定义读线圈的命令 | |||
@@ -127,7 +129,7 @@ namespace ModbusDemo.VIewModel | |||
set | |||
{ | |||
_writeData = value; | |||
RaisePropertyChanged(); | |||
} | |||
} | |||
@@ -152,70 +154,15 @@ namespace ModbusDemo.VIewModel | |||
} | |||
public CoilUCViewModel(SerialPort serialPort, ModbusRTU modbusRTU) | |||
public CoilUCViewModel(SerialPort serialPort, ModbusRTU modbusRTU, ModbusDbContext modbusDbContext) | |||
{ | |||
_serialPort = serialPort; | |||
ReadCoilCmm = new DelegateCommand(ReadCoil); | |||
_modbusRTU = modbusRTU; | |||
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) | |||
}); | |||
_modbusDbContext = modbusDbContext; | |||
//初始化查询操作 | |||
ModbusLogList = GetOperateCoil(); | |||
} | |||
/// <summary> | |||
@@ -266,5 +213,18 @@ namespace ModbusDemo.VIewModel | |||
} | |||
} | |||
/// <summary> | |||
/// 获取展示数据 | |||
/// </summary> | |||
/// <returns></returns> | |||
private List<ModbusLog> GetOperateCoil() | |||
{ | |||
return _modbusDbContext.ModbusLog | |||
.Where(log => log.OperationType == "读线圈" || log.OperationType == "写线圈") | |||
.OrderByDescending(log => log.Time) // 按时间倒序,最新的在前 | |||
.Take(20) // 只取前20条记录 | |||
.ToList(); // 执行查询 | |||
} | |||
} | |||
} |
@@ -1,4 +1,5 @@ | |||
using ModbusDemo.Device; | |||
using ModbusDemo.Model; | |||
using Prism.Commands; | |||
using Prism.Mvvm; | |||
using System; | |||
@@ -13,7 +14,8 @@ namespace ModbusDemo.VIewModel | |||
{ | |||
class RegisterUCViewModel:BindableBase | |||
{ | |||
//定义数据库操作类 | |||
private ModbusDbContext _modbusDbContext; | |||
//获取读线圈的类 | |||
private IModbusRTU _modbusRTU; | |||
//定义读线圈的命令 | |||
@@ -131,16 +133,32 @@ namespace ModbusDemo.VIewModel | |||
#endregion | |||
private List<ModbusLog> _modbusLogList; | |||
public List<ModbusLog> ModbusLogList | |||
{ | |||
get { return _modbusLogList; } | |||
set | |||
{ | |||
_modbusLogList = value; | |||
RaisePropertyChanged(); | |||
} | |||
} | |||
public RegisterUCViewModel() | |||
{ | |||
} | |||
public RegisterUCViewModel(SerialPort serialPort, ModbusRTU modbusRTU) | |||
public RegisterUCViewModel(SerialPort serialPort, ModbusRTU modbusRTU,ModbusDbContext modbusDbContext) | |||
{ | |||
_serialPort = serialPort; | |||
ReadRegisterCmm = new DelegateCommand(ReadRegister); | |||
_modbusRTU = modbusRTU; | |||
WriteRegisterCmm = new DelegateCommand(WriteRegister); | |||
_modbusDbContext = modbusDbContext; | |||
ModbusLogList = GetOperateRegister(); | |||
} | |||
@@ -208,5 +226,17 @@ namespace ModbusDemo.VIewModel | |||
} | |||
} | |||
/// <summary> | |||
/// 获取展示数据 | |||
/// </summary> | |||
/// <returns></returns> | |||
private List<ModbusLog> GetOperateRegister() | |||
{ | |||
return _modbusDbContext.ModbusLog | |||
.Where(log => log.OperationType == "读寄存器" || log.OperationType == "写寄存器") | |||
.OrderByDescending(log => log.Time) // 按时间倒序,最新的在前 | |||
.Take(20) // 只取前20条记录 | |||
.ToList(); // 执行查询 | |||
} | |||
} | |||
} |