using HslCommunication.BasicFramework; using HslCommunication.Core; using HslCommunication.Core.Net; using System; using System.Collections.Generic; using System.Linq; using System.Net.Sockets; using System.Text; using HslCommunication.Core.IMessage; using HslCommunication.Core.Address; namespace HslCommunication.Profinet.Siemens { /// /// 西门子S7协议的虚拟服务器,支持TCP协议,无视PLC的型号,所以在客户端进行操作操作的时候,选择1200或是1500或是300或是400都是一样的。 /// /// /// 地址支持的列表如下: /// /// /// 地址名称 /// 地址代号 /// 示例 /// 地址进制 /// 字操作 /// 位操作 /// 备注 /// /// /// 中间寄存器 /// M /// M100,M200 /// 10 /// /// /// /// /// /// 输入寄存器 /// I /// I100,I200 /// 10 /// /// /// /// /// /// 输出寄存器 /// Q /// Q100,Q200 /// 10 /// /// /// /// /// /// DB块寄存器 /// DB /// DB1.100,DB1.200 /// 10 /// /// /// /// /// /// V寄存器 /// V /// V100,V200 /// 10 /// /// /// V寄存器本质就是DB块1 /// /// /// 定时器的值 /// T /// T100,T200 /// 10 /// /// /// 未测试通过 /// /// /// 计数器的值 /// C /// C100,C200 /// 10 /// /// /// 未测试通过 /// /// /// 对于200smartPLC的V区,就是DB1.X,例如,V100=DB1.100 /// /// /// 你可以很快速并且简单的创建一个虚拟的s7服务器 /// /// 当然如果需要高级的服务器,指定日志,限制客户端的IP地址,获取客户端发送的信息,在服务器初始化的时候就要参照下面的代码: /// /// 服务器创建好之后,我们就可以对服务器进行一些读写的操作了,下面的代码是基础的BCL类型的读写操作。 /// /// 高级的对于byte数组类型的数据进行批量化的读写操作如下: /// /// 更高级操作请参见源代码。 /// public class SiemensS7Server : NetworkDataServerBase { #region Constructor /// /// 实例化一个S7协议的服务器,支持I,Q,M,DB1.X 数据区块的读写操作 /// public SiemensS7Server( ) { // 四个数据池初始化,输入寄存器,输出寄存器,中间寄存器,DB块寄存器 inputBuffer = new SoftBuffer( DataPoolLength ); outputBuffer = new SoftBuffer( DataPoolLength ); memeryBuffer = new SoftBuffer( DataPoolLength ); dbBlockBuffer = new SoftBuffer( DataPoolLength ); WordLength = 2; ByteTransform = new ReverseBytesTransform( ); } #endregion #region NetworkDataServerBase Override /// /// 读取自定义的寄存器的值 /// /// 起始地址,示例:"I100","M100" /// 数据长度 /// /// byte数组值 public override OperateResult Read( string address, ushort length ) { OperateResult analysis = S7AddressData.ParseFrom( address, length ); if (!analysis.IsSuccess) return OperateResult.CreateFailedResult( analysis ); switch (analysis.Content.DataCode) { case 0x81: return OperateResult.CreateSuccessResult( inputBuffer.GetBytes( analysis.Content.AddressStart / 8, length ) ); case 0x82: return OperateResult.CreateSuccessResult( outputBuffer.GetBytes( analysis.Content.AddressStart / 8, length ) ); case 0x83: return OperateResult.CreateSuccessResult( memeryBuffer.GetBytes( analysis.Content.AddressStart / 8, length ) ); case 0x84: return OperateResult.CreateSuccessResult( dbBlockBuffer.GetBytes( analysis.Content.AddressStart / 8, length ) ); default: return new OperateResult( StringResources.Language.NotSupportedDataType ); } } /// /// 写入自定义的数据到数据内存中去 /// /// 地址 /// 数据值 /// 是否写入成功的结果对象 public override OperateResult Write( string address, byte[] value ) { OperateResult analysis = S7AddressData.ParseFrom( address ); if (!analysis.IsSuccess) return OperateResult.CreateFailedResult( analysis ); switch (analysis.Content.DataCode) { case 0x81: inputBuffer.SetBytes( value, analysis.Content.AddressStart / 8 ); return OperateResult.CreateSuccessResult( ); case 0x82: outputBuffer.SetBytes(value, analysis.Content.AddressStart / 8 ); return OperateResult.CreateSuccessResult( ); case 0x83: memeryBuffer.SetBytes(value, analysis.Content.AddressStart / 8 ); return OperateResult.CreateSuccessResult( ); case 0x84: dbBlockBuffer.SetBytes( value, analysis.Content.AddressStart / 8 ); return OperateResult.CreateSuccessResult( ); default: return new OperateResult( StringResources.Language.NotSupportedDataType ); } } #endregion #region Byte Read Write Operate /// /// 读取指定地址的字节数据 /// /// 西门子的地址信息 /// 带有成功标志的结果对象 public OperateResult ReadByte(string address ) { OperateResult read = Read( address, 1 ); if (!read.IsSuccess) return OperateResult.CreateFailedResult( read ); return OperateResult.CreateSuccessResult( read.Content[0] ); } /// /// 将byte数据信息写入到指定的地址当中 /// /// 西门子的地址信息 /// 字节数据信息 /// 是否成功的结果 public OperateResult Write(string address, byte value ) { return Write( address, new byte[] { value } ); } #endregion #region Bool Read Write Operate /// /// 读取指定地址的bool数据对象 /// /// 西门子的地址信息 /// 带有成功标志的结果对象 public OperateResult ReadBool(string address ) { OperateResult analysis = S7AddressData.ParseFrom( address ); if (!analysis.IsSuccess) return OperateResult.CreateFailedResult( analysis ); switch (analysis.Content.DataCode) { case 0x81: return OperateResult.CreateSuccessResult( inputBuffer.GetBool( analysis.Content.AddressStart ) ); case 0x82: return OperateResult.CreateSuccessResult( outputBuffer.GetBool( analysis.Content.AddressStart ) ); case 0x83: return OperateResult.CreateSuccessResult( memeryBuffer.GetBool( analysis.Content.AddressStart ) ); case 0x84: return OperateResult.CreateSuccessResult( dbBlockBuffer.GetBool( analysis.Content.AddressStart ) ); default: return new OperateResult( StringResources.Language.NotSupportedDataType ); } } /// /// 往指定的地址里写入bool数据对象 /// /// 西门子的地址信息 /// 值 /// 是否成功的结果 public OperateResult Write(string address, bool value ) { OperateResult analysis = S7AddressData.ParseFrom( address ); if (!analysis.IsSuccess) return analysis; switch (analysis.Content.DataCode) { case 0x81: inputBuffer.SetBool( value, analysis.Content.AddressStart ); return OperateResult.CreateSuccessResult( ); case 0x82: outputBuffer.SetBool( value, analysis.Content.AddressStart ); return OperateResult.CreateSuccessResult( ); case 0x83: memeryBuffer.SetBool( value, analysis.Content.AddressStart ); return OperateResult.CreateSuccessResult( ); case 0x84: dbBlockBuffer.SetBool( value, analysis.Content.AddressStart ); return OperateResult.CreateSuccessResult( ); default: return new OperateResult( StringResources.Language.NotSupportedDataType ); } } #endregion #region NetServer Override /// /// 当客户端登录后,进行Ip信息的过滤,然后触发本方法,也就是说之后的客户端需要 /// /// 网络套接字 /// 终端节点 protected override void ThreadPoolLoginAfterClientCheck( Socket socket, System.Net.IPEndPoint endPoint ) { // 接收2次的握手协议 S7Message s7Message = new S7Message( ); OperateResult read1 = ReceiveByMessage( socket, 5000, s7Message ); if (!read1.IsSuccess) return; OperateResult send1 = Send( socket, SoftBasic.HexStringToBytes( "03 00 00 16 11 D0 00 01 00 0C 00 C0 01 0A C1 02 01 02 C2 02 01 00" ) ); if (!send1.IsSuccess) return; OperateResult read2 = ReceiveByMessage( socket, 5000, s7Message ); if (!read1.IsSuccess) return; OperateResult send2 = Send( socket, SoftBasic.HexStringToBytes( "03 00 00 1B 02 F0 80 32 03 00 00 04 00 00 08 00 00 00 00 F0 00 00 01 00 01 00 F0" ) ); if (!read2.IsSuccess) return; // 开始接收数据信息 AppSession appSession = new AppSession( ); appSession.IpEndPoint = endPoint; appSession.WorkSocket = socket; try { socket.BeginReceive( new byte[0], 0, 0, SocketFlags.None, new AsyncCallback( SocketAsyncCallBack ), appSession ); AddClient( appSession ); } catch { socket.Close( ); LogNet?.WriteDebug( ToString( ), string.Format( StringResources.Language.ClientOfflineInfo, endPoint ) ); } } private void SocketAsyncCallBack( IAsyncResult ar ) { if (ar.AsyncState is AppSession session) { try { int receiveCount = session.WorkSocket.EndReceive( ar ); S7Message s7Message = new S7Message( ); OperateResult read1 = ReceiveByMessage( session.WorkSocket, 5000, s7Message ); if (!read1.IsSuccess) { LogNet?.WriteDebug( ToString( ), string.Format( StringResources.Language.ClientOfflineInfo, session.IpEndPoint ) ); RemoveClient( session ); return; }; byte[] receive = read1.Content; if (receive[17] == 0x04) { // 读数据 session.WorkSocket.Send( ReadByMessage( receive ) ); } else if (receive[17] == 0x05) { // 写数据 session.WorkSocket.Send( WriteByMessage( receive ) ); } else if(receive[17] == 0x00) { // 请求订货号 session.WorkSocket.Send( SoftBasic.HexStringToBytes( "03 00 00 7D 02 F0 80 32 07 00 00 00 01 00 0C 00 60 00 01 12 08 12 84 01 01 00 00 00 00 FF" + " 09 00 5C 00 11 00 00 00 1C 00 03 00 01 36 45 53 37 20 32 31 35 2D 31 41 47 34 30 2D 30 58 42 30 20 00 00 00 06 20 20 00 06 36 45 53 37 20" + " 32 31 35 2D 31 41 47 34 30 2D 30 58 42 30 20 00 00 00 06 20 20 00 07 36 45 53 37 20 32 31 35 2D 31 41 47 34 30 2D 30 58 42 30 20 00 00 56 04 02 01" ) ); } else { session.WorkSocket.Close( ); } RaiseDataReceived( receive ); session.WorkSocket.BeginReceive( new byte[0], 0, 0, SocketFlags.None, new AsyncCallback( SocketAsyncCallBack ), session ); } catch { // 关闭连接,记录日志 session.WorkSocket?.Close( ); LogNet?.WriteDebug( ToString( ), string.Format( StringResources.Language.ClientOfflineInfo, session.IpEndPoint ) ); RemoveClient( session ); return; } } } private byte[] ReadByMessage( byte[] packCommand ) { List content = new List( ); int count = packCommand[18]; int index = 19; for(int i=0; i < count; i++) { byte length = packCommand[index + 1]; byte[] command = ByteTransform.TransByte( packCommand, index, length + 2 ); index += length + 2; content.AddRange( ReadByCommand( command ) ); } byte[] back = new byte[21 + content.Count]; SoftBasic.HexStringToBytes( "03 00 00 1A 02 F0 80 32 03 00 00 00 01 00 02 00 05 00 00 04 01" ).CopyTo( back, 0 ); back[ 2] = (byte)(back.Length / 256); back[ 3] = (byte)(back.Length % 256); back[15] = (byte)(packCommand.Length / 256); back[16] = (byte)(packCommand.Length % 256); back[20] = packCommand[18]; content.CopyTo( back, 21 ); return back; } private byte[] ReadByCommand(byte[] command ) { if(command[3] == 0x01) { // 位读取 int startIndex = command[9] * 65536 + command[10] * 256 + command[11]; switch (command[8]) { case 0x81: return PackReadBitCommandBack( inputBuffer.GetBool( startIndex ) ); case 0x82: return PackReadBitCommandBack( outputBuffer.GetBool( startIndex ) ); case 0x83: return PackReadBitCommandBack( memeryBuffer.GetBool( startIndex ) ); case 0x84: return PackReadBitCommandBack( dbBlockBuffer.GetBool( startIndex ) ); default: throw new Exception( StringResources.Language.NotSupportedDataType ); } } else { // 字读取 ushort length = ByteTransform.TransUInt16( command, 4 ); int startIndex = (command[9] * 65536 + command[10] * 256 + command[11]) / 8; switch (command[8]) { case 0x81: return PackReadWordCommandBack( inputBuffer.GetBytes( startIndex, length ) ); case 0x82: return PackReadWordCommandBack( outputBuffer.GetBytes( startIndex, length ) ); case 0x83: return PackReadWordCommandBack( memeryBuffer.GetBytes( startIndex, length ) ); case 0x84: return PackReadWordCommandBack( dbBlockBuffer.GetBytes( startIndex, length ) ); default: throw new Exception( StringResources.Language.NotSupportedDataType ); } } } private byte[] PackReadWordCommandBack( byte[] result ) { byte[] back = new byte[4 + result.Length]; back[0] = 0xFF; back[1] = 0x04; ByteTransform.TransByte( (ushort)result.Length ).CopyTo( back, 2 ); result.CopyTo( back, 4 ); return back; } private byte[] PackReadBitCommandBack( bool value ) { byte[] back = new byte[5]; back[0] = 0xFF; back[1] = 0x03; back[2] = 0x00; back[3] = 0x01; back[4] = (byte)(value ? 0x01 : 0x00); return back; } private byte[] WriteByMessage( byte[] packCommand ) { if(packCommand[22] == 0x02) { // 字写入 int count = ByteTransform.TransInt16( packCommand, 23 ); int startIndex = (packCommand[28] * 65536 + packCommand[29] * 256 + packCommand[30]) / 8; byte[] data = ByteTransform.TransByte( packCommand, 35, count ); switch (packCommand[27]) { case 0x81: inputBuffer.SetBytes( data, startIndex );break; case 0x82: outputBuffer.SetBytes( data, startIndex ); break; case 0x83: memeryBuffer.SetBytes( data, startIndex ); break; case 0x84: dbBlockBuffer.SetBytes( data, startIndex ); break; default: throw new Exception( StringResources.Language.NotSupportedDataType ); } return SoftBasic.HexStringToBytes( "03 00 00 16 02 F0 80 32 03 00 00 00 01 00 02 00 01 00 00 05 01 FF" ); } else { // 位写入 int startIndex = packCommand[28] * 65536 + packCommand[29] * 256 + packCommand[30]; bool value = packCommand[35] != 0x00; switch (packCommand[27]) { case 0x81: inputBuffer.SetBool( value, startIndex ); break; case 0x82: outputBuffer.SetBool( value, startIndex ); break; case 0x83: memeryBuffer.SetBool( value, startIndex ); break; case 0x84: dbBlockBuffer.SetBool( value, startIndex ); break; default: throw new Exception( StringResources.Language.NotSupportedDataType ); } return SoftBasic.HexStringToBytes( "03 00 00 16 02 F0 80 32 03 00 00 00 01 00 02 00 01 00 00 05 01 FF" ); } } #endregion #region Data Save Load Override /// /// 从字节数据加载数据信息 /// /// 字节数据 protected override void LoadFromBytes( byte[] content ) { if (content.Length < DataPoolLength * 4) throw new Exception( "File is not correct" ); inputBuffer.SetBytes( content, 0, 0, DataPoolLength ); outputBuffer.SetBytes( content, DataPoolLength, 0, DataPoolLength ); memeryBuffer.SetBytes( content, DataPoolLength * 2, 0, DataPoolLength ); dbBlockBuffer.SetBytes( content, DataPoolLength * 3, 0, DataPoolLength ); } /// /// 将数据信息存储到字节数组去 /// /// 所有的内容 protected override byte[] SaveToBytes( ) { byte[] buffer = new byte[DataPoolLength * 4]; Array.Copy( inputBuffer.GetBytes( ), 0, buffer, 0, DataPoolLength ); Array.Copy( outputBuffer.GetBytes( ), 0, buffer, DataPoolLength, DataPoolLength ); Array.Copy( memeryBuffer.GetBytes( ), 0, buffer, DataPoolLength * 2, DataPoolLength ); Array.Copy( dbBlockBuffer.GetBytes( ), 0, buffer, DataPoolLength * 3, DataPoolLength); return buffer; } #endregion #region IDisposable Support /// /// 释放当前的对象 /// /// 是否托管对象 protected override void Dispose( bool disposing ) { if (disposing) { inputBuffer?.Dispose( ); outputBuffer?.Dispose( ); memeryBuffer?.Dispose( ); dbBlockBuffer?.Dispose( ); } base.Dispose( disposing ); } #endregion #region Private Member private SoftBuffer inputBuffer; // 输入寄存器的数据池 private SoftBuffer outputBuffer; // 离散输入的数据池 private SoftBuffer memeryBuffer; // 寄存器的数据池 private SoftBuffer dbBlockBuffer; // 输入寄存器的数据池 private const int DataPoolLength = 65536; // 数据的长度 #endregion #region Object Override /// /// 返回表示当前对象的字符串 /// /// 字符串信息 public override string ToString( ) { return $"SiemensS7Server[{Port}]"; } #endregion } }