This commit is contained in:
@@ -52,21 +52,29 @@
|
||||
BorderThickness="0" Margin="0,0,10,0" Click="BtnCurve_Click"/>
|
||||
</StackPanel>
|
||||
|
||||
<!-- 右侧:PLC连接区域 + 连接状态(靠右显示) -->
|
||||
<!-- 右侧:PLC连接区域 + 连接状态 + IP/端口 + 连接按钮 -->
|
||||
<StackPanel Grid.Column="1" Orientation="Horizontal"
|
||||
HorizontalAlignment="Right"
|
||||
VerticalAlignment="Center"
|
||||
Margin="0,0,20,0" Visibility="Hidden">
|
||||
|
||||
|
||||
HorizontalAlignment="Right"
|
||||
VerticalAlignment="Center"
|
||||
Margin="0,0,20,0">
|
||||
<!-- IP和端口显示 -->
|
||||
<TextBlock x:Name="TxtPLCIP" Text="IP: " FontSize="12" Foreground="White" Margin="0,0,5,0" VerticalAlignment="Center"/>
|
||||
<TextBlock x:Name="TxtPLCIPValue" Text="192.168.1.170" FontSize="12" Foreground="White" Margin="0,0,15,0" VerticalAlignment="Center"/>
|
||||
<TextBlock x:Name="TxtPLCPort" Text="Port: " FontSize="12" Foreground="White" Margin="0,0,5,0" VerticalAlignment="Center"/>
|
||||
<TextBlock x:Name="TxtPLCPortValue" Text="502" FontSize="12" Foreground="White" Margin="0,0,15,0" VerticalAlignment="Center"/>
|
||||
|
||||
<!-- 连接状态 -->
|
||||
<!-- 连接按钮 -->
|
||||
<Button x:Name="BtnConnectPLC" Content="连接PLC" Width="80" Height="30"
|
||||
FontSize="12" Background="#27AE60" Foreground="White"
|
||||
BorderThickness="0" Margin="10,0,0,0" Click="ConnectButton_Click"/>
|
||||
|
||||
<!-- 连接状态指示 -->
|
||||
<StackPanel Orientation="Horizontal" Margin="15,0,0,0"
|
||||
VerticalAlignment="Center">
|
||||
VerticalAlignment="Center">
|
||||
<Ellipse Width="18" Height="18" Fill="{Binding StatusColor}"
|
||||
Margin="0,0,8,0"/>
|
||||
Margin="0,0,8,0"/>
|
||||
<TextBlock Text="{Binding ConnectionStatus}"
|
||||
FontSize="14" Foreground="White"/>
|
||||
FontSize="12" Foreground="White"/>
|
||||
</StackPanel>
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
|
||||
@@ -113,6 +113,20 @@ namespace PLCDataMonitor
|
||||
{
|
||||
_hasInsertedReport = false; // 重置插入标记
|
||||
};
|
||||
|
||||
|
||||
|
||||
// 设置IP和端口显示
|
||||
string ip = ConfigurationManager.AppSettings["PLC_IP"];
|
||||
string port = ConfigurationManager.AppSettings["PLC_PORT"];
|
||||
TxtPLCIPValue.Text = string.IsNullOrEmpty(ip) ? "192.168.1.170" : ip;
|
||||
TxtPLCPortValue.Text = string.IsNullOrEmpty(port) ? "502" : port;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void SetupLogging()
|
||||
@@ -220,12 +234,13 @@ namespace PLCDataMonitor
|
||||
{
|
||||
_keepReading = false;
|
||||
|
||||
// 使用超时等待线程结束
|
||||
// 等待线程结束,最多 2 秒
|
||||
if (_readThread != null && _readThread.IsAlive)
|
||||
{
|
||||
if (!_readThread.Join(500))
|
||||
if (!_readThread.Join(2000))
|
||||
{
|
||||
|
||||
// 如果线程未结束,强制终止(慎用)
|
||||
// _readThread.Abort(); // 不推荐,此处仅为保底
|
||||
}
|
||||
}
|
||||
|
||||
@@ -234,7 +249,8 @@ namespace PLCDataMonitor
|
||||
|
||||
if (_tcpClient != null)
|
||||
{
|
||||
_tcpClient.Close();
|
||||
if (_tcpClient.Connected)
|
||||
_tcpClient.Close();
|
||||
_tcpClient.Dispose();
|
||||
_tcpClient = null;
|
||||
}
|
||||
@@ -373,9 +389,12 @@ namespace PLCDataMonitor
|
||||
// 连续读取 HR504.1 ~ HR504.5(5个线圈,起始 41265)
|
||||
alarmsHR504 = _modbusMaster?.ReadCoils(0x01, 41265, 5);
|
||||
// 连续读取 HR509.0 ~ HR509.3(4个线圈,起始 41345)
|
||||
alarmsHR509 = _modbusMaster?.ReadCoils(0x01, 41344, 4);
|
||||
alarmsHR509 = _modbusMaster?.ReadCoils(0x01, 41344, 6);
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
}
|
||||
catch { }
|
||||
|
||||
string alarmMessage = "";
|
||||
if (alarmsHR504 != null && alarmsHR504.Length >= 5)
|
||||
@@ -387,19 +406,22 @@ namespace PLCDataMonitor
|
||||
// 索引2=41267=测试完成(待确认)
|
||||
if (alarmsHR504[2]) alarmMessage += "测试完成 ";
|
||||
// 索引3=41268=摩擦力过大(待确认)
|
||||
if (alarmsHR504[3]) alarmMessage += "摩擦力过大 ";
|
||||
//if (alarmsHR504[3]) alarmMessage += "摩擦力过大 ";
|
||||
// 索引4=41269=超温95℃(待确认)
|
||||
if (alarmsHR504[4]) alarmMessage += "超温95℃ ";
|
||||
}
|
||||
if (alarmsHR509 != null && alarmsHR509.Length >= 4)
|
||||
if (alarmsHR509 != null && alarmsHR509.Length >= 6)
|
||||
{
|
||||
if (alarmsHR509[0]) alarmMessage += "1工位低水位 ";
|
||||
if (alarmsHR509[2]) alarmMessage += "水泵未运行 ";
|
||||
// 索引3=41348=2工位低水位
|
||||
if (alarmsHR509[3]) alarmMessage += "2工位低水位 ";
|
||||
if (alarmsHR509[4]) alarmMessage += "摩擦力过大 ";
|
||||
if (alarmsHR509[5]) alarmMessage += "伺服驱动器报警,请清除 ";
|
||||
}
|
||||
if (string.IsNullOrEmpty(alarmMessage))
|
||||
alarmMessage = "无报警";
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -476,15 +498,34 @@ namespace PLCDataMonitor
|
||||
|
||||
}), System.Windows.Threading.DispatcherPriority.Background);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
// 如果连接标志为 true 但实际未连接,主动断开
|
||||
if (_isConnected)
|
||||
{
|
||||
Dispatcher.BeginInvoke(new Action(() => DisconnectFromPLC()));
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// 非阻塞地报告错误到 UI
|
||||
Dispatcher.BeginInvoke(new Action(() =>
|
||||
// 捕获 Socket 异常
|
||||
if (ex is SocketException || (ex.InnerException is SocketException) ||
|
||||
ex.Message.Contains("non-connected") || ex.Message.Contains("socket"))
|
||||
{
|
||||
StatusColor = Brushes.Orange;
|
||||
ConnectionStatus = $"读取错误:{ex.Message}";
|
||||
}), System.Windows.Threading.DispatcherPriority.Background);
|
||||
// 连接已丢失,触发断开
|
||||
Dispatcher.BeginInvoke(new Action(() => DisconnectFromPLC()));
|
||||
}
|
||||
else
|
||||
{
|
||||
// 其他异常正常显示
|
||||
Dispatcher.BeginInvoke(new Action(() =>
|
||||
{
|
||||
StatusColor = Brushes.Orange;
|
||||
ConnectionStatus = $"读取错误:{ex.Message}";
|
||||
}), System.Windows.Threading.DispatcherPriority.Background);
|
||||
}
|
||||
}
|
||||
Thread.Sleep(200); // 100ms刷新一次
|
||||
}
|
||||
@@ -556,7 +597,21 @@ namespace PLCDataMonitor
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception($"Modbus读取错误:{ex.Message}");
|
||||
//throw new Exception($"Modbus读取错误:{ex.Message}");
|
||||
return (
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
true
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -567,14 +622,27 @@ namespace PLCDataMonitor
|
||||
|
||||
private void Window_Loaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (!_isConnected)
|
||||
{
|
||||
ConnectToPLC();
|
||||
}
|
||||
else
|
||||
{
|
||||
DisconnectFromPLC();
|
||||
}
|
||||
//if (!_isConnected)
|
||||
//{
|
||||
// ConnectToPLC();
|
||||
// if (_isConnected)
|
||||
// {
|
||||
// BtnConnectPLC.Content = "断开PLC";
|
||||
// BtnConnectPLC.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#E67E22"));
|
||||
// }
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// DisconnectFromPLC();
|
||||
// BtnConnectPLC.Content = "连接PLC";
|
||||
// BtnConnectPLC.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#27AE60"));
|
||||
//}
|
||||
// 只做界面初始化,不自动连接
|
||||
// 确保连接按钮状态正确
|
||||
BtnConnectPLC.Content = "连接PLC";
|
||||
BtnConnectPLC.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#27AE60"));
|
||||
StatusColor = Brushes.Red;
|
||||
ConnectionStatus = "未连接";
|
||||
}
|
||||
|
||||
private void ConnectButton_Click(object sender, RoutedEventArgs e)
|
||||
@@ -582,10 +650,15 @@ namespace PLCDataMonitor
|
||||
if (!_isConnected)
|
||||
{
|
||||
ConnectToPLC();
|
||||
// 更新按钮文本
|
||||
BtnConnectPLC.Content = "断开PLC";
|
||||
BtnConnectPLC.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#E67E22"));
|
||||
}
|
||||
else
|
||||
{
|
||||
DisconnectFromPLC();
|
||||
BtnConnectPLC.Content = "连接PLC";
|
||||
BtnConnectPLC.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#27AE60"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user