using System.Text.Json.Serialization; namespace GSModbus.Config { /// /// Modbus通信完整配置 /// public class ModbusConfiguration { /// /// 配置文件版本 /// [JsonPropertyName("ConfigVersion")] public string ConfigVersion { get; set; } = "1.0"; /// /// 项目名称 /// [JsonPropertyName("ProjectName")] public string ProjectName { get; set; } = string.Empty; /// /// 配置描述 /// [JsonPropertyName("Description")] public string Description { get; set; } = string.Empty; /// /// 连接配置 /// [JsonPropertyName("Connection")] public ConnectionConfig Connection { get; set; } = new(); /// /// 通信配置 /// [JsonPropertyName("Communication")] public CommunicationConfig Communication { get; set; } = new(); /// /// 输出地址配置(MES发送给PLC) /// [JsonPropertyName("OutputAddresses")] public OutputAddressConfig OutputAddresses { get; set; } = new(); /// /// 输入地址配置(从PLC读取) /// [JsonPropertyName("InputAddresses")] public InputAddressConfig InputAddresses { get; set; } = new(); /// /// 用户界面配置 /// [JsonPropertyName("UI")] public UIConfig UI { get; set; } = new(); /// /// 数据库配置 /// [JsonPropertyName("Database")] public DatabaseConfig Database { get; set; } = new(); } /// /// 连接配置 /// public class ConnectionConfig { /// /// PLC IP地址 /// [JsonPropertyName("IpAddress")] public string IpAddress { get; set; } = "192.168.1.100"; /// /// PLC端口号 /// [JsonPropertyName("Port")] public int Port { get; set; } = 502; /// /// 连接超时时间(毫秒) /// [JsonPropertyName("ConnectionTimeoutMs")] public int ConnectionTimeoutMs { get; set; } = 5000; /// /// 操作超时时间(毫秒) /// [JsonPropertyName("OperationTimeoutMs")] public int OperationTimeoutMs { get; set; } = 3000; } /// /// 通信配置 /// public class CommunicationConfig { /// /// 心跳间隔(毫秒) /// [JsonPropertyName("HeartbeatIntervalMs")] public int HeartbeatIntervalMs { get; set; } = 1000; /// /// 数据轮询间隔(毫秒) /// [JsonPropertyName("DataPollingIntervalMs")] public int DataPollingIntervalMs { get; set; } = 500; /// /// 是否启用自动重连 /// [JsonPropertyName("EnableAutoReconnect")] public bool EnableAutoReconnect { get; set; } = true; /// /// 重连延迟(毫秒) /// [JsonPropertyName("ReconnectDelayMs")] public int ReconnectDelayMs { get; set; } = 3000; /// /// 最大重试次数 /// [JsonPropertyName("MaxRetryCount")] public int MaxRetryCount { get; set; } = 3; } /// /// 输出地址配置(MES发送给PLC) /// public class OutputAddressConfig { /// /// 配置描述 /// [JsonPropertyName("Description")] public string Description { get; set; } = string.Empty; /// /// 心跳地址配置 /// [JsonPropertyName("HeartbeatAddress")] public AddressField HeartbeatAddress { get; set; } = new(); /// /// 数据确认地址配置 /// [JsonPropertyName("DataConfirmationAddress")] public AddressField DataConfirmationAddress { get; set; } = new(); } /// /// 输入地址配置(从PLC读取) /// public class InputAddressConfig { /// /// 配置描述 /// [JsonPropertyName("Description")] public string Description { get; set; } = string.Empty; /// /// 控制信号配置 /// [JsonPropertyName("ControlSignals")] public Dictionary ControlSignals { get; set; } = new(); /// /// 产品数据配置 /// [JsonPropertyName("ProductData")] public Dictionary ProductData { get; set; } = new(); /// /// 测量数据配置 /// [JsonPropertyName("MeasurementData")] public Dictionary MeasurementData { get; set; } = new(); } /// /// 简单地址字段配置 /// public class AddressField { /// /// 寄存器地址 /// [JsonPropertyName("Address")] public int Address { get; set; } /// /// 数据类型 /// [JsonPropertyName("DataType")] public string DataType { get; set; } = "Byte"; /// /// 字段描述 /// [JsonPropertyName("Description")] public string Description { get; set; } = string.Empty; } /// /// 数据字段配置 /// public class DataField : AddressField { /// /// 数据长度(寄存器数量) /// [JsonPropertyName("Length")] public int Length { get; set; } = 1; /// /// 显示名称 /// [JsonPropertyName("DisplayName")] public string DisplayName { get; set; } = string.Empty; /// /// 编码方式(用于字符串和时间戳) /// [JsonPropertyName("Encoding")] public string Encoding { get; set; } = "BigEndian"; /// /// 缩放因子(用于整数转换为实际值) /// [JsonPropertyName("Scale")] public double Scale { get; set; } = 1.0; /// /// 单位 /// [JsonPropertyName("Unit")] public string Unit { get; set; } = string.Empty; /// /// 小数位数 /// [JsonPropertyName("DecimalPlaces")] public int DecimalPlaces { get; set; } = 0; /// /// 时间戳格式(用于时间戳数据类型) /// [JsonPropertyName("Format")] public string Format { get; set; } = string.Empty; /// /// 值映射(用于枚举值显示) /// [JsonPropertyName("ValueMap")] public Dictionary? ValueMap { get; set; } } /// /// 用户界面配置 /// public class UIConfig { /// /// 窗口标题 /// [JsonPropertyName("WindowTitle")] public string WindowTitle { get; set; } = "GSModbus - 通用通信系统"; /// /// 是否自动开始数据轮询 /// [JsonPropertyName("AutoStartPolling")] public bool AutoStartPolling { get; set; } = false; /// /// 日志级别 /// [JsonPropertyName("LogLevel")] public string LogLevel { get; set; } = "Info"; /// /// 最大日志行数 /// [JsonPropertyName("MaxLogLines")] public int MaxLogLines { get; set; } = 1000; } /// /// 支持的数据类型枚举 /// public enum ModbusDataType { Byte, // 单字节 Integer, // 整数 String, // 字符串 Timestamp, // 时间戳 Float, // 浮点数 Boolean // 布尔值 } /// /// 编码方式枚举 /// public enum EncodingType { BigEndian, // 大端 LittleEndian // 小端 } /// /// 数据库配置 /// public class DatabaseConfig { /// /// 是否启用数据库 /// [JsonPropertyName("Enabled")] public bool Enabled { get; set; } = false; /// /// 数据库类型(SqlServer, MySql, Oracle, Sqlite等) /// [JsonPropertyName("Type")] public string Type { get; set; } = "SqlServer"; /// /// 数据库连接字符串 /// [JsonPropertyName("ConnectionString")] public string ConnectionString { get; set; } = string.Empty; /// /// 是否自动创建表 /// [JsonPropertyName("AutoCreateTables")] public bool AutoCreateTables { get; set; } = true; /// /// 批量插入大小 /// [JsonPropertyName("BatchSize")] public int BatchSize { get; set; } = 100; /// /// 数据保留天数(0表示永久保留) /// [JsonPropertyName("DataRetentionDays")] public int DataRetentionDays { get; set; } = 30; /// /// 表配置 /// [JsonPropertyName("Tables")] public DatabaseTableConfig Tables { get; set; } = new(); } /// /// 数据库表配置 /// public class DatabaseTableConfig { /// /// 主数据表名 /// [JsonPropertyName("ModbusDataTable")] public string ModbusDataTable { get; set; } = "ModbusData"; /// /// 通信日志表名 /// [JsonPropertyName("CommunicationLogTable")] public string CommunicationLogTable { get; set; } = "CommunicationLog"; /// /// 错误日志表名 /// [JsonPropertyName("ErrorLogTable")] public string ErrorLogTable { get; set; } = "ErrorLog"; /// /// 统计数据表名 /// [JsonPropertyName("StatisticsTable")] public string StatisticsTable { get; set; } = "Statistics"; } }