# Snet local example environment / 本地示例环境

Download **snet-local-demo.zip** beside this file and extract it into a new folder.
下载本目录的 **snet-local-demo.zip**，解压到新目录后运行。

Requires Docker Engine/Desktop with Compose v2, and the .NET 10 SDK on the host.
需要 Docker Engine/Desktop、Compose v2，以及宿主机上的 .NET 10 SDK。
The bundle pins one tested Snet version in every project; see its `VERSION` file.
包内三个项目使用同一验证版本，详见 `VERSION`，不要手工混用不同版本。

## 1. Start simulators / 启动环境

```sh
docker compose up -d --wait
docker compose ps
```

Only host loopback ports are published: Modbus `127.0.0.1:1502`, MQTT `127.0.0.1:1883`.
仅向本机回环发布端口。Modbus 模拟器提供站号 1、功能码 03、零基寄存器 0 的值 1234。
示例使用 `AddressStartWithZero=false`，所以地址字符串 `"1"` 对应该寄存器。
The simulator implements the small demo protocol subset, not a complete PLC emulator.
Mosquitto uses anonymous access within this local demo environment.

## 2. Read a value / 读取

```sh
dotnet run --project examples/modbus/Demo.csproj
```

The result JSON contains `ResultValue: 1234` for address `1`.
结果 JSON 中，地址 `1` 的 `ResultValue` 应为 1234。

## 3. Observe forwarding / 观察转发

Start the subscriber **first** in terminal A / 先在终端 A 启动订阅：

```sh
docker compose exec mqtt mosquitto_sub -h 127.0.0.1 -t snet/demo -v -C 1
```

In terminal B / 然后在终端 B 运行：

```sh
dotnet run --project examples/mqtt/Demo.csproj
```

The publisher prints `Published`; terminal A receives the address/value JSON.
发布端显示 Published，终端 A 收到含地址与数值的 JSON。消息不保留，因此订阅者须先启动。

## 4. Siemens and scope / Siemens 与验证范围

```sh
dotnet build examples/siemens/Demo.csproj
```

S7 is compile-only. This stack contains **no S7 simulator** and does not claim physical-PLC validation.
S7 仅编译；包内没有 S7 模拟器，连接参数需按真实设备调整。

The same published Modbus simulator was used for the recorded .NET loopback check;
MQTT receipt was checked with an in-process protocol test server. Docker is unavailable
in the implementation environment, so this Compose/Mosquitto stack has not been run here.
本次代码已编译并进行 Modbus/MQTT 回环断言；由于本机无 Docker，未实际启动该容器栈。

If Docker is unavailable, Modbus alone can run with `python modbus_simulator.py`.
The MQTT example still needs a broker. If a port is occupied, stop the conflicting local
test service or update both Compose and the example configuration consistently.

## 5. Stop / 停止

```sh
docker compose down
```

References: [Docker Compose](https://docs.docker.com/compose/gettingstarted/),
[Mosquitto](https://mosquitto.org/documentation/),
[Snet quickstart](https://docs.snet.cn/getting-started/quickstart).
