Files
2026-01-16 19:25:21 +08:00

238 lines
8.7 KiB
C#

using Modbus.Device;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using .PLC;
namespace
{
public partial class frm_Setting : UIForm
{
Function Fc;
ModbusMaster master;
public frm_Setting(ModbusMaster master_In)
{
InitializeComponent();
master = master_In;
Fc=new Function(master);
//switch (int.Parse(ConfigurationManager.AppSettings["SampleRate"]))
//{
// case 1000:
// uiComboBox1.SelectedIndex = 0;
// break;
// case 5000:
// uiComboBox1.SelectedIndex = 1;
// break;
// case 10000:
// uiComboBox1.SelectedIndex = 2;
// break;
// case 50000:
// uiComboBox1.SelectedIndex = 3;
// break;
// case 100000:
// uiComboBox1.SelectedIndex = 4;
// break;
// case 200000:
// uiComboBox1.SelectedIndex = 5;
// break;
//}
this.uiComboBox1.SelectedIndexChanged += new System.EventHandler(this.uiComboBox1_SelectedIndexChanged);
}
private void btn_上前_MouseDown(object sender, MouseEventArgs e)
{
Fc.BtnClickFunction(Function.ButtonType., 12);
}
private void btn_上前_MouseUp(object sender, MouseEventArgs e)
{
Fc.BtnClickFunction(Function.ButtonType., 12);
}
private void btn_上后_MouseDown(object sender, MouseEventArgs e)
{
Fc.BtnClickFunction(Function.ButtonType., 13);
}
private void btn_上后_MouseUp(object sender, MouseEventArgs e)
{
Fc.BtnClickFunction(Function.ButtonType., 13);
}
private void btn_下前_MouseDown(object sender, MouseEventArgs e)
{
Fc.BtnClickFunction(Function.ButtonType., 10);
}
private void btn_下前_MouseUp(object sender, MouseEventArgs e)
{
Fc.BtnClickFunction(Function.ButtonType., 10);
}
private void btn_下后_MouseDown(object sender, MouseEventArgs e)
{
Fc.BtnClickFunction(Function.ButtonType., 11);
}
private void btn_下后_MouseUp(object sender, MouseEventArgs e)
{
Fc.BtnClickFunction(Function.ButtonType., 11);
}
private void btn_发射激光_Click(object sender, EventArgs e)
{
Fc.BtnClickFunction(Function.ButtonType., 0);
}
DataChange dc = new DataChange();
private void uiTextBox1_ButtonClick(object sender, EventArgs e)
{
Fc.WriteToPLC(uiTextBox1.Text,200,Function.DataType.) ;
ushort[] ushorts = new ushort[2];
ushorts=master.ReadHoldingRegisters(1,200, 2);
uiTextBox1.Text = dc.UshortToFloat(ushorts[1], ushorts[0]).ToString();
}
private void uiTextBox2_ButtonClick(object sender, EventArgs e)
{
Fc.WriteToPLC(uiTextBox2.Text, 210, Function.DataType.);
ushort[] ushorts = new ushort[2];
ushorts = master.ReadHoldingRegisters(1, 210, 2);
uiTextBox2.Text = dc.UshortToFloat(ushorts[1], ushorts[0]).ToString();
}
private void uiTextBox4_ButtonClick(object sender, EventArgs e)
{
Fc.WriteToPLC(uiTextBox4.Text, 300, Function.DataType.);
ushort[] ushorts = new ushort[2];
ushorts = master.ReadHoldingRegisters(1, 300, 2);
uiTextBox4.Text = ushorts[0].ToString();
}
private void uiTextBox3_ButtonClick(object sender, EventArgs e)
{
int Value_In=uiTextBox3.Text.ToInt();
if (UIInputDialog.ShowInputIntegerDialog(ref Value_In, UIStyle.Inherited, desc: "请输入:"))
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// 更新appSettings中的值
//config.AppSettings.Settings["step"].Value = Value_In.ToString();
config.AppSettings.Settings["FFTSize"].Value = Value_In.ToString();
// 保存更改
config.Save(ConfigurationSaveMode.Modified);
uiTextBox3.Text = Value_In.ToString();
// 强制重新加载配置节,以确保更改立即生效(可选)
ConfigurationManager.RefreshSection("appSettings");
FromMain._step=Value_In;
}
}
private void frm_Setting_FormClosing(object sender, FormClosingEventArgs e)
{
this.Dispose();
}
private void frm_Setting_Load(object sender, EventArgs e)
{
ushort[] ushorts = new ushort[2];
// ushorts = master.ReadHoldingRegisters(1, 200, 2);
uiTextBox1.Text = dc.UshortToFloat(ushorts[1], ushorts[0]).ToString();
ushort[] ushorts2 = new ushort[2];
// ushorts2 = master.ReadHoldingRegisters(1, 210, 2);
uiTextBox2.Text = dc.UshortToFloat(ushorts2[1], ushorts2[0]).ToString();
ushort[] ushorts3 = new ushort[2];
//ushorts3 = master.ReadHoldingRegisters(1, 300, 2);
uiTextBox4.Text = ushorts3[0].ToString();
uiTextBox3.Text = ConfigurationManager.AppSettings["FFTSize"];
ushort[] ushort4=new ushort[2];
//ushort4=master.ReadHoldingRegisters(1, 450, 2);
tb_目标温度.Text = ushort4[0].ToString();
//bool[] bools1 = master.ReadCoils(1,62,1);
// if (bools1[0])
btn_开始加热.FillColor = Color.Red;
// else
btn_开始加热.FillColor = Color.FromArgb(80, 160, 255);
}
private void uiTextBox3_TextChanged(object sender, EventArgs e)
{
}
private void tb_目标温度_ButtonClick(object sender, EventArgs e)
{
int value_int = tb_目标温度.Text.ToInt();
if (UIInputDialog.ShowInputIntegerDialog(ref value_int, UIStyle.Inherited, desc: "请输入值:"))
{
master.WriteMultipleRegisters(1, 450, dc.intToushorts(value_int*10));
tb_目标温度.Text = value_int.ToString();
}
}
private void btn_开始加热_Click(object sender, EventArgs e)
{
Fc.BtnClickFunction(Function.ButtonType., 60);
btn_开始加热.FillColor = Color.Red;
}
private void btn_停止加热_Click(object sender, EventArgs e)
{
Fc.BtnClickFunction(Function.ButtonType., 61);
btn_开始加热.FillColor = Color.FromArgb(80, 160, 255);
}
private void uiComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
switch (uiComboBox1.SelectedIndex)
{
case 0:
config.AppSettings.Settings["SampleRate"].Value = "1000";
break;
case 1:
config.AppSettings.Settings["SampleRate"].Value = "5000";
break;
case 2:
config.AppSettings.Settings["SampleRate"].Value = "10000";
break;
case 3:
config.AppSettings.Settings["SampleRate"].Value = "50000";
break;
case 4:
config.AppSettings.Settings["SampleRate"].Value = "100000";
break;
case 5:
config.AppSettings.Settings["SampleRate"].Value = "200000";
break;
}
// 保存更改
config.Save(ConfigurationSaveMode.Modified);
// 强制重新加载配置节,以确保更改立即生效(可选)
ConfigurationManager.RefreshSection("appSettings");
MessageBox.Show("修改成功!重启软件生效!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void uiComboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
{
}
}
}