整理教材示例:项目 1 任务 2、项目 4 任务 1 和 3、项目 5 任务 1

- 界面取自配套参考工程,用设计器引擎按书上表格改控件名(label1 → lbl_port 等),
  去掉 BOM、换行统一为 LF;代码按书整理,修正 boolIsOpen 等印刷错误
- 测试:所有示例都能编译(10 个),以及计算器 / 多态、红外对射、报警灯、火焰的行为测试

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
xuyueandClaude Opus 5.5 committed 2026-10-22 11:29:37 +08:00
1 parent 8feda289de
commit 4c53caf7fa
22 files changed
+1101 -7

No files matched your search

+138
View File
@@ -0,0 +1,138 @@
namespace 报警灯
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.alarm1 = new WinFormsDeviceControlLibrary.Alarm();
this.btn_alarmOpen = new System.Windows.Forms.Button();
this.btn_alarmClose = new System.Windows.Forms.Button();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.btn_open = new System.Windows.Forms.Button();
this.com_port = new System.Windows.Forms.ComboBox();
this.label1 = new System.Windows.Forms.Label();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// alarm1
//
this.alarm1.BackColor = System.Drawing.Color.White;
this.alarm1.Location = new System.Drawing.Point(12, 12);
this.alarm1.Name = "alarm1";
this.alarm1.Size = new System.Drawing.Size(150, 150);
this.alarm1.TabIndex = 0;
//
// btn_alarmOpen
//
this.btn_alarmOpen.Location = new System.Drawing.Point(12, 192);
this.btn_alarmOpen.Name = "btn_alarmOpen";
this.btn_alarmOpen.Size = new System.Drawing.Size(35, 23);
this.btn_alarmOpen.TabIndex = 1;
this.btn_alarmOpen.Text = "开";
this.btn_alarmOpen.UseVisualStyleBackColor = true;
this.btn_alarmOpen.Click += new System.EventHandler(this.btn_alarmOpen_Click);
//
// btn_alarmClose
//
this.btn_alarmClose.Location = new System.Drawing.Point(127, 192);
this.btn_alarmClose.Name = "btn_alarmClose";
this.btn_alarmClose.Size = new System.Drawing.Size(35, 23);
this.btn_alarmClose.TabIndex = 2;
this.btn_alarmClose.Text = "关";
this.btn_alarmClose.UseVisualStyleBackColor = true;
this.btn_alarmClose.Click += new System.EventHandler(this.btn_alarmClose_Click);
//
// groupBox1
//
this.groupBox1.Controls.Add(this.btn_open);
this.groupBox1.Controls.Add(this.com_port);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Location = new System.Drawing.Point(195, 58);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(218, 114);
this.groupBox1.TabIndex = 3;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "配置";
//
// btn_open
//
this.btn_open.Location = new System.Drawing.Point(113, 71);
this.btn_open.Name = "btn_open";
this.btn_open.Size = new System.Drawing.Size(75, 23);
this.btn_open.TabIndex = 2;
this.btn_open.Text = "开启";
this.btn_open.UseVisualStyleBackColor = true;
this.btn_open.Click += new System.EventHandler(this.btn_open_Click);
//
// com_port
//
this.com_port.FormattingEnabled = true;
this.com_port.Location = new System.Drawing.Point(67, 33);
this.com_port.Name = "com_port";
this.com_port.Size = new System.Drawing.Size(121, 20);
this.com_port.TabIndex = 1;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(20, 36);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(41, 12);
this.label1.TabIndex = 0;
this.label1.Text = "报警灯";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.White;
this.ClientSize = new System.Drawing.Size(425, 262);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.btn_alarmClose);
this.Controls.Add(this.btn_alarmOpen);
this.Controls.Add(this.alarm1);
this.Name = "Form1";
this.Text = "报警灯";
this.Load += new System.EventHandler(this.Form1_Load);
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private WinFormsDeviceControlLibrary.Alarm alarm1;
private System.Windows.Forms.Button btn_alarmOpen;
private System.Windows.Forms.Button btn_alarmClose;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Button btn_open;
private System.Windows.Forms.ComboBox com_port;
private System.Windows.Forms.Label label1;
}
}
+73
View File
@@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using DeviceCtrlLibrary;
namespace 报警灯
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
bool IsOpen = false;
private void Form1_Load(object sender, EventArgs e)
{
com_port.Items.Clear();
foreach (string item in SerialPort.GetPortNames())
{
com_port.Items.Add(item);
}
}
private void AlarmOnOff(string Device, bool IsOpen)
{
//利用switch语句打开或关闭继电器模块
switch (Device)
{
case "Alarm":
DeviceCtrlHelper.adam4150Helper.OnOff(0, IsOpen);//报警灯接入DO0
alarm1.OnOff(IsOpen);//更改界面报警灯状态
break;
case "LED1": DeviceCtrlHelper.adam4150Helper.OnOff(1, IsOpen); break;//开启或关闭继电器1模块
case "LED2": DeviceCtrlHelper.adam4150Helper.OnOff(2, IsOpen); break;//开启或关闭继电器2模块
default: MessageBox.Show("没有合适的接口程序!", "错误提示!"); break;//若为其他模块,则提示错误
}
}
private void btn_open_Click(object sender, EventArgs e)
{
IsOpen = !IsOpen;
if (IsOpen)
{
DeviceCtrlHelper.adam4150Helper.Open(com_port.Text);
btn_open.Text = "关闭";
}
else
{
DeviceCtrlHelper.adam4150Helper.Close();
btn_open.Text = "开启";
}
}
private void btn_alarmOpen_Click(object sender, EventArgs e)
{
AlarmOnOff("Alarm", true);
}
private void btn_alarmClose_Click(object sender, EventArgs e)
{
AlarmOnOff("Alarm", false);
}
}
}
+22
View File
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 报警灯
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
+4
View File
@@ -0,0 +1,4 @@
{
"title": "项目4 任务3 控制报警灯",
"source": "教材书 p84–87,界面取自配套参考工程;修正书上 boolIsOpen 少空格"
}