330 lines
12 KiB
C#
330 lines
12 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using ProtocolUnit = Modbus.Net.ProtocolUnit<byte[], byte[]>;
|
||
|
||
namespace Modbus.Net.Siemens
|
||
{
|
||
/// <summary>
|
||
/// 西门子 S7 协议枚举和基类 / Siemens S7 Protocol Enums and Base Classes
|
||
/// <remarks>
|
||
/// 定义西门子 S7 协议的数据类型、错误码和协议基类
|
||
/// Defines data types, error codes and protocol base classes for Siemens S7 protocol
|
||
/// <para>
|
||
/// 主要枚举 / Main Enums:
|
||
/// <list type="bullet">
|
||
/// <item><strong>SiemensTypeCode</strong> - 数据类型编码 / Data type codes</item>
|
||
/// <item><strong>SiemensAccessResult</strong> - 访问结果错误码 / Access result error codes</item>
|
||
/// <item><strong>SiemensDataType</strong> - 数据访问类型 / Data access types</item>
|
||
/// </list>
|
||
/// </para>
|
||
/// </remarks>
|
||
/// </summary>
|
||
|
||
#region 西门子数据类型枚举 / Siemens Data Type Enums
|
||
|
||
/// <summary>
|
||
/// 西门子数据类型代码 / Siemens Data Type Code
|
||
/// <remarks>
|
||
/// 定义西门子 PLC 支持的各种数据类型
|
||
/// Defines various data types supported by Siemens PLC
|
||
/// <para>
|
||
/// 数据类型说明 / Data Type Description:
|
||
/// <list type="bullet">
|
||
/// <item><strong>Bool (0x01)</strong> - 布尔类型,1 位 / Boolean type, 1 bit</item>
|
||
/// <item><strong>Byte (0x02)</strong> - 字节类型,8 位 / Byte type, 8 bits</item>
|
||
/// <item><strong>Word (0x03)</strong> - 字类型,16 位 / Word type, 16 bits</item>
|
||
/// <item><strong>DWord (0x04)</strong> - 双字类型,32 位 / Double word type, 32 bits</item>
|
||
/// <item><strong>C (0x1E)</strong> - 计数器 / Counter</item>
|
||
/// <item><strong>T (0x1F)</strong> - 计时器 / Timer</item>
|
||
/// <item><strong>HC (0x20)</strong> - 高速计数器 / High-speed counter</item>
|
||
/// </list>
|
||
/// </para>
|
||
/// <para>
|
||
/// 使用示例 / Usage Example:
|
||
/// <code>
|
||
/// // 读取 DB 块字数据 / Read DB block word data
|
||
/// var result = await utility.GetDatasAsync<ushort>("DB1.DBW0", 10);
|
||
/// // 数据类型:SiemensTypeCode.Word (0x03)
|
||
///
|
||
/// // 读取位数据 / Read bit data
|
||
/// var bitResult = await utility.GetDatasAsync<bool>("DB1.DBX0.0", 1);
|
||
/// // 数据类型:SiemensTypeCode.Bool (0x01)
|
||
/// </code>
|
||
/// </para>
|
||
/// </remarks>
|
||
/// </summary>
|
||
public enum SiemensTypeCode : byte
|
||
{
|
||
/// <summary>
|
||
/// 布尔类型 / Boolean Type
|
||
/// <remarks>
|
||
/// 1 位,取值 0 或 1
|
||
/// 1 bit, value 0 or 1
|
||
/// </remarks>
|
||
/// </summary>
|
||
Bool = 0x01,
|
||
|
||
/// <summary>
|
||
/// 字节类型 / Byte Type
|
||
/// <remarks>
|
||
/// 8 位,无符号整数 (0-255)
|
||
/// 8 bits, unsigned integer (0-255)
|
||
/// </remarks>
|
||
/// </summary>
|
||
Byte = 0x02,
|
||
|
||
/// <summary>
|
||
/// 字类型 / Word Type
|
||
/// <remarks>
|
||
/// 16 位,无符号整数 (0-65535)
|
||
/// 16 bits, unsigned integer (0-65535)
|
||
/// </remarks>
|
||
/// </summary>
|
||
Word = 0x03,
|
||
|
||
/// <summary>
|
||
/// 双字类型 / Double Word Type
|
||
/// <remarks>
|
||
/// 32 位,无符号整数 (0-4294967295)
|
||
/// 32 bits, unsigned integer (0-4294967295)
|
||
/// </remarks>
|
||
/// </summary>
|
||
DWord = 0x04,
|
||
|
||
/// <summary>
|
||
/// 计数器 / Counter
|
||
/// <remarks>
|
||
/// S7-200 系列计数器
|
||
/// S7-200 series counter
|
||
/// </remarks>
|
||
/// </summary>
|
||
C = 0x1E,
|
||
|
||
/// <summary>
|
||
/// 计时器 / Timer
|
||
/// <remarks>
|
||
/// S7-200 系列计时器
|
||
/// S7-200 series timer
|
||
/// </remarks>
|
||
/// </summary>
|
||
T = 0x1F,
|
||
|
||
/// <summary>
|
||
/// 高速计数器 / High-Speed Counter
|
||
/// <remarks>
|
||
/// S7-200 系列高速计数器
|
||
/// S7-200 series high-speed counter
|
||
/// </remarks>
|
||
/// </summary>
|
||
HC = 0x20
|
||
}
|
||
|
||
/// <summary>
|
||
/// 西门子通讯访问结果 / Siemens Communication Access Result
|
||
/// <remarks>
|
||
/// 定义西门子 PLC 访问的错误码
|
||
/// Defines error codes for Siemens PLC access
|
||
/// <para>
|
||
/// 错误码说明 / Error Code Description:
|
||
/// <list type="bullet">
|
||
/// <item><strong>0xFF</strong> - 无错误 / No error</item>
|
||
/// <item><strong>0x01</strong> - 硬件错误 / Hardware fault</item>
|
||
/// <item><strong>0x03</strong> - 非法对象访问 / Illegal object access</item>
|
||
/// <item><strong>0x05</strong> - 非法地址访问 / Invalid address</item>
|
||
/// <item><strong>0x06</strong> - 不支持的数据类型 / Data type not supported</item>
|
||
/// <item><strong>0x0A</strong> - 对象不存在或长度错误 / Object not exist or length error</item>
|
||
/// </list>
|
||
/// </para>
|
||
/// </remarks>
|
||
/// </summary>
|
||
public enum SiemensAccessResult : byte
|
||
{
|
||
/// <summary>
|
||
/// 无错误 / No Error
|
||
/// <remarks>
|
||
/// 访问成功
|
||
/// Access successful
|
||
/// </remarks>
|
||
/// </summary>
|
||
NoError = 0xFF,
|
||
|
||
/// <summary>
|
||
/// 硬件错误 / Hardware Fault
|
||
/// <remarks>
|
||
/// PLC 硬件故障
|
||
/// PLC hardware fault
|
||
/// </remarks>
|
||
/// </summary>
|
||
HardwareFault = 0x01,
|
||
|
||
/// <summary>
|
||
/// 非法对象访问(Area 错误) / Illegal Object Access (Area Error)
|
||
/// <remarks>
|
||
/// 访问了不存在的存储区
|
||
/// Accessed non-existent memory area
|
||
/// </remarks>
|
||
/// </summary>
|
||
IllegalObjectAccess = 0x03,
|
||
|
||
/// <summary>
|
||
/// 非法地址访问 / Invalid Address Access
|
||
/// <remarks>
|
||
/// 访问了超出范围的地址
|
||
/// Accessed out-of-range address
|
||
/// </remarks>
|
||
/// </summary>
|
||
InvalidAddress = 0x05,
|
||
|
||
/// <summary>
|
||
/// 不支持的数据类型 / Data Type Not Supported
|
||
/// <remarks>
|
||
/// 使用了 PLC 不支持的数据类型
|
||
/// Used data type not supported by PLC
|
||
/// </remarks>
|
||
/// </summary>
|
||
DataTypeNotSupport = 0x06,
|
||
|
||
/// <summary>
|
||
/// 对象不存在或长度超出允许范围 / Object Not Exist or Length Error
|
||
/// <remarks>
|
||
/// DB 块不存在或读取长度超出范围
|
||
/// DB block not exist or read length out of range
|
||
/// </remarks>
|
||
/// </summary>
|
||
ObjNotExistOrLengthError = 0x0A
|
||
}
|
||
|
||
/// <summary>
|
||
/// 西门子数据访问类型 / Siemens Data Access Type
|
||
/// <remarks>
|
||
/// 定义数据访问的方式(位访问或字节访问)
|
||
/// Defines data access method (bit access or byte access)
|
||
/// </remarks>
|
||
/// </summary>
|
||
public enum SiemensDataType : byte
|
||
{
|
||
/// <summary>
|
||
/// 错误 / Error
|
||
/// <remarks>
|
||
/// 访问错误
|
||
/// Access error
|
||
/// </remarks>
|
||
/// </summary>
|
||
Error = 0x00,
|
||
|
||
/// <summary>
|
||
/// 比特位访问 / Bit Access
|
||
/// <remarks>
|
||
/// 访问单个位 (如 DB1.DBX0.0)
|
||
/// Access single bit (e.g., DB1.DBX0.0)
|
||
/// </remarks>
|
||
/// </summary>
|
||
BitAccess = 0x03,
|
||
|
||
/// <summary>
|
||
/// 一般访问(字节/字/双字) / Other Access (Byte/Word/DWord)
|
||
/// <remarks>
|
||
/// 访问字节、字或双字
|
||
/// Access byte, word or double word
|
||
/// </remarks>
|
||
/// </summary>
|
||
OtherAccess = 0x04
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 西门子协议基类 / Siemens Protocol Base Class
|
||
|
||
/// <summary>
|
||
/// 西门子 S7 协议基类 / Siemens S7 Protocol Base Class
|
||
/// <remarks>
|
||
/// 所有西门子协议实现类的基类
|
||
/// Base class for all Siemens protocol implementation classes
|
||
/// <para>
|
||
/// 主要功能 / Main Functions:
|
||
/// <list type="bullet">
|
||
/// <item>提供协议连接功能 / Provides protocol connection functionality</item>
|
||
/// <item>定义大端格式 (BigEndianLsb) / Defines big-endian format (BigEndianLsb)</item>
|
||
/// <item>派生具体协议类 (TCP/PPI) / Derives specific protocol classes (TCP/PPI)</item>
|
||
/// </list>
|
||
/// </para>
|
||
/// <para>
|
||
/// 派生类 / Derived Classes:
|
||
/// <list type="bullet">
|
||
/// <item><see cref="SiemensTcpProtocol"/> - 以太网协议 / Ethernet protocol</item>
|
||
/// <item><see cref="SiemensPpiProtocol"/> - PPI 串口协议 / PPI serial protocol</item>
|
||
/// </list>
|
||
/// </para>
|
||
/// </remarks>
|
||
/// </summary>
|
||
public abstract class SiemensProtocol : BaseProtocol
|
||
{
|
||
/// <summary>
|
||
/// 构造函数 / Constructor
|
||
/// <remarks>
|
||
/// 初始化西门子协议基类
|
||
/// Initialize Siemens protocol base class
|
||
/// <para>
|
||
/// 端格式 / Endianness:
|
||
/// <list type="bullet">
|
||
/// <item>BigEndianLsb - 大端格式,低有效位在前</item>
|
||
/// <item>Big-endian format, least significant bit first</item>
|
||
/// </list>
|
||
/// </para>
|
||
/// </remarks>
|
||
/// </summary>
|
||
/// <param name="slaveAddress">
|
||
/// 从站号 / Slave Address
|
||
/// <remarks>PLC 的站地址 / PLC station address</remarks>
|
||
/// </param>
|
||
/// <param name="masterAddress">
|
||
/// 主站号 / Master Address
|
||
/// <remarks>PC/上位机的站地址 / PC/HMI station address</remarks>
|
||
/// </param>
|
||
protected SiemensProtocol(byte slaveAddress, byte masterAddress)
|
||
: base(slaveAddress, masterAddress, Endian.BigEndianLsb)
|
||
{
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
// 注意:以下代码被注释掉,是串口连接建立的实现
|
||
// Note: The following code is commented out, it's the implementation for serial connection establishment
|
||
|
||
/*
|
||
#region 串口连接建立 / Serial Connection Establishment
|
||
|
||
internal class ComCreateReferenceSiemensInputStruct : IInputStruct
|
||
{
|
||
public ComCreateReferenceSiemensInputStruct(byte slaveAddress, byte masterAddress)
|
||
{
|
||
SlaveAddress = slaveAddress;
|
||
MasterAddress = masterAddress;
|
||
}
|
||
|
||
public byte SlaveAddress { get; set; }
|
||
public byte MasterAddress { get; set; }
|
||
}
|
||
|
||
internal class ComCreateReferenceSiemensOutputStruct : IOutputStruct
|
||
{
|
||
public ComCreateReferenceSiemensOutputStruct(byte slaveAddress, byte masterAddress, byte confirmMessage)
|
||
{
|
||
SlaveAddress = slaveAddress;
|
||
MasterAddress = masterAddress;
|
||
ConfirmMessage = confirmMessage;
|
||
}
|
||
|
||
public byte SlaveAddress { get; set; }
|
||
public byte MasterAddress { get; set; }
|
||
public byte ConfirmMessage { get; set; }
|
||
}
|
||
|
||
// ... 更多代码 ...
|
||
|
||
#endregion
|
||
*/
|
||
}
|