---
id: adaptersiemenss7
title: 西门子S7协议
---

## 说明

本代码仅适用以下协议。

西门子S7协议

:::tip 提示

封装较多，以下代码只摘录部分，如果需要全部代码，请查看[开源地址](https://gitee.com/diego2098/ThingsGateway)

:::  

## 版权

该代码所有版权归Diego所有，使用时请务必注明。

## 代码

```csharp showLineNumbers


namespace ThingsGateway.Foundation.Adapter.Siemens;

/// <summary>
/// SiemensS7PLCDataHandleAdapter
/// </summary>
public class SiemensS7PLCDataHandleAdapter : ReadWriteDevicesTcpDataHandleAdapter<SiemensMessage>
{
    /// <inheritdoc/>
    public override byte[] PackCommand(byte[] command)
    {
        return command;
    }

    /// <summary>
    /// <inheritdoc/>
    /// </summary>
    /// <returns></returns>
    protected override SiemensMessage GetInstance()
    {
        return new SiemensMessage();
    }

    /// <inheritdoc/>
    protected override FilterResult UnpackResponse(SiemensMessage request, byte[] send, byte[] body, byte[] response)
    {
        var result = new OperResult<byte[]>();
        if (response[2] * 256 + response[3] == 7)
        {
            result = new OperResult<byte[]>(response);
        }
        else
        {
            //以请求方为准，分开返回类型校验
            switch (send[17])
            {
                case 0x04:
                    result = SiemensHelper.AnalysisReadByte(send, response);
                    break;
                case 0x05:
                    result = SiemensHelper.AnalysisWrite(response);
                    break;
            }
        }
        request.ResultCode = result.ResultCode;
        request.Message = result.Message;
        request.Content = result.Content;
        return FilterResult.Success;
    }
}



```
