Files
IOT_SIM/runtime/Sim.Devices/DeviceCtrlLibrary/TrainingBoard.cs
T
xuyueandClaude Opus 5.5 26104ae81e 第 1 阶段:虚拟实训台,项目 2、3 的示例跑通
- 实训台状态由服务器统一保存:滑块和开关设定传感器读数,推给学生程序;
  学生程序控制的灯、继电器、LED 屏回写到实训台
- 网页按教材实训台布局图绘制实训台,传感器卡片上直接调节环境
- 新控件:CheckBox、RadioButton(事件顺序与 WinForms 一致),Lamp、Alarm
- 设备类库:串口未打开时控制无效,与实物一致
- 修正:列表属性序列化时数字被转成字符串
- 示例:项目 2 任务 2–3、项目 3 任务 1–2;网址参数 ?sample= 打开指定示例
- 测试:实训台单元测试,项目 2、3 的端到端测试

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 10:32:38 +08:00

47 lines
1.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Text.Json.Nodes;
namespace DeviceCtrlLibrary;
/// <summary>
/// 学生程序进程里的实训台传感器读数副本。权威状态在 Bun 服务器上(网页滑块和开关控制):
/// 启动时从环境变量 SIM_BOARD 读入,之后服务器每次变化都通过标准输入推送过来。
/// SetData() 直接读这里,不需要等待网络往返。
/// </summary>
public static class TrainingBoard
{
private static readonly object Lock = new();
// ADAM-4017 Vin0–Vin7(温度、湿度、光照、气压、空气质量、CO2、风速、预留)
private static int[] _analog = { 25, 60, 500, 101, 60, 600, 3, 0 };
// ADAM-4150 DI0–DI6(人体红外、火焰、烟雾、红外对射、预留)
private static bool[] _digital = new bool[7];
static TrainingBoard()
{
var initial = Environment.GetEnvironmentVariable("SIM_BOARD");
if (!string.IsNullOrEmpty(initial) && JsonNode.Parse(initial) is JsonObject json) Update(json);
}
/// <summary>平台内部使用:应用服务器推送的 {"analog":[…],"digital":[…]}</summary>
public static void Update(JsonObject json)
{
lock (Lock)
{
if (json["analog"] is JsonArray analog)
for (var i = 0; i < analog.Count && i < _analog.Length; i++) _analog[i] = (int?)analog[i] ?? 0;
if (json["digital"] is JsonArray digital)
for (var i = 0; i < digital.Count && i < _digital.Length; i++) _digital[i] = (bool?)digital[i] ?? false;
}
}
internal static int ReadAnalog(int channel)
{
lock (Lock) return channel >= 0 && channel < _analog.Length ? _analog[channel] : 0;
}
internal static bool ReadDigital(int channel)
{
lock (Lock) return channel >= 0 && channel < _digital.Length && _digital[channel];
}
}