增加调节温度功能

This commit is contained in:
uu
2025-06-18 16:24:20 +08:00
parent 37685a7c5d
commit 97d0d3c578
7 changed files with 100 additions and 6 deletions

View File

@@ -219,6 +219,11 @@ namespace HitBotCSharpDemo
textBox8.Click += TextBox_Click;
textBox9.Click += TextBox_Click;
textBox2.Click += TextBox_SetTemp_Click;
textBox3.Click += TextBox_SetTemp_Click;
textBox4.Click += TextBox_SetTemp_Click;
textBox5.Click += TextBox_SetTemp_Click;
}
private void TextBox_Click(object sender, EventArgs e)
@@ -281,6 +286,67 @@ namespace HitBotCSharpDemo
inputForm.Dispose();
}
}
private void TextBox_SetTemp_Click(object sender, EventArgs e)
{
TextBox textBox = sender as TextBox;
if (textBox == null) return;
// 检查串口是否打开
if (tempControl == null || !tempControl.IsSerialPortOpen())
{
MessageBox.Show("串口未打开,无法设置参数");
return;
}
// 根据控件名称确定下位机编号和显示名称
int deviceNumber = -1;
string parameterName = "";
switch (textBox.Name)
{
case "textBox2":
deviceNumber = 0;
parameterName = "下位机1 设定温度";
break;
case "textBox3":
deviceNumber = 1;
parameterName = "下位机2 设定温度";
break;
case "textBox4":
deviceNumber = 2;
parameterName = "下位机3 设定温度";
break;
case "textBox5":
deviceNumber = 3;
parameterName = "下位机4 设定温度";
break;
}
if (deviceNumber >= 0)
{
// 创建并显示输入窗口
ParameterInputForm inputForm = new ParameterInputForm(parameterName, textBox.Text);
if (inputForm.ShowDialog(this) == DialogResult.OK)
{
// 用户点击了保存按钮
string newValue = inputForm.InputValue;
// 检查输入是否为有效数字
if (double.TryParse(newValue, out double inputValue))
{
// 更新textbox显示
textBox.Text = newValue;
// 构建并发送命令
string command = $"TC1:TCADJUSTTEMP={inputValue}@{deviceNumber}\r";
tempControl.SendSetTempCommand(command);
}
else
{
MessageBox.Show("请输入有效的数字", "输入错误",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
inputForm.Dispose();
}
}
private void cob_Robot_ID_Click(object sender, EventArgs e)
{

View File

@@ -31,7 +31,7 @@ namespace HitBotCSharpDemo
private Label[] settingTemperatureLabels;
private int currentCommandIndex = 0;
private int currentSettingCommandIndex = 0;
private string lastCommandType = "";
private string receivedData = "";
public tempControl(Label[] labels, Label[] setting_labels)
@@ -283,16 +283,13 @@ namespace HitBotCSharpDemo
{
string replyPart = parts[0];
string addressPart = parts[1];
string[] replyParts = replyPart.Split('=');
if (replyParts.Length == 2)
{
int errorCode = int.Parse(replyParts[1]);
int address = int.Parse(addressPart);
// 处理错误码
string message = GetErrorMessage(errorCode);
// 在UI线程中显示消息
if (temperatureLabels[0].InvokeRequired)
{
@@ -342,16 +339,18 @@ namespace HitBotCSharpDemo
}
private void ShowCommandResult(int address, int errorCode, string message)
{
string commandName = lastCommandType == "SETTEMP" ? "设定温度" : "RAMPSPEED";
if (errorCode == 1)
{
// 设置成功,显示成功消息
MessageBox.Show($"下位机{address + 1}号 RAMPSPEED设置成功", "提示",
MessageBox.Show($"下位机{address + 1}号 {commandName}设置成功", "提示",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
// 设置失败,显示错误消息
MessageBox.Show($"下位机{address + 1}号 RAMPSPEED设置失败\n错误码: {errorCode}\n错误信息: {message}",
MessageBox.Show($"下位机{address + 1}号 {commandName}设置失败\n错误码: {errorCode}\n错误信息: {message}",
"错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
@@ -453,6 +452,10 @@ namespace HitBotCSharpDemo
{
try
{
// 记录命令类型
if (command.Contains("TCRAMPSPEED"))
lastCommandType = "RAMPSPEED";
serialPort.Write(command);
}
catch (Exception ex)
@@ -467,6 +470,31 @@ namespace HitBotCSharpDemo
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
public void SendSetTempCommand(string command)
{
if (serialPort != null && serialPort.IsOpen)
{
try
{
// 记录命令类型
lastCommandType = "SETTEMP";
serialPort.Write(command);
}
catch (Exception ex)
{
MessageBox.Show($"发送设定温度命令失败: {ex.Message}", "错误",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("串口未打开", "提示",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
}