s7_peer.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*=============================================================================|
  2. | PROJECT SNAP7 1.3.0 |
  3. |==============================================================================|
  4. | Copyright (C) 2013, 2015 Davide Nardella |
  5. | All rights reserved. |
  6. |==============================================================================|
  7. | SNAP7 is free software: you can redistribute it and/or modify |
  8. | it under the terms of the Lesser GNU General Public License as published by |
  9. | the Free Software Foundation, either version 3 of the License, or |
  10. | (at your option) any later version. |
  11. | |
  12. | It means that you can distribute your commercial software linked with |
  13. | SNAP7 without the requirement to distribute the source code of your |
  14. | application and without the requirement that your application be itself |
  15. | distributed under LGPL. |
  16. | |
  17. | SNAP7 is distributed in the hope that it will be useful, |
  18. | but WITHOUT ANY WARRANTY; without even the implied warranty of |
  19. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
  20. | Lesser GNU General Public License for more details. |
  21. | |
  22. | You should have received a copy of the GNU General Public License and a |
  23. | copy of Lesser GNU General Public License along with Snap7. |
  24. | If not, see http://www.gnu.org/licenses/ |
  25. |=============================================================================*/
  26. #include "s7_peer.h"
  27. //---------------------------------------------------------------------------
  28. TSnap7Peer::TSnap7Peer()
  29. {
  30. PDUH_out=PS7ReqHeader(&PDU.Payload);
  31. PDURequest=480; // Our request, FPDULength will contain the CPU answer
  32. LastError=0;
  33. cntword = 0;
  34. Destroying = false;
  35. }
  36. //---------------------------------------------------------------------------
  37. TSnap7Peer::~TSnap7Peer()
  38. {
  39. Destroying = true;
  40. }
  41. //---------------------------------------------------------------------------
  42. int TSnap7Peer::SetError(int Error)
  43. {
  44. if (Error==0)
  45. ClrError();
  46. else
  47. LastError=Error | LastIsoError | LastTcpError;
  48. return Error;
  49. }
  50. //---------------------------------------------------------------------------
  51. void TSnap7Peer::ClrError()
  52. {
  53. LastError=0;
  54. LastIsoError=0;
  55. LastTcpError=0;
  56. }
  57. //---------------------------------------------------------------------------
  58. word TSnap7Peer::GetNextWord()
  59. {
  60. if (cntword==0xFFFF)
  61. cntword=0;
  62. return cntword++;
  63. }
  64. //---------------------------------------------------------------------------
  65. int TSnap7Peer::NegotiatePDULength( )
  66. {
  67. int Result, IsoSize = 0;
  68. PReqFunNegotiateParams ReqNegotiate;
  69. PResFunNegotiateParams ResNegotiate;
  70. PS7ResHeader23 Answer;
  71. ClrError();
  72. // Setup Pointers
  73. ReqNegotiate = PReqFunNegotiateParams(pbyte(PDUH_out) + sizeof(TS7ReqHeader));
  74. // Header
  75. PDUH_out->P = 0x32; // Always $32
  76. PDUH_out->PDUType = PduType_request; // $01
  77. PDUH_out->AB_EX = 0x0000; // Always $0000
  78. PDUH_out->Sequence = GetNextWord(); // AutoInc
  79. PDUH_out->ParLen = SwapWord(sizeof(TReqFunNegotiateParams)); // 8 bytes
  80. PDUH_out->DataLen = 0x0000;
  81. // Params
  82. ReqNegotiate->FunNegotiate = pduNegotiate;
  83. ReqNegotiate->Unknown = 0x00;
  84. ReqNegotiate->ParallelJobs_1 = 0x0100;
  85. ReqNegotiate->ParallelJobs_2 = 0x0100;
  86. ReqNegotiate->PDULength = SwapWord(PDURequest);
  87. IsoSize = sizeof( TS7ReqHeader ) + sizeof( TReqFunNegotiateParams );
  88. Result = isoExchangeBuffer(NULL, IsoSize);
  89. if ((Result == 0) && (IsoSize == int(sizeof(TS7ResHeader23) + sizeof(TResFunNegotiateParams))))
  90. {
  91. // Setup pointers
  92. Answer = PS7ResHeader23(&PDU.Payload);
  93. ResNegotiate = PResFunNegotiateParams(pbyte(Answer) + sizeof(TS7ResHeader23));
  94. if ( Answer->Error != 0 )
  95. Result = SetError(errNegotiatingPDU);
  96. if ( Result == 0 )
  97. PDULength = SwapWord(ResNegotiate->PDULength);
  98. }
  99. return Result;
  100. }
  101. //---------------------------------------------------------------------------
  102. void TSnap7Peer::PeerDisconnect( )
  103. {
  104. ClrError();
  105. isoDisconnect(true);
  106. }
  107. //---------------------------------------------------------------------------
  108. int TSnap7Peer::PeerConnect( )
  109. {
  110. int Result;
  111. ClrError();
  112. Result = isoConnect();
  113. if (Result == 0)
  114. {
  115. Result = NegotiatePDULength();
  116. if (Result != 0)
  117. PeerDisconnect();
  118. }
  119. return Result;
  120. }