添加开始升温按钮

This commit is contained in:
uu
2025-06-18 17:29:49 +08:00
parent 97d0d3c578
commit 8658d7e1ff
6 changed files with 112 additions and 10 deletions

View File

@@ -65,6 +65,8 @@ namespace HitBotCSharpDemo
private int dwellTimeMinutes = 1; // 停留时间(分钟) private int dwellTimeMinutes = 1; // 停留时间(分钟)
private string cycleStartTime = ""; // 循环开始时间 private string cycleStartTime = ""; // 循环开始时间
private bool[] heatingStates = new bool[4];
private CameraManager cameraManager; private CameraManager cameraManager;
private tempControl tempControl; private tempControl tempControl;
private ControlBeanEx robot; private ControlBeanEx robot;
@@ -224,6 +226,22 @@ namespace HitBotCSharpDemo
textBox4.Click += TextBox_SetTemp_Click; textBox4.Click += TextBox_SetTemp_Click;
textBox5.Click += TextBox_SetTemp_Click; textBox5.Click += TextBox_SetTemp_Click;
InitializeHeatingButtons();
}
private void InitializeHeatingButtons()
{
// 初始化升温状态为关闭
for (int i = 0; i < heatingStates.Length; i++)
{
heatingStates[i] = false;
}
// 设置按钮初始文本
button4.Text = "开始升温";
button7.Text = "开始升温";
button9.Text = "开始升温";
button11.Text = "开始升温";
} }
private void TextBox_Click(object sender, EventArgs e) private void TextBox_Click(object sender, EventArgs e)
@@ -1475,27 +1493,62 @@ namespace HitBotCSharpDemo
private void button4_Click_1(object sender, EventArgs e) private void button4_Click_1(object sender, EventArgs e)
{ {
HandleHeatingButtonClick(0, button4); // 下位机0
} }
private void button7_Click(object sender, EventArgs e) private void button7_Click(object sender, EventArgs e)
{ {
HandleHeatingButtonClick(1, button7); // 下位机1
} }
private void button9_Click(object sender, EventArgs e) private void button9_Click(object sender, EventArgs e)
{ {
HandleHeatingButtonClick(2, button9); // 下位机2
} }
private void button11_Click(object sender, EventArgs e) private void button11_Click(object sender, EventArgs e)
{ {
HandleHeatingButtonClick(3, button11); // 下位机3
} }
private void label26_Click(object sender, EventArgs e) private void label26_Click(object sender, EventArgs e)
{ {
} }
private void HandleHeatingButtonClick(int deviceNumber, Button button)
{
// 检查串口是否打开
if (tempControl == null || !tempControl.IsSerialPortOpen())
{
MessageBox.Show("串口未打开,无法控制升温");
return;
}
// 切换升温状态
heatingStates[deviceNumber] = !heatingStates[deviceNumber];
// 构建命令
int commandValue = heatingStates[deviceNumber] ? 1 : 0;
string command = $"TC1:TCSW={commandValue}@{deviceNumber}\r";
// 发送升温控制命令
tempControl.SendHeatingCommand(command, deviceNumber);
// 更新按钮文本
button.Text = heatingStates[deviceNumber] ? "关闭升温" : "开始升温";
// 临时禁用按钮,避免重复点击
button.Enabled = false;
// 使用定时器在1秒后重新启用按钮
System.Windows.Forms.Timer enableTimer = new System.Windows.Forms.Timer();
enableTimer.Interval = 1000;
enableTimer.Tick += (s, args) =>
{
button.Enabled = true;
enableTimer.Stop();
enableTimer.Dispose();
};
enableTimer.Start();
}
} }
} }

View File

@@ -33,7 +33,8 @@ namespace HitBotCSharpDemo
private int currentSettingCommandIndex = 0; private int currentSettingCommandIndex = 0;
private string lastCommandType = ""; private string lastCommandType = "";
private string receivedData = ""; private string receivedData = "";
private string lastHeatingCommand = "";
private int lastHeatingDeviceNumber = -1;
public tempControl(Label[] labels, Label[] setting_labels) public tempControl(Label[] labels, Label[] setting_labels)
{ {
temperatureLabels = labels; temperatureLabels = labels;
@@ -288,8 +289,10 @@ namespace HitBotCSharpDemo
{ {
int errorCode = int.Parse(replyParts[1]); int errorCode = int.Parse(replyParts[1]);
int address = int.Parse(addressPart); int address = int.Parse(addressPart);
// 处理错误码 // 处理错误码
string message = GetErrorMessage(errorCode); string message = GetErrorMessage(errorCode);
// 在UI线程中显示消息 // 在UI线程中显示消息
if (temperatureLabels[0].InvokeRequired) if (temperatureLabels[0].InvokeRequired)
{ {
@@ -339,13 +342,34 @@ namespace HitBotCSharpDemo
} }
private void ShowCommandResult(int address, int errorCode, string message) private void ShowCommandResult(int address, int errorCode, string message)
{ {
string commandName = lastCommandType == "SETTEMP" ? "设定温度" : "RAMPSPEED"; string commandName;
switch (lastCommandType)
{
case "SETTEMP":
commandName = "设定温度";
break;
case "HEATING":
commandName = "升温控制";
break;
default:
commandName = "参数设置";
break;
}
if (errorCode == 1) if (errorCode == 1)
{ {
// 设置成功,显示成功消息 // 设置成功
MessageBox.Show($"下位机{address + 1}号 {commandName}设置成功", "提示", if (lastCommandType == "HEATING")
MessageBoxButtons.OK, MessageBoxIcon.Information); {
// 升温命令成功,显示简短提示
string heatingAction = lastHeatingCommand.Contains("=1@") ? "开始升温" : "关闭升温";
MessageBox.Show($"下位机{address + 1}号 {heatingAction}设置成功", "提示",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show($"下位机{address + 1}号 {commandName}设置成功", "提示",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
} }
else else
{ {
@@ -493,6 +517,31 @@ namespace HitBotCSharpDemo
MessageBoxButtons.OK, MessageBoxIcon.Warning); MessageBoxButtons.OK, MessageBoxIcon.Warning);
} }
} }
public void SendHeatingCommand(string command, int deviceNumber)
{
if (serialPort != null && serialPort.IsOpen)
{
try
{
// 记录命令类型和设备号
lastCommandType = "HEATING";
lastHeatingCommand = command;
lastHeatingDeviceNumber = deviceNumber;
serialPort.Write(command);
}
catch (Exception ex)
{
MessageBox.Show($"发送升温命令失败: {ex.Message}", "错误",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("串口未打开", "提示",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
} }