配置 - Snet Docs

⚙️ 配置

本页记录了三种最常见设备类型的实际配置属性:Modbus、Siemens 和 MQTT Client。

所有配置类都是嵌套的 Basics 类,它们继承自 SubscribeData.SCData(用于 DAQ 协议)或是独立的(用于 MQTT)。


ModbusData.Basics

命名空间:Snet.Modbus 继承自:SubscribeData.SCData

属性 类型 默认值 描述
SN string 自动 GUID 此设备实例的唯一标识符
IpAddress string? "127.0.0.1" Modbus 设备的 IP 地址
Port int 6688 TCP/UDP 端口号
ConnectTimeOut int 1000 连接超时时间(毫秒)
ReceiveTimeOut int 1000 接收超时时间(毫秒)
SleepTime int 0 操作之间的休眠时间(毫秒)
SocketKeepAliveTime int -1 Socket 保活间隔(毫秒),-1 表示禁用
IsPersistentConnection bool true 是否保持连接存活
Station int 1 Modbus 从站 ID
AddressStartWithZero bool true 地址是否从 0 或 1 开始
IsCheckMessageId bool true 检查事务 ID 一致性
IsStringReverse bool false 反转字符串中的字节顺序
DataFormat DataFormat CDAB 字节顺序:ABCDBADCCDABDCBA
SerialPortInfo string? "COM3-9600-8-N-1" 串口配置字符串(RTU/ASCII 模式)
RtsEnable bool false 启用 RS-485 的 RTS 信号
DtrEnable bool false 启用 DTR 信号
Crc16CheckEnable bool true 启用 CRC16 校验
IsClearCacheBeforeRead bool false 读取前清除缓冲区
StationCheckMatch bool true 验证响应中的站号
ProtocolType ProtocolType ModbusTcpNet 协议变体(见下文)

ModbusData.ProtocolType 枚举

描述
ModbusTcpNet 标准 Modbus TCP 客户端
ModbusUdpNet 基于 UDP 的 Modbus
ModbusRtu 基于串口的 Modbus RTU
ModbusRtuOverTcp 通过 TCP 隧道的 Modbus RTU
ModbusAscii 基于串口的 Modbus ASCII
ModbusAsciiOverTcp 通过 TCP 隧道的 Modbus ASCII

示例:Modbus TCP

var basics = new ModbusData.Basics
{
    IpAddress = "192.168.1.100",
    Port = 502,
    Station = 1,
    DataFormat = Snet.Driver.Core.DataFormat.CDAB
};

示例:Modbus RTU 通过串口

var basics = new ModbusData.Basics
{
    SerialPortInfo = "COM3-9600-8-N-1",
    Station = 1,
    DataFormat = Snet.Driver.Core.DataFormat.CDAB,
    ProtocolType = ModbusData.ProtocolType.ModbusRtu,
    RtsEnable = true,
    Crc16CheckEnable = true
};

SiemensData.Basics

命名空间:Snet.Siemens 继承自:SubscribeData.SCData

属性 类型 默认值 描述
SN string 自动 GUID 唯一标识符
IpAddress string? "127.0.0.1" Siemens PLC 的 IP 地址
Port int 6688 TCP 端口(S7 通常为 102)
ConnectTimeOut int 1000 连接超时时间(毫秒)
ReceiveTimeOut int 1000 接收超时时间(毫秒)
SleepTime int 0 操作之间的休眠时间(毫秒)
SocketKeepAliveTime int -1 保活间隔,-1 表示禁用
IsPersistentConnection bool true 保持 TCP 连接存活
PDULength int -1 PDU 大小(-1 = 自动协商)
LocalTSAP int 19799 本地 TSAP 参数
Rack int 0 PLC 机架号
Slot int 0 PLC 槽位号
SerialPortInfo string? "COM3-9600-8-N-1" PPI 模式的串口
RtsEnable bool false 串口 RTS 信号
DtrEnable bool false 串口 DTR 信号
Station int 1 站号
ProtocolType ProtocolType SiemensPPIOverTcp 协议变体

SiemensData.ProtocolType 枚举(部分)

描述
SiemensPPIOverTcp 基于 TCP 的 PPI 协议(S7-200)
SiemensS7Net_S200 S7-200 系列的 S7 协议
SiemensS7Net_S300 S7-300/400 系列的 S7 协议
SiemensS7Net_S1200 S7-1200 系列的 S7 协议
SiemensS7Net_S1500 S7-1500 系列的 S7 协议
SiemensFetchWriteNet Fetch/Write 协议

示例:Siemens S7-1200

var basics = new SiemensData.Basics
{
    IpAddress = "192.168.1.50",
    Port = 102,
    Rack = 0,
    Slot = 1,
    ProtocolType = SiemensData.ProtocolType.SiemensS7Net_S1200
};

MqttClientData.Basics

命名空间:Snet.Mqtt.client 不继承自 SubscribeData.SCData(独立类)。

属性 类型 默认值 描述
SN string 自动 GUID 唯一标识符
IpAddress string "127.0.0.1" MQTT Broker IP 地址
Port int 6688 MQTT Broker 端口(通常为 1883)
UserName string? "shunnet" 认证用户名
Password string? "shunnet" 认证密码
ClientID string? null 客户端标识符(为 null 时自动生成)
MessageExpirationTime int 86400000 消息过期时间(毫秒,默认 24 小时)
QualityOfServiceLevel MqttQualityOfServiceLevel AtMostOnce QoS 0、1 或 2
ResponseType ResponseType Content 响应处理模式

MqttQualityOfServiceLevel

级别 含义
AtMostOnce(0) 发送即忘,无确认
AtLeastOnce(1) 保证送达,可能重复
ExactlyOnce(2) 保证精确一次送达

示例:MQTT Client

var basics = new MqttClientData.Basics
{
    IpAddress = "broker.emqx.io",
    Port = 1883,
    UserName = "myuser",
    Password = "mypassword",
    QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce
};

传递配置属性

由于 ModbusData.BasicsSiemensData.Basics 继承自 SubscribeData.SCData,它们继承了与订阅相关的属性(由订阅管道内部使用):

继承的属性 默认值 描述
HandleInterval 1000 订阅的轮询间隔(毫秒)
ChangeOut false 仅在值变化时发送通知
AllOut false 一次性发送所有订阅数据
TaskNumber 1 并发轮询任务数