染色机计算机
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

142 lines
5.5 KiB

using DyeingComputer.ViewModel;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace DyeingComputer.View
{
/// <summary>
/// EngineerSetView.xaml 的交互逻辑
/// </summary>
public partial class EngineerSetView : UserControl
{
int _S=0;
public EngineerSetView()
{
InitializeComponent();
CountDown();
// 获取所有可用串口端口,并添加到comboBoxCOM
string[] ports = System.IO.Ports.SerialPort.GetPortNames();
comboBoxCOM.ItemsSource = ports;
comboBoxCOM.Text = Configini.IniReadvalue("SYS", "COM");
string[] protocols = { "SUNLIGHT_R", "MODBUS_RTU" };
comboBoxCOMmod.ItemsSource = protocols;
comboBoxCOMmod.Text = Configini.IniReadvalue("SYS", "Protocols");
string[] Language = { "en-US", "zh-CN", "zh -TW" };
comboBoxLanguage.ItemsSource = Language;
comboBoxLanguage.Text = Configini.IniReadvalue("SYS", "Language");
SocketIP.Text = "SocketIP:"+ GetLocalIPAddress()+"[7789][7799]";
}
//调用配置文件
private UserClass.IniFile.IniFiles Configini = new UserClass.IniFile.IniFiles(Convert.ToString(System.AppDomain.CurrentDomain.BaseDirectory) + "DyeingComputer.ini");
public static string GetLocalIPAddress()
{
// 获取主机名和所有 IP 地址
var hostEntry = Dns.GetHostEntry(Dns.GetHostName());
// 优先尝试获取非内部、非虚拟网卡的 IPv4 地址(如以太网、Wi-Fi)
var ipAddress = hostEntry.AddressList
.FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork
&& !IPAddress.IsLoopback(ip)
&& ip.ToString().StartsWith("192.168")); // 示例:优先 192.168 网段
// 如果没找到优先的,则选择第一个非环回的 IPv4 地址
if (ipAddress == null)
{
ipAddress = hostEntry.AddressList
.FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork
&& !IPAddress.IsLoopback(ip));
}
// 如果还是没找到,返回环回地址或抛出异常
return ipAddress?.ToString() ?? "127.0.0.1";
}
private void comboBoxCOM_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Configini.IniWritevalue("SYS", "COM", comboBoxCOM.SelectedValue.ToString());
}
private void comboBoxCOMmod_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Configini.IniWritevalue("SYS", "Protocols", comboBoxCOMmod.SelectedValue.ToString());
}
private void comboBoxLanguage_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Configini.IniWritevalue("SYS", "Language", comboBoxLanguage.SelectedValue.ToString());
}
public void CountDown()
{
DispatcherTimer timer1s = new DispatcherTimer//初始化循环,每1秒调用一次Tick
{
Interval = TimeSpan.FromSeconds(1)//秒
};
timer1s.Tick += Tick_Event_1S;
timer1s.Start();
}
void Tick_Event_1S(object sender, EventArgs e)//Tick_Event周期执行事件1S
{
RX.Text = MainWindowViewModel.RX.ToString();
TX.Text = MainWindowViewModel.TX.ToString();
_S++;
if (_S > 60)
{
_S = 0;
var displayData = MainWindowViewModel.Hardware.Select(kvp => new AdvancedDictionaryEntry
{
Key = kvp.Key,
Value = kvp.Value
}).ToList();
_set.ItemsSource = displayData;
}
}
private void Quit_Click(object sender, RoutedEventArgs e)
{
MessageBoxResult messageBoxResult = System.Windows.MessageBox.Show(Properties.Resources.Quit, "800", MessageBoxButton.YesNo);
if (messageBoxResult == MessageBoxResult.No) return;
if (messageBoxResult == MessageBoxResult.Yes)
{
// Application.Current.Shutdown();
System.Environment.Exit(0);//退出
}
}
private void Shutdown_Click(object sender, RoutedEventArgs e)
{
MessageBoxResult messageBoxResult = System.Windows.MessageBox.Show(Properties.Resources.Shutdown, "800", MessageBoxButton.YesNo);
if (messageBoxResult == MessageBoxResult.No) return;
if (messageBoxResult == MessageBoxResult.Yes)
{
// Application.Current.Shutdown();
//System.Environment.Exit(0);//退出
Process.Start("shutdown", "/s /t 10");
}
}
public class AdvancedDictionaryEntry
{
public string Key { get; set; }
public object Value { get; set; }
}
}
}