RTU添加
This commit is contained in:
@@ -13,7 +13,7 @@ namespace MainModule.ViewModels
|
||||
#region 私有字段
|
||||
private Events.ProtocolType _selectedProtocol = Events.ProtocolType.S7;
|
||||
private string _ipAddress = "127.0.0.1";
|
||||
private int _port = 102;
|
||||
private int _port = 502;
|
||||
private string _serialPort = "COM1";
|
||||
private int _baudRate = 9600;
|
||||
private Parity _parity = Parity.None;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using DeviceCommand.Base;
|
||||
using DeviceCommand.Base;
|
||||
using DeviceCommand.Devices;
|
||||
using MainModule.Events;
|
||||
using MainModule.Views;
|
||||
@@ -21,6 +21,7 @@ namespace MainModule.ViewModels
|
||||
{
|
||||
private readonly THC1100 _thc1100 = new();
|
||||
private readonly UMC1300 _umc1300;
|
||||
private UMC1000Rtu? _umc1000Rtu;
|
||||
public ObservableCollection<ChamberMonitorItem> Chambers { get; } = new();
|
||||
private IContainerProvider _containerProvider;
|
||||
private CancellationTokenSource _refreshCts;
|
||||
@@ -114,6 +115,20 @@ namespace MainModule.ViewModels
|
||||
set => SetProperty(ref _configBaudRate, value);
|
||||
}
|
||||
|
||||
private int _configDataBits = 8;
|
||||
public int ConfigDataBits
|
||||
{
|
||||
get => _configDataBits;
|
||||
set => SetProperty(ref _configDataBits, value);
|
||||
}
|
||||
|
||||
private byte _configSlaveId = 1;
|
||||
public byte ConfigSlaveId
|
||||
{
|
||||
get => _configSlaveId;
|
||||
set => SetProperty(ref _configSlaveId, value);
|
||||
}
|
||||
|
||||
private int _configSendTimeout = 3000;
|
||||
public int ConfigSendTimeout
|
||||
{
|
||||
@@ -184,7 +199,7 @@ namespace MainModule.ViewModels
|
||||
{
|
||||
Id = 2,
|
||||
Name = "环境箱 02",
|
||||
ProtocolType = "Modbus TCP B+",
|
||||
ProtocolType = "Modbus RTU",
|
||||
IsConnected = false,
|
||||
Temperature = 0.0,
|
||||
Humidity = 0.0
|
||||
@@ -456,7 +471,7 @@ namespace MainModule.ViewModels
|
||||
System.Diagnostics.Debug.WriteLine("[定时刷新] THC1100未连接,跳过数据读取");
|
||||
}
|
||||
|
||||
// 检查UMC1300连接状态
|
||||
// 检查UMC1300连接状态 → 环境箱01 (Modbus TCP)
|
||||
if (_umc1300.IsConnected)
|
||||
{
|
||||
var chamber1 = GetChamberById(1);
|
||||
@@ -479,6 +494,30 @@ namespace MainModule.ViewModels
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 检查UMC1000Rtu连接状态 → 环境箱02 (Modbus RTU)
|
||||
if (_umc1000Rtu != null && _umc1000Rtu.IsConnected)
|
||||
{
|
||||
var chamber2 = GetChamberById(2);
|
||||
if (chamber2 != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var temp = await _umc1000Rtu.ReadTemperatureAsync();
|
||||
var humidity = await _umc1000Rtu.ReadHumidityAsync();
|
||||
|
||||
Application.Current.Dispatcher.Invoke(() =>
|
||||
{
|
||||
chamber2.Temperature = temp;
|
||||
chamber2.Humidity = humidity;
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"[定时刷新] 读取UMC1000Rtu数据异常: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -572,6 +611,9 @@ namespace MainModule.ViewModels
|
||||
case "Modbus TCP":
|
||||
_umc1300.Close();
|
||||
break;
|
||||
case "Modbus RTU":
|
||||
_umc1000Rtu?.Close();
|
||||
break;
|
||||
}
|
||||
|
||||
// 停止数据刷新
|
||||
@@ -608,10 +650,10 @@ namespace MainModule.ViewModels
|
||||
System.Diagnostics.Debug.WriteLine("[连接] 使用设备配置的Modbus TCP协议");
|
||||
connected = await ConnectModbusTcpDeviceAsync(item);
|
||||
break;
|
||||
case "Modbus TCP B+":
|
||||
// Modbus TCP B+连接
|
||||
System.Diagnostics.Debug.WriteLine("[连接] 使用设备配置的Modbus TCP B+协议");
|
||||
connected = await ConnectModbusTcpDeviceAsync(item);
|
||||
case "Modbus RTU":
|
||||
// Modbus RTU连接 → UMC1000Rtu 温湿度
|
||||
System.Diagnostics.Debug.WriteLine("[连接] 使用设备配置的Modbus RTU协议");
|
||||
connected = await ConnectUMC1000RtuDeviceAsync(item);
|
||||
break;
|
||||
case "HTTP":
|
||||
// HTTP连接
|
||||
@@ -666,19 +708,51 @@ namespace MainModule.ViewModels
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Modbus TCP设备连接
|
||||
/// UMC1000Rtu 温湿度设备连接(Modbus RTU)
|
||||
/// </summary>
|
||||
private async Task<bool> ConnectUMC1000RtuDeviceAsync(ChamberMonitorItem item)
|
||||
{
|
||||
try
|
||||
{
|
||||
_umc1000Rtu = new UMC1000Rtu(
|
||||
slaveId: ConfigSlaveId,
|
||||
portName: ConfigSerialPort,
|
||||
baudRate: ConfigBaudRate,
|
||||
dataBits: ConfigDataBits,
|
||||
stopBits: System.IO.Ports.StopBits.One,
|
||||
parity: System.IO.Ports.Parity.None,
|
||||
readTimeout: ConfigReceiveTimeout,
|
||||
writeTimeout: ConfigSendTimeout);
|
||||
|
||||
bool result = await _umc1000Rtu.ConnectAsync();
|
||||
|
||||
if (result)
|
||||
{
|
||||
StartPeriodicRefresh();
|
||||
}
|
||||
|
||||
System.Diagnostics.Debug.WriteLine($"UMC1000Rtu Modbus RTU连接: {ConfigSerialPort}, 波特率:{ConfigBaudRate}, 从站:{ConfigSlaveId}, 结果: {result}");
|
||||
return result;
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"UMC1000Rtu连接异常: {ex.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Modbus TCP设备连接(UMC1300)
|
||||
/// </summary>
|
||||
private async Task<bool> ConnectModbusTcpDeviceAsync(ChamberMonitorItem item)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 配置UMC1300设备
|
||||
_umc1300.ConfigureDevice(ConfigIpAddress, ConfigPort, ConfigSendTimeout, ConfigReceiveTimeout);
|
||||
bool result = await _umc1300.ConnectAsync();
|
||||
|
||||
if (result)
|
||||
{
|
||||
// 启动数据刷新
|
||||
StartPeriodicRefresh();
|
||||
}
|
||||
|
||||
@@ -692,28 +766,6 @@ namespace MainModule.ViewModels
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Modbus RTU设备连接
|
||||
/// </summary>
|
||||
private async Task<bool> ConnectModbusRtuDeviceAsync(ChamberMonitorItem item)
|
||||
{
|
||||
// 实现Modbus RTU连接逻辑
|
||||
System.Diagnostics.Debug.WriteLine($"Modbus RTU连接: {ConfigSerialPort}, 波特率:{ConfigBaudRate}");
|
||||
await Task.Delay(500);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// TCP设备连接
|
||||
/// </summary>
|
||||
private async Task<bool> ConnectTcpDeviceAsync(ChamberMonitorItem item)
|
||||
{
|
||||
// 实现TCP连接逻辑
|
||||
System.Diagnostics.Debug.WriteLine($"TCP连接: {ConfigIpAddress}:{ConfigPort}");
|
||||
await Task.Delay(500);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// HTTP设备连接
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user