using CommunityToolkit.Mvvm.ComponentModel;
|
using HslCommunication;
|
using HslCommunication.ModBus;
|
using System.Windows;
|
using System.Windows.Controls;
|
|
namespace Plugin.CurrentSensorControl
|
{
|
/// <summary>
|
/// SingleSensor.xaml 的交互逻辑
|
/// </summary>
|
[ObservableObject]
|
public partial class SingleSensor : UserControl
|
{
|
private static Dictionary<string, ModbusTcpNet> clients = new Dictionary<string, ModbusTcpNet>();
|
private static Dictionary<ModbusTcpNet, bool> status = new Dictionary<ModbusTcpNet, bool>();
|
|
[ObservableProperty]
|
private string sensorName;
|
|
[ObservableProperty]
|
private bool isConnected;
|
|
[ObservableProperty]
|
private string current;
|
|
[ObservableProperty]
|
private string ip;
|
|
[ObservableProperty]
|
private int port;
|
|
|
|
/// <summary>
|
/// 地址(站号)
|
/// </summary>
|
[ObservableProperty]
|
private int address;
|
|
[ObservableProperty]
|
private string errMsg;
|
|
/// <summary>
|
/// 通道
|
/// </summary>
|
[ObservableProperty]
|
private int channel;
|
|
private ModbusTcpNet client;
|
|
|
public SingleSensor()
|
{
|
DataContext = this;
|
InitializeComponent();
|
}
|
|
public SingleSensor(string name, string ip, int port, int address, int channel) : this()
|
{
|
SensorName = name;
|
Ip = ip;
|
Port = port;
|
Address = address;
|
Channel = channel;
|
string key = $"{Ip}:{Port}";
|
if (!clients.ContainsKey(key))
|
{
|
client = new ModbusTcpNet(Ip, Port);
|
clients[key] = client;
|
}
|
else
|
{
|
client = clients[key];
|
}
|
}
|
|
private void controlBtn_Click(object sender, RoutedEventArgs e)
|
{
|
if (IsConnected)
|
{
|
this.Close();
|
}
|
else
|
{
|
Task.Factory.StartNew(() => { this.Connect(); });
|
}
|
}
|
|
public void Connect()
|
{
|
lock (client)
|
{
|
if (!status.ContainsKey(client) || !status[client])
|
{
|
var res = client.ConnectServer();
|
IsConnected = res.IsSuccess;
|
status[client] = IsConnected;
|
}
|
else
|
{
|
IsConnected = status[client];
|
}
|
}
|
}
|
|
public void ReadCurrent()
|
{
|
//if (status.ContainsKey(client) && status[client])
|
//{
|
// OperateResult<double[]> res;
|
// lock (client)
|
// {
|
// res = client.ReadDouble(channel.ToString(), 1);
|
// }
|
// Dispatcher.Invoke(() =>
|
// {
|
// if (res.IsSuccess)
|
// {
|
// Current = res.Content[0].ToString();
|
// ErrMsg = string.Empty;
|
// }
|
// else
|
// {
|
// ErrMsg = res.Message;
|
// //Close();
|
// }
|
// });
|
//}
|
//else
|
//{
|
// IsConnected = false;
|
//}
|
var sb = status[client];
|
if (IsConnected)
|
{
|
OperateResult<ushort> res;
|
lock (client)
|
{
|
res = client.ReadUInt16(channel.ToString());
|
}
|
Dispatcher.Invoke(() =>
|
{
|
if (res.IsSuccess)
|
{
|
Current = (res.Content / 1000d).ToString();
|
ErrMsg = string.Empty;
|
}
|
else
|
{
|
ErrMsg = res.Message;
|
//Close();
|
}
|
});
|
}
|
}
|
|
public void Close()
|
{
|
lock (client)
|
{
|
var connected = status[client];
|
if (connected)
|
{
|
var res = client.ConnectClose();
|
if (res.IsSuccess)
|
{
|
client.Dispose();
|
}
|
IsConnected = !res.IsSuccess;
|
status[client] = IsConnected;
|
}
|
else
|
{
|
IsConnected = connected;
|
}
|
}
|
}
|
}
|
}
|