1 /*=============================================================================| 2 | PROJECT SNAP7 1.4.0 | 3 |==============================================================================| 4 | Copyright (C) 2013, 2014 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 | | 27 | C/C++ Snap 7 classes/Imports definitions | 28 | | 29 |=============================================================================*/ 30 #ifndef snap7_h 31 #define snap7_h 32 //--------------------------------------------------------------------------- 33 // Platform detection 34 //--------------------------------------------------------------------------- 35 #if defined (_WIN32)||defined(_WIN64)||defined(__WIN32__)||defined(__WINDOWS__) 36 # define OS_WINDOWS 37 #endif 38 39 // Visual Studio needs this to use the correct time_t size 40 #if defined (_WIN32) && !defined(_WIN64) 41 # define _USE_32BIT_TIME_T 42 #endif 43 44 #if defined(unix) || defined(__unix__) || defined(__unix) 45 # define PLATFORM_UNIX 46 #endif 47 48 #if defined(__SVR4) || defined(__svr4__) 49 # define OS_SOLARIS 50 #endif 51 52 #if BSD>=0 53 # define OS_BSD 54 #endif 55 56 #if defined(__APPLE__) 57 # define OS_OSX 58 #endif 59 60 #if defined(PLATFORM_UNIX) || defined(OS_OSX) 61 # include <unistd.h> 62 # if defined(_POSIX_VERSION) 63 # define POSIX 64 # endif 65 #endif 66 67 //--------------------------------------------------------------------------- 68 // C++ Library 69 //--------------------------------------------------------------------------- 70 #ifdef __cplusplus 71 #include <string> 72 #include <time.h> 73 74 // Visual C++ not C99 compliant (VS2008--) 75 #ifdef _MSC_VER 76 # if _MSC_VER >= 1600 77 # include <stdint.h> // VS2010++ have it 78 # else 79 typedef signed __int8 int8_t; 80 typedef signed __int16 int16_t; 81 typedef signed __int32 int32_t; 82 typedef signed __int64 int64_t; 83 typedef unsigned __int8 uint8_t; 84 typedef unsigned __int16 uint16_t; 85 typedef unsigned __int32 uint32_t; 86 typedef unsigned __int64 uint64_t; 87 #ifdef _WIN64 88 typedef unsigned __int64 uintptr_t; 89 #else 90 typedef unsigned __int32 uintptr_t; 91 #endif 92 # endif 93 #else 94 # include <stdint.h> 95 #endif 96 97 extern "C" { 98 #endif 99 //--------------------------------------------------------------------------- 100 // C exact length types 101 //--------------------------------------------------------------------------- 102 #ifndef __cplusplus 103 104 #ifdef OS_BSD 105 # include <stdint.h> 106 # include <time.h> 107 #endif 108 109 #ifdef OS_OSX 110 # include <stdint.h> 111 # include <time.h> 112 #endif 113 114 #ifdef OS_SOLARIS 115 # include <stdint.h> 116 # include <time.h> 117 #endif 118 119 #if defined(_UINTPTR_T_DEFINED) 120 # include <stdint.h> 121 # include <time.h> 122 #endif 123 124 #if !defined(_UINTPTR_T_DEFINED) && !defined(OS_SOLARIS) && !defined(OS_BSD) && !defined(OS_OSX) 125 typedef unsigned char uint8_t; // 8 bit unsigned integer 126 typedef unsigned short uint16_t; // 16 bit unsigned integer 127 typedef unsigned int uint32_t; // 32 bit unsigned integer 128 typedef unsigned long uintptr_t;// 64 bit unsigned integer 129 #endif 130 131 #endif 132 133 #ifdef OS_WINDOWS 134 # define S7API __stdcall 135 #else 136 # define S7API 137 #endif 138 139 #pragma pack(1) 140 //****************************************************************************** 141 // COMMON 142 //****************************************************************************** 143 // Exact length types regardless of platform/processor 144 typedef uint8_t byte; 145 typedef uint16_t word; 146 typedef uint32_t longword; 147 typedef byte *pbyte; 148 typedef word *pword; 149 typedef uintptr_t S7Object; // multi platform/processor object reference 150 // DON'T CONFUSE IT WITH AN OLE OBJECT, IT'S SIMPLY 151 // AN INTEGER VALUE (32 OR 64 BIT) USED AS HANDLE. 152 153 #ifndef __cplusplus 154 typedef struct 155 { 156 int tm_sec; 157 int tm_min; 158 int tm_hour; 159 int tm_mday; 160 int tm_mon; 161 int tm_year; 162 int tm_wday; 163 int tm_yday; 164 int tm_isdst; 165 }tm; 166 167 typedef int bool; 168 #define false 0; 169 #define true 1; 170 #endif 171 172 const int errLibInvalidParam = -1; 173 const int errLibInvalidObject = -2; 174 175 // CPU status 176 #define S7CpuStatusUnknown 0x00 177 #define S7CpuStatusRun 0x08 178 #define S7CpuStatusStop 0x04 179 180 // ISO Errors 181 const longword errIsoConnect = 0x00010000; // Connection error 182 const longword errIsoDisconnect = 0x00020000; // Disconnect error 183 const longword errIsoInvalidPDU = 0x00030000; // Bad format 184 const longword errIsoInvalidDataSize = 0x00040000; // Bad Datasize passed to send/recv buffer is invalid 185 const longword errIsoNullPointer = 0x00050000; // Null passed as pointer 186 const longword errIsoShortPacket = 0x00060000; // A short packet received 187 const longword errIsoTooManyFragments = 0x00070000; // Too many packets without EoT flag 188 const longword errIsoPduOverflow = 0x00080000; // The sum of fragments data exceded maximum packet size 189 const longword errIsoSendPacket = 0x00090000; // An error occurred during send 190 const longword errIsoRecvPacket = 0x000A0000; // An error occurred during recv 191 const longword errIsoInvalidParams = 0x000B0000; // Invalid TSAP params 192 const longword errIsoResvd_1 = 0x000C0000; // Unassigned 193 const longword errIsoResvd_2 = 0x000D0000; // Unassigned 194 const longword errIsoResvd_3 = 0x000E0000; // Unassigned 195 const longword errIsoResvd_4 = 0x000F0000; // Unassigned 196 197 // Tag Struct 198 typedef struct{ 199 int Area; 200 int DBNumber; 201 int Start; 202 int Size; 203 int WordLen; 204 }TS7Tag, *PS7Tag; 205 206 //------------------------------------------------------------------------------ 207 // PARAMS LIST 208 //------------------------------------------------------------------------------ 209 const int p_u16_LocalPort = 1; 210 const int p_u16_RemotePort = 2; 211 const int p_i32_PingTimeout = 3; 212 const int p_i32_SendTimeout = 4; 213 const int p_i32_RecvTimeout = 5; 214 const int p_i32_WorkInterval = 6; 215 const int p_u16_SrcRef = 7; 216 const int p_u16_DstRef = 8; 217 const int p_u16_SrcTSap = 9; 218 const int p_i32_PDURequest = 10; 219 const int p_i32_MaxClients = 11; 220 const int p_i32_BSendTimeout = 12; 221 const int p_i32_BRecvTimeout = 13; 222 const int p_u32_RecoveryTime = 14; 223 const int p_u32_KeepAliveTime = 15; 224 225 // Client/Partner Job status 226 const int JobComplete = 0; 227 const int JobPending = 1; 228 229 //****************************************************************************** 230 // CLIENT 231 //****************************************************************************** 232 233 // Error codes 234 const longword errNegotiatingPDU = 0x00100000; 235 const longword errCliInvalidParams = 0x00200000; 236 const longword errCliJobPending = 0x00300000; 237 const longword errCliTooManyItems = 0x00400000; 238 const longword errCliInvalidWordLen = 0x00500000; 239 const longword errCliPartialDataWritten = 0x00600000; 240 const longword errCliSizeOverPDU = 0x00700000; 241 const longword errCliInvalidPlcAnswer = 0x00800000; 242 const longword errCliAddressOutOfRange = 0x00900000; 243 const longword errCliInvalidTransportSize = 0x00A00000; 244 const longword errCliWriteDataSizeMismatch = 0x00B00000; 245 const longword errCliItemNotAvailable = 0x00C00000; 246 const longword errCliInvalidValue = 0x00D00000; 247 const longword errCliCannotStartPLC = 0x00E00000; 248 const longword errCliAlreadyRun = 0x00F00000; 249 const longword errCliCannotStopPLC = 0x01000000; 250 const longword errCliCannotCopyRamToRom = 0x01100000; 251 const longword errCliCannotCompress = 0x01200000; 252 const longword errCliAlreadyStop = 0x01300000; 253 const longword errCliFunNotAvailable = 0x01400000; 254 const longword errCliUploadSequenceFailed = 0x01500000; 255 const longword errCliInvalidDataSizeRecvd = 0x01600000; 256 const longword errCliInvalidBlockType = 0x01700000; 257 const longword errCliInvalidBlockNumber = 0x01800000; 258 const longword errCliInvalidBlockSize = 0x01900000; 259 const longword errCliDownloadSequenceFailed = 0x01A00000; 260 const longword errCliInsertRefused = 0x01B00000; 261 const longword errCliDeleteRefused = 0x01C00000; 262 const longword errCliNeedPassword = 0x01D00000; 263 const longword errCliInvalidPassword = 0x01E00000; 264 const longword errCliNoPasswordToSetOrClear = 0x01F00000; 265 const longword errCliJobTimeout = 0x02000000; 266 const longword errCliPartialDataRead = 0x02100000; 267 const longword errCliBufferTooSmall = 0x02200000; 268 const longword errCliFunctionRefused = 0x02300000; 269 const longword errCliDestroying = 0x02400000; 270 const longword errCliInvalidParamNumber = 0x02500000; 271 const longword errCliCannotChangeParam = 0x02600000; 272 273 const int MaxVars = 20; // Max vars that can be transferred with MultiRead/MultiWrite 274 275 // Client Connection Type 276 const word CONNTYPE_PG = 0x0001; // Connect to the PLC as a PG 277 const word CONNTYPE_OP = 0x0002; // Connect to the PLC as an OP 278 const word CONNTYPE_BASIC = 0x0003; // Basic connection 279 280 // Area ID 281 const byte S7AreaPE = 0x81; 282 const byte S7AreaPA = 0x82; 283 const byte S7AreaMK = 0x83; 284 const byte S7AreaDB = 0x84; 285 const byte S7AreaCT = 0x1C; 286 const byte S7AreaTM = 0x1D; 287 288 // Word Length 289 const int S7WLBit = 0x01; 290 const int S7WLByte = 0x02; 291 const int S7WLWord = 0x04; 292 const int S7WLDWord = 0x06; 293 const int S7WLReal = 0x08; 294 const int S7WLCounter = 0x1C; 295 const int S7WLTimer = 0x1D; 296 297 // Block type 298 const byte Block_OB = 0x38; 299 const byte Block_DB = 0x41; 300 const byte Block_SDB = 0x42; 301 const byte Block_FC = 0x43; 302 const byte Block_SFC = 0x44; 303 const byte Block_FB = 0x45; 304 const byte Block_SFB = 0x46; 305 306 // Sub Block Type 307 const byte SubBlk_OB = 0x08; 308 const byte SubBlk_DB = 0x0A; 309 const byte SubBlk_SDB = 0x0B; 310 const byte SubBlk_FC = 0x0C; 311 const byte SubBlk_SFC = 0x0D; 312 const byte SubBlk_FB = 0x0E; 313 const byte SubBlk_SFB = 0x0F; 314 315 // Block languages 316 const byte BlockLangAWL = 0x01; 317 const byte BlockLangKOP = 0x02; 318 const byte BlockLangFUP = 0x03; 319 const byte BlockLangSCL = 0x04; 320 const byte BlockLangDB = 0x05; 321 const byte BlockLangGRAPH = 0x06; 322 323 // Read/Write Multivars 324 typedef struct{ 325 int Area; 326 int WordLen; 327 int Result; 328 int DBNumber; 329 int Start; 330 int Amount; 331 void *pdata; 332 } TS7DataItem, *PS7DataItem; 333 334 //typedef int TS7ResultItems[MaxVars]; 335 //typedef TS7ResultItems *PS7ResultItems; 336 337 // List Blocks 338 typedef struct { 339 int OBCount; 340 int FBCount; 341 int FCCount; 342 int SFBCount; 343 int SFCCount; 344 int DBCount; 345 int SDBCount; 346 } TS7BlocksList, *PS7BlocksList; 347 348 // Blocks info 349 typedef struct { 350 int BlkType; // Block Type (OB, DB) 351 int BlkNumber; // Block number 352 int BlkLang; // Block Language 353 int BlkFlags; // Block flags 354 int MC7Size; // The real size in bytes 355 int LoadSize; // Load memory size 356 int LocalData; // Local data 357 int SBBLength; // SBB Length 358 int CheckSum; // Checksum 359 int Version; // Block version 360 // Chars info 361 char CodeDate[11]; // Code date 362 char IntfDate[11]; // Interface date 363 char Author[9]; // Author 364 char Family[9]; // Family 365 char Header[9]; // Header 366 } TS7BlockInfo, *PS7BlockInfo ; 367 368 typedef word TS7BlocksOfType[0x2000]; 369 typedef TS7BlocksOfType *PS7BlocksOfType; 370 371 // Order code 372 typedef struct { 373 char Code[21]; 374 byte V1; 375 byte V2; 376 byte V3; 377 } TS7OrderCode, *PS7OrderCode; 378 379 // CPU Info 380 typedef struct { 381 char ModuleTypeName[33]; 382 char SerialNumber[25]; 383 char ASName[25]; 384 char Copyright[27]; 385 char ModuleName[25]; 386 } TS7CpuInfo, *PS7CpuInfo; 387 388 // CP Info 389 typedef struct { 390 int MaxPduLengt; 391 int MaxConnections; 392 int MaxMpiRate; 393 int MaxBusRate; 394 } TS7CpInfo, *PS7CpInfo; 395 396 // See §33.1 of "System Software for S7-300/400 System and Standard Functions" 397 // and see SFC51 description too 398 typedef struct { 399 word LENTHDR; 400 word N_DR; 401 } SZL_HEADER, *PSZL_HEADER; 402 403 typedef struct { 404 SZL_HEADER Header; 405 byte Data[0x4000-4]; 406 } TS7SZL, *PS7SZL; 407 408 // SZL List of available SZL IDs : same as SZL but List items are big-endian adjusted 409 typedef struct { 410 SZL_HEADER Header; 411 word List[0x2000-2]; 412 } TS7SZLList, *PS7SZLList; 413 414 // See §33.19 of "System Software for S7-300/400 System and Standard Functions" 415 typedef struct { 416 word sch_schal; 417 word sch_par; 418 word sch_rel; 419 word bart_sch; 420 word anl_sch; 421 } TS7Protection, *PS7Protection; 422 423 // Client completion callback 424 typedef void (S7API *pfn_CliCompletion) (void *usrPtr, int opCode, int opResult); 425 //------------------------------------------------------------------------------ 426 // Import prototypes 427 //------------------------------------------------------------------------------ 428 S7Object S7API Cli_Create(); 429 void S7API Cli_Destroy(S7Object *Client); 430 int S7API Cli_ConnectTo(S7Object Client, const char *Address, int Rack, int Slot); 431 int S7API Cli_SetConnectionParams(S7Object Client, const char *Address, word LocalTSAP, word RemoteTSAP); 432 int S7API Cli_SetConnectionType(S7Object Client, word ConnectionType); 433 int S7API Cli_Connect(S7Object Client); 434 int S7API Cli_Disconnect(S7Object Client); 435 int S7API Cli_GetParam(S7Object Client, int ParamNumber, void *pValue); 436 int S7API Cli_SetParam(S7Object Client, int ParamNumber, void *pValue); 437 int S7API Cli_SetAsCallback(S7Object Client, pfn_CliCompletion pCompletion, void *usrPtr); 438 // Data I/O main functions 439 int S7API Cli_ReadArea(S7Object Client, int Area, int DBNumber, int Start, int Amount, int WordLen, void *pUsrData); 440 int S7API Cli_WriteArea(S7Object Client, int Area, int DBNumber, int Start, int Amount, int WordLen, void *pUsrData); 441 int S7API Cli_ReadMultiVars(S7Object Client, PS7DataItem Item, int ItemsCount); 442 int S7API Cli_WriteMultiVars(S7Object Client, PS7DataItem Item, int ItemsCount); 443 // Data I/O Lean functions 444 int S7API Cli_DBRead(S7Object Client, int DBNumber, int Start, int Size, void *pUsrData); 445 int S7API Cli_DBWrite(S7Object Client, int DBNumber, int Start, int Size, void *pUsrData); 446 int S7API Cli_MBRead(S7Object Client, int Start, int Size, void *pUsrData); 447 int S7API Cli_MBWrite(S7Object Client, int Start, int Size, void *pUsrData); 448 int S7API Cli_EBRead(S7Object Client, int Start, int Size, void *pUsrData); 449 int S7API Cli_EBWrite(S7Object Client, int Start, int Size, void *pUsrData); 450 int S7API Cli_ABRead(S7Object Client, int Start, int Size, void *pUsrData); 451 int S7API Cli_ABWrite(S7Object Client, int Start, int Size, void *pUsrData); 452 int S7API Cli_TMRead(S7Object Client, int Start, int Amount, void *pUsrData); 453 int S7API Cli_TMWrite(S7Object Client, int Start, int Amount, void *pUsrData); 454 int S7API Cli_CTRead(S7Object Client, int Start, int Amount, void *pUsrData); 455 int S7API Cli_CTWrite(S7Object Client, int Start, int Amount, void *pUsrData); 456 // Directory functions 457 int S7API Cli_ListBlocks(S7Object Client, TS7BlocksList *pUsrData); 458 int S7API Cli_GetAgBlockInfo(S7Object Client, int BlockType, int BlockNum, TS7BlockInfo *pUsrData); 459 int S7API Cli_GetPgBlockInfo(S7Object Client, void *pBlock, TS7BlockInfo *pUsrData, int Size); 460 int S7API Cli_ListBlocksOfType(S7Object Client, int BlockType, TS7BlocksOfType *pUsrData, int *ItemsCount); 461 // Blocks functions 462 int S7API Cli_Upload(S7Object Client, int BlockType, int BlockNum, void *pUsrData, int *Size); 463 int S7API Cli_FullUpload(S7Object Client, int BlockType, int BlockNum, void *pUsrData, int *Size); 464 int S7API Cli_Download(S7Object Client, int BlockNum, void *pUsrData, int Size); 465 int S7API Cli_Delete(S7Object Client, int BlockType, int BlockNum); 466 int S7API Cli_DBGet(S7Object Client, int DBNumber, void *pUsrData, int *Size); 467 int S7API Cli_DBFill(S7Object Client, int DBNumber, int FillChar); 468 // Date/Time functions 469 int S7API Cli_GetPlcDateTime(S7Object Client, tm *DateTime); 470 int S7API Cli_SetPlcDateTime(S7Object Client, tm *DateTime); 471 int S7API Cli_SetPlcSystemDateTime(S7Object Client); 472 // System Info functions 473 int S7API Cli_GetOrderCode(S7Object Client, TS7OrderCode *pUsrData); 474 int S7API Cli_GetCpuInfo(S7Object Client, TS7CpuInfo *pUsrData); 475 int S7API Cli_GetCpInfo(S7Object Client, TS7CpInfo *pUsrData); 476 int S7API Cli_ReadSZL(S7Object Client, int ID, int Index, TS7SZL *pUsrData, int *Size); 477 int S7API Cli_ReadSZLList(S7Object Client, TS7SZLList *pUsrData, int *ItemsCount); 478 // Control functions 479 int S7API Cli_PlcHotStart(S7Object Client); 480 int S7API Cli_PlcColdStart(S7Object Client); 481 int S7API Cli_PlcStop(S7Object Client); 482 int S7API Cli_CopyRamToRom(S7Object Client, int Timeout); 483 int S7API Cli_Compress(S7Object Client, int Timeout); 484 int S7API Cli_GetPlcStatus(S7Object Client, int *Status); 485 // Security functions 486 int S7API Cli_GetProtection(S7Object Client, TS7Protection *pUsrData); 487 int S7API Cli_SetSessionPassword(S7Object Client, char *Password); 488 int S7API Cli_ClearSessionPassword(S7Object Client); 489 // Low level 490 int S7API Cli_IsoExchangeBuffer(S7Object Client, void *pUsrData, int *Size); 491 // Misc 492 int S7API Cli_GetExecTime(S7Object Client, int *Time); 493 int S7API Cli_GetLastError(S7Object Client, int *LastError); 494 int S7API Cli_GetPduLength(S7Object Client, int *Requested, int *Negotiated); 495 int S7API Cli_ErrorText(int Error, char *Text, int TextLen); 496 // 1.1.0 497 int S7API Cli_GetConnected(S7Object Client, int *Connected); 498 //------------------------------------------------------------------------------ 499 // Async functions 500 //------------------------------------------------------------------------------ 501 int S7API Cli_AsReadArea(S7Object Client, int Area, int DBNumber, int Start, int Amount, int WordLen, void *pUsrData); 502 int S7API Cli_AsWriteArea(S7Object Client, int Area, int DBNumber, int Start, int Amount, int WordLen, void *pUsrData); 503 int S7API Cli_AsDBRead(S7Object Client, int DBNumber, int Start, int Size, void *pUsrData); 504 int S7API Cli_AsDBWrite(S7Object Client, int DBNumber, int Start, int Size, void *pUsrData); 505 int S7API Cli_AsMBRead(S7Object Client, int Start, int Size, void *pUsrData); 506 int S7API Cli_AsMBWrite(S7Object Client, int Start, int Size, void *pUsrData); 507 int S7API Cli_AsEBRead(S7Object Client, int Start, int Size, void *pUsrData); 508 int S7API Cli_AsEBWrite(S7Object Client, int Start, int Size, void *pUsrData); 509 int S7API Cli_AsABRead(S7Object Client, int Start, int Size, void *pUsrData); 510 int S7API Cli_AsABWrite(S7Object Client, int Start, int Size, void *pUsrData); 511 int S7API Cli_AsTMRead(S7Object Client, int Start, int Amount, void *pUsrData); 512 int S7API Cli_AsTMWrite(S7Object Client, int Start, int Amount, void *pUsrData); 513 int S7API Cli_AsCTRead(S7Object Client, int Start, int Amount, void *pUsrData); 514 int S7API Cli_AsCTWrite(S7Object Client, int Start, int Amount, void *pUsrData); 515 int S7API Cli_AsListBlocksOfType(S7Object Client, int BlockType, TS7BlocksOfType *pUsrData, int *ItemsCount); 516 int S7API Cli_AsReadSZL(S7Object Client, int ID, int Index, TS7SZL *pUsrData, int *Size); 517 int S7API Cli_AsReadSZLList(S7Object Client, TS7SZLList *pUsrData, int *ItemsCount); 518 int S7API Cli_AsUpload(S7Object Client, int BlockType, int BlockNum, void *pUsrData, int *Size); 519 int S7API Cli_AsFullUpload(S7Object Client, int BlockType, int BlockNum, void *pUsrData, int *Size); 520 int S7API Cli_AsDownload(S7Object Client, int BlockNum, void *pUsrData, int Size); 521 int S7API Cli_AsCopyRamToRom(S7Object Client, int Timeout); 522 int S7API Cli_AsCompress(S7Object Client, int Timeout); 523 int S7API Cli_AsDBGet(S7Object Client, int DBNumber, void *pUsrData, int *Size); 524 int S7API Cli_AsDBFill(S7Object Client, int DBNumber, int FillChar); 525 int S7API Cli_CheckAsCompletion(S7Object Client, int *opResult); 526 int S7API Cli_WaitAsCompletion(S7Object Client, int Timeout); 527 528 //****************************************************************************** 529 // SERVER 530 //****************************************************************************** 531 const int OperationRead = 0; 532 const int OperationWrite = 1; 533 534 const int mkEvent = 0; 535 const int mkLog = 1; 536 537 // Server Area ID (use with Register/unregister - Lock/unlock Area) 538 const int srvAreaPE = 0; 539 const int srvAreaPA = 1; 540 const int srvAreaMK = 2; 541 const int srvAreaCT = 3; 542 const int srvAreaTM = 4; 543 const int srvAreaDB = 5; 544 545 // Errors 546 const longword errSrvCannotStart = 0x00100000; // Server cannot start 547 const longword errSrvDBNullPointer = 0x00200000; // Passed null as PData 548 const longword errSrvAreaAlreadyExists = 0x00300000; // Area Re-registration 549 const longword errSrvUnknownArea = 0x00400000; // Unknown area 550 const longword errSrvInvalidParams = 0x00500000; // Invalid param(s) supplied 551 const longword errSrvTooManyDB = 0x00600000; // Cannot register DB 552 const longword errSrvInvalidParamNumber = 0x00700000; // Invalid param (srv_get/set_param) 553 const longword errSrvCannotChangeParam = 0x00800000; // Cannot change because running 554 555 // TCP Server Event codes 556 const longword evcServerStarted = 0x00000001; 557 const longword evcServerStopped = 0x00000002; 558 const longword evcListenerCannotStart = 0x00000004; 559 const longword evcClientAdded = 0x00000008; 560 const longword evcClientRejected = 0x00000010; 561 const longword evcClientNoRoom = 0x00000020; 562 const longword evcClientException = 0x00000040; 563 const longword evcClientDisconnected = 0x00000080; 564 const longword evcClientTerminated = 0x00000100; 565 const longword evcClientsDropped = 0x00000200; 566 const longword evcReserved_00000400 = 0x00000400; // actually unused 567 const longword evcReserved_00000800 = 0x00000800; // actually unused 568 const longword evcReserved_00001000 = 0x00001000; // actually unused 569 const longword evcReserved_00002000 = 0x00002000; // actually unused 570 const longword evcReserved_00004000 = 0x00004000; // actually unused 571 const longword evcReserved_00008000 = 0x00008000; // actually unused 572 // S7 Server Event Code 573 const longword evcPDUincoming = 0x00010000; 574 const longword evcDataRead = 0x00020000; 575 const longword evcDataWrite = 0x00040000; 576 const longword evcNegotiatePDU = 0x00080000; 577 const longword evcReadSZL = 0x00100000; 578 const longword evcClock = 0x00200000; 579 const longword evcUpload = 0x00400000; 580 const longword evcDownload = 0x00800000; 581 const longword evcDirectory = 0x01000000; 582 const longword evcSecurity = 0x02000000; 583 const longword evcControl = 0x04000000; 584 const longword evcReserved_08000000 = 0x08000000; // actually unused 585 const longword evcReserved_10000000 = 0x10000000; // actually unused 586 const longword evcReserved_20000000 = 0x20000000; // actually unused 587 const longword evcReserved_40000000 = 0x40000000; // actually unused 588 const longword evcReserved_80000000 = 0x80000000; // actually unused 589 // Masks to enable/disable all events 590 const longword evcAll = 0xFFFFFFFF; 591 const longword evcNone = 0x00000000; 592 // Event SubCodes 593 const word evsUnknown = 0x0000; 594 const word evsStartUpload = 0x0001; 595 const word evsStartDownload = 0x0001; 596 const word evsGetBlockList = 0x0001; 597 const word evsStartListBoT = 0x0002; 598 const word evsListBoT = 0x0003; 599 const word evsGetBlockInfo = 0x0004; 600 const word evsGetClock = 0x0001; 601 const word evsSetClock = 0x0002; 602 const word evsSetPassword = 0x0001; 603 const word evsClrPassword = 0x0002; 604 // Event Params : functions group 605 const word grProgrammer = 0x0041; 606 const word grCyclicData = 0x0042; 607 const word grBlocksInfo = 0x0043; 608 const word grSZL = 0x0044; 609 const word grPassword = 0x0045; 610 const word grBSend = 0x0046; 611 const word grClock = 0x0047; 612 const word grSecurity = 0x0045; 613 // Event Params : control codes 614 const word CodeControlUnknown = 0x0000; 615 const word CodeControlColdStart = 0x0001; 616 const word CodeControlWarmStart = 0x0002; 617 const word CodeControlStop = 0x0003; 618 const word CodeControlCompress = 0x0004; 619 const word CodeControlCpyRamRom = 0x0005; 620 const word CodeControlInsDel = 0x0006; 621 // Event Result 622 const word evrNoError = 0x0000; 623 const word evrFragmentRejected = 0x0001; 624 const word evrMalformedPDU = 0x0002; 625 const word evrSparseBytes = 0x0003; 626 const word evrCannotHandlePDU = 0x0004; 627 const word evrNotImplemented = 0x0005; 628 const word evrErrException = 0x0006; 629 const word evrErrAreaNotFound = 0x0007; 630 const word evrErrOutOfRange = 0x0008; 631 const word evrErrOverPDU = 0x0009; 632 const word evrErrTransportSize = 0x000A; 633 const word evrInvalidGroupUData = 0x000B; 634 const word evrInvalidSZL = 0x000C; 635 const word evrDataSizeMismatch = 0x000D; 636 const word evrCannotUpload = 0x000E; 637 const word evrCannotDownload = 0x000F; 638 const word evrUploadInvalidID = 0x0010; 639 const word evrResNotFound = 0x0011; 640 641 typedef struct{ 642 time_t EvtTime; // Timestamp 643 int EvtSender; // Sender 644 longword EvtCode; // Event code 645 word EvtRetCode; // Event result 646 word EvtParam1; // Param 1 (if available) 647 word EvtParam2; // Param 2 (if available) 648 word EvtParam3; // Param 3 (if available) 649 word EvtParam4; // Param 4 (if available) 650 }TSrvEvent, *PSrvEvent; 651 652 // Server Events callback 653 typedef void (S7API *pfn_SrvCallBack)(void *usrPtr, PSrvEvent PEvent, int Size); 654 // Server Read/Write callback 655 typedef int(S7API *pfn_RWAreaCallBack)(void *usrPtr, int Sender, int Operation, PS7Tag PTag, void *pUsrData); 656 657 S7Object S7API Srv_Create(); 658 void S7API Srv_Destroy(S7Object *Server); 659 int S7API Srv_GetParam(S7Object Server, int ParamNumber, void *pValue); 660 int S7API Srv_SetParam(S7Object Server, int ParamNumber, void *pValue); 661 int S7API Srv_StartTo(S7Object Server, const char *Address); 662 int S7API Srv_Start(S7Object Server); 663 int S7API Srv_Stop(S7Object Server); 664 int S7API Srv_RegisterArea(S7Object Server, int AreaCode, word Index, void *pUsrData, int Size); 665 int S7API Srv_UnregisterArea(S7Object Server, int AreaCode, word Index); 666 int S7API Srv_LockArea(S7Object Server, int AreaCode, word Index); 667 int S7API Srv_UnlockArea(S7Object Server, int AreaCode, word Index); 668 int S7API Srv_GetStatus(S7Object Server, int *ServerStatus, int *CpuStatus, int *ClientsCount); 669 int S7API Srv_SetCpuStatus(S7Object Server, int CpuStatus); 670 int S7API Srv_ClearEvents(S7Object Server); 671 int S7API Srv_PickEvent(S7Object Server, TSrvEvent *pEvent, int *EvtReady); 672 int S7API Srv_GetMask(S7Object Server, int MaskKind, longword *Mask); 673 int S7API Srv_SetMask(S7Object Server, int MaskKind, longword Mask); 674 int S7API Srv_SetEventsCallback(S7Object Server, pfn_SrvCallBack pCallback, void *usrPtr); 675 int S7API Srv_SetReadEventsCallback(S7Object Server, pfn_SrvCallBack pCallback, void *usrPtr); 676 int S7API Srv_SetRWAreaCallback(S7Object Server, pfn_RWAreaCallBack pCallback, void *usrPtr); 677 int S7API Srv_EventText(TSrvEvent *Event, char *Text, int TextLen); 678 int S7API Srv_ErrorText(int Error, char *Text, int TextLen); 679 680 //****************************************************************************** 681 // PARTNER 682 //****************************************************************************** 683 684 // Status 685 const int par_stopped = 0; // stopped 686 const int par_connecting = 1; // running and active connecting 687 const int par_waiting = 2; // running and waiting for a connection 688 const int par_linked = 3; // running and connected : linked 689 const int par_sending = 4; // sending data 690 const int par_receiving = 5; // receiving data 691 const int par_binderror = 6; // error starting passive server 692 693 // Errors 694 const longword errParAddressInUse = 0x00200000; 695 const longword errParNoRoom = 0x00300000; 696 const longword errServerNoRoom = 0x00400000; 697 const longword errParInvalidParams = 0x00500000; 698 const longword errParNotLinked = 0x00600000; 699 const longword errParBusy = 0x00700000; 700 const longword errParFrameTimeout = 0x00800000; 701 const longword errParInvalidPDU = 0x00900000; 702 const longword errParSendTimeout = 0x00A00000; 703 const longword errParRecvTimeout = 0x00B00000; 704 const longword errParSendRefused = 0x00C00000; 705 const longword errParNegotiatingPDU = 0x00D00000; 706 const longword errParSendingBlock = 0x00E00000; 707 const longword errParRecvingBlock = 0x00F00000; 708 const longword errParBindError = 0x01000000; 709 const longword errParDestroying = 0x01100000; 710 const longword errParInvalidParamNumber = 0x01200000; // Invalid param (par_get/set_param) 711 const longword errParCannotChangeParam = 0x01300000; // Cannot change because running 712 const longword errParBufferTooSmall = 0x01400000; // Raised by LabVIEW wrapper 713 714 // Brecv Data incoming Callback 715 typedef void (S7API *pfn_ParRecvCallBack)(void * usrPtr, int opResult, longword R_ID, void *pData, int Size); 716 // BSend Completion Callback 717 typedef void (S7API *pfn_ParSendCompletion)(void * usrPtr, int opResult); 718 719 S7Object S7API Par_Create(int Active); 720 void S7API Par_Destroy(S7Object *Partner); 721 int S7API Par_GetParam(S7Object Partner, int ParamNumber, void *pValue); 722 int S7API Par_SetParam(S7Object Partner, int ParamNumber, void *pValue); 723 int S7API Par_StartTo(S7Object Partner, const char *LocalAddress, const char *RemoteAddress, 724 word LocTsap, word RemTsap); 725 int S7API Par_Start(S7Object Partner); 726 int S7API Par_Stop(S7Object Partner); 727 // BSend 728 int S7API Par_BSend(S7Object Partner, longword R_ID, void *pUsrData, int Size); 729 int S7API Par_AsBSend(S7Object Partner, longword R_ID, void *pUsrData, int Size); 730 int S7API Par_CheckAsBSendCompletion(S7Object Partner, int *opResult); 731 int S7API Par_WaitAsBSendCompletion(S7Object Partner, longword Timeout); 732 int S7API Par_SetSendCallback(S7Object Partner, pfn_ParSendCompletion pCompletion, void *usrPtr); 733 // BRecv 734 int S7API Par_BRecv(S7Object Partner, longword *R_ID, void *pData, int *Size, longword Timeout); 735 int S7API Par_CheckAsBRecvCompletion(S7Object Partner, int *opResult, longword *R_ID, 736 void *pData, int *Size); 737 int S7API Par_SetRecvCallback(S7Object Partner, pfn_ParRecvCallBack pCompletion, void *usrPtr); 738 // Stat 739 int S7API Par_GetTimes(S7Object Partner, longword *SendTime, longword *RecvTime); 740 int S7API Par_GetStats(S7Object Partner, longword *BytesSent, longword *BytesRecv, 741 longword *SendErrors, longword *RecvErrors); 742 int S7API Par_GetLastError(S7Object Partner, int *LastError); 743 int S7API Par_GetStatus(S7Object Partner, int *Status); 744 int S7API Par_ErrorText(int Error, char *Text, int TextLen); 745 746 747 #pragma pack() 748 #ifdef __cplusplus 749 } 750 #endif // __cplusplus 751 752 #ifdef __cplusplus 753 754 //****************************************************************************** 755 // CLIENT CLASS DEFINITION 756 //****************************************************************************** 757 class TS7Client 758 { 759 private: 760 S7Object Client; 761 public: 762 TS7Client(); 763 ~TS7Client(); 764 // Control functions 765 int Connect(); 766 int ConnectTo(const char *RemAddress, int Rack, int Slot); 767 int SetConnectionParams(const char *RemAddress, word LocalTSAP, word RemoteTSAP); 768 int SetConnectionType(word ConnectionType); 769 int Disconnect(); 770 int GetParam(int ParamNumber, void *pValue); 771 int SetParam(int ParamNumber, void *pValue); 772 // Data I/O Main functions 773 int ReadArea(int Area, int DBNumber, int Start, int Amount, int WordLen, void *pUsrData); 774 int WriteArea(int Area, int DBNumber, int Start, int Amount, int WordLen, void *pUsrData); 775 int ReadMultiVars(PS7DataItem Item, int ItemsCount); 776 int WriteMultiVars(PS7DataItem Item, int ItemsCount); 777 // Data I/O Lean functions 778 int DBRead(int DBNumber, int Start, int Size, void *pUsrData); 779 int DBWrite(int DBNumber, int Start, int Size, void *pUsrData); 780 int MBRead(int Start, int Size, void *pUsrData); 781 int MBWrite(int Start, int Size, void *pUsrData); 782 int EBRead(int Start, int Size, void *pUsrData); 783 int EBWrite(int Start, int Size, void *pUsrData); 784 int ABRead(int Start, int Size, void *pUsrData); 785 int ABWrite(int Start, int Size, void *pUsrData); 786 int TMRead(int Start, int Amount, void *pUsrData); 787 int TMWrite(int Start, int Amount, void *pUsrData); 788 int CTRead(int Start, int Amount, void *pUsrData); 789 int CTWrite(int Start, int Amount, void *pUsrData); 790 // Directory functions 791 int ListBlocks(PS7BlocksList pUsrData); 792 int GetAgBlockInfo(int BlockType, int BlockNum, PS7BlockInfo pUsrData); 793 int GetPgBlockInfo(void *pBlock, PS7BlockInfo pUsrData, int Size); 794 int ListBlocksOfType(int BlockType, TS7BlocksOfType *pUsrData, int *ItemsCount); 795 // Blocks functions 796 int Upload(int BlockType, int BlockNum, void *pUsrData, int *Size); 797 int FullUpload(int BlockType, int BlockNum, void *pUsrData, int *Size); 798 int Download(int BlockNum, void *pUsrData, int Size); 799 int Delete(int BlockType, int BlockNum); 800 int DBGet(int DBNumber, void *pUsrData, int *Size); 801 int DBFill(int DBNumber, int FillChar); 802 // Date/Time functions 803 int GetPlcDateTime(tm *DateTime); 804 int SetPlcDateTime(tm *DateTime); 805 int SetPlcSystemDateTime(); 806 // System Info functions 807 int GetOrderCode(PS7OrderCode pUsrData); 808 int GetCpuInfo(PS7CpuInfo pUsrData); 809 int GetCpInfo(PS7CpInfo pUsrData); 810 int ReadSZL(int ID, int Index, PS7SZL pUsrData, int *Size); 811 int ReadSZLList(PS7SZLList pUsrData, int *ItemsCount); 812 // Control functions 813 int PlcHotStart(); 814 int PlcColdStart(); 815 int PlcStop(); 816 int CopyRamToRom(int Timeout); 817 int Compress(int Timeout); 818 // Security functions 819 int GetProtection(PS7Protection pUsrData); 820 int SetSessionPassword(char *Password); 821 int ClearSessionPassword(); 822 // Properties 823 int ExecTime(); 824 int LastError(); 825 int PDURequested(); 826 int PDULength(); 827 int PlcStatus(); 828 bool Connected(); 829 // Async functions 830 int SetAsCallback(pfn_CliCompletion pCompletion, void *usrPtr); 831 bool CheckAsCompletion(int *opResult); 832 int WaitAsCompletion(longword Timeout); 833 int AsReadArea(int Area, int DBNumber, int Start, int Amount, int WordLen, void *pUsrData); 834 int AsWriteArea(int Area, int DBNumber, int Start, int Amount, int WordLen, void *pUsrData); 835 int AsListBlocksOfType(int BlockType, PS7BlocksOfType pUsrData, int *ItemsCount); 836 int AsReadSZL(int ID, int Index, PS7SZL pUsrData, int *Size); 837 int AsReadSZLList(PS7SZLList pUsrData, int *ItemsCount); 838 int AsUpload(int BlockType, int BlockNum, void *pUsrData, int *Size); 839 int AsFullUpload(int BlockType, int BlockNum, void *pUsrData, int *Size); 840 int AsDownload(int BlockNum, void *pUsrData, int Size); 841 int AsCopyRamToRom(int Timeout); 842 int AsCompress(int Timeout); 843 int AsDBRead(int DBNumber, int Start, int Size, void *pUsrData); 844 int AsDBWrite(int DBNumber, int Start, int Size, void *pUsrData); 845 int AsMBRead(int Start, int Size, void *pUsrData); 846 int AsMBWrite(int Start, int Size, void *pUsrData); 847 int AsEBRead(int Start, int Size, void *pUsrData); 848 int AsEBWrite(int Start, int Size, void *pUsrData); 849 int AsABRead(int Start, int Size, void *pUsrData); 850 int AsABWrite(int Start, int Size, void *pUsrData); 851 int AsTMRead(int Start, int Amount, void *pUsrData); 852 int AsTMWrite(int Start, int Amount, void *pUsrData); 853 int AsCTRead(int Start, int Amount, void *pUsrData); 854 int AsCTWrite(int Start, int Amount, void *pUsrData); 855 int AsDBGet(int DBNumber, void *pUsrData, int *Size); 856 int AsDBFill(int DBNumber, int FillChar); 857 }; 858 typedef TS7Client *PS7Client; 859 //****************************************************************************** 860 // SERVER CLASS DEFINITION 861 //****************************************************************************** 862 class TS7Server 863 { 864 private: 865 S7Object Server; 866 public: 867 TS7Server(); 868 ~TS7Server(); 869 // Control 870 int Start(); 871 int StartTo(const char *Address); 872 int Stop(); 873 int GetParam(int ParamNumber, void *pValue); 874 int SetParam(int ParamNumber, void *pValue); 875 // Events 876 int SetEventsCallback(pfn_SrvCallBack PCallBack, void *UsrPtr); 877 int SetReadEventsCallback(pfn_SrvCallBack PCallBack, void *UsrPtr); 878 int SetRWAreaCallback(pfn_RWAreaCallBack PCallBack, void *UsrPtr); 879 bool PickEvent(TSrvEvent *pEvent); 880 void ClearEvents(); 881 longword GetEventsMask(); 882 longword GetLogMask(); 883 void SetEventsMask(longword Mask); 884 void SetLogMask(longword Mask); 885 // Resources 886 int RegisterArea(int AreaCode, word Index, void *pUsrData, word Size); 887 int UnregisterArea(int AreaCode, word Index); 888 int LockArea(int AreaCode, word Index); 889 int UnlockArea(int AreaCode, word Index); 890 // Properties 891 int ServerStatus(); 892 int GetCpuStatus(); 893 int SetCpuStatus(int Status); 894 int ClientsCount(); 895 }; 896 typedef TS7Server *PS7Server; 897 898 //****************************************************************************** 899 // PARTNER CLASS DEFINITION 900 //****************************************************************************** 901 class TS7Partner 902 { 903 private: 904 S7Object Partner; // Partner Handle 905 public: 906 TS7Partner(bool Active); 907 ~TS7Partner(); 908 // Control 909 int GetParam(int ParamNumber, void *pValue); 910 int SetParam(int ParamNumber, void *pValue); 911 int Start(); 912 int StartTo(const char *LocalAddress, 913 const char *RemoteAddress, 914 int LocalTSAP, 915 int RemoteTSAP); 916 int Stop(); 917 // Data I/O functions : BSend 918 int BSend(longword R_ID, void *pUsrData, int Size); 919 int AsBSend(longword R_ID, void *pUsrData, int Size); 920 bool CheckAsBSendCompletion(int *opResult); 921 int WaitAsBSendCompletion(longword Timeout); 922 int SetSendCallback(pfn_ParSendCompletion pCompletion, void *usrPtr); 923 // Data I/O functions : BRecv 924 int BRecv(longword *R_ID, void *pUsrData, int *Size, longword Timeout); 925 bool CheckAsBRecvCompletion(int *opResult, longword *R_ID, void *pUsrData, int *Size); 926 int SetRecvCallback(pfn_ParRecvCallBack pCallback, void *usrPtr); 927 // Properties 928 int Status(); 929 int LastError(); 930 int GetTimes(longword *SendTime, longword *RecvTime); 931 int GetStats(longword *BytesSent, 932 longword *BytesRecv, 933 longword *ErrSend, 934 longword *ErrRecv); 935 bool Linked(); 936 }; 937 typedef TS7Partner *PS7Partner; 938 //****************************************************************************** 939 // TEXT ROUTINES 940 // Only for C++, for pure C use xxx_ErrorText() which uses *char 941 //****************************************************************************** 942 #define TextLen 1024 943 944 // String type 945 // Here we define generic TextString (by default mapped onto std::string). 946 // So you can change it if needed (Unicodestring, Ansistring etc...) 947 948 typedef std::string TextString; 949 950 TextString CliErrorText(int Error); 951 TextString SrvErrorText(int Error); 952 TextString ParErrorText(int Error); 953 TextString SrvEventText(TSrvEvent *Event); 954 955 956 #endif // __cplusplus 957 #endif // snap7_h 958