build success, test on ok1052-c

This commit is contained in:
Forsworns 2022-01-14 16:33:23 +08:00
parent 28533454c0
commit 4f59e4ea24
4 changed files with 409 additions and 12 deletions

View File

@ -1,3 +1,3 @@
SRC_FILES :=s7_demo.cpp SRC_FILES :=s7_demo.c
include $(KERNEL_ROOT)/compiler.mk include $(KERNEL_ROOT)/compiler.mk

View File

@ -1,9 +1,10 @@
#include "snap7_libmain.h" #include "snap7.h"
#include <transform.h>
static S7Object Client; static S7Object Client;
static unsigned char Buffer[65536]; // 64 K buffer static unsigned char Buffer[65536]; // 64 K buffer
static char Address[20]="192.168.250.8"; // PLC IP Address static char Address[20]="192.168.250.252"; // PLC IP Address 192.168.250.8
static int Rack = 0, Slot = 1; // Default Rack and Slot static int Rack = 0, Slot = 1; // Default Rack and Slot
static int Area = S7AreaDB, DBNumber = 13, Start = 0, Amount = 4, WordLen = S7WLByte; // Default Rack and Slot static int Area = S7AreaDB, DBNumber = 13, Start = 0, Amount = 4, WordLen = S7WLByte; // Default Rack and Slot
static int bRead = 1; // By default read static int bRead = 1; // By default read
@ -17,9 +18,9 @@ static int CliConnect()
if (Check(Client, res, "UNIT Connection")) if (Check(Client, res, "UNIT Connection"))
{ {
Cli_GetPduLength(Client, Requested, Negotiated); Cli_GetPduLength(Client, Requested, Negotiated);
printf(" Connected to : %s (Rack=%d, Slot=%d)\n", Address, Rack, Slot); KPrintf(" Connected to : %s (Rack=%d, Slot=%d)\n", Address, Rack, Slot);
printf(" PDU Requested : %d bytes\n", Requested); KPrintf(" PDU Requested : %d bytes\n", Requested);
printf(" PDU Negotiated : %d bytes\n", Negotiated); KPrintf(" PDU Negotiated : %d bytes\n", Negotiated);
}; };
return !res; return !res;
} }
@ -37,20 +38,20 @@ void PerformTasks()
Buffer[0] = 0x01; Buffer[0] = 0x01;
Buffer[3] = 0x14; Buffer[3] = 0x14;
Cli_WriteAndDump(Client, Area, DBNumber, Start, Amount, WordLen, Buffer, "write DB"); Cli_WriteAndDump(Client, Area, DBNumber, Start, Amount, WordLen, Buffer, "write DB");
sleep(29); PrivTaskDelay(29);
} }
if(bRead){ if(bRead){
Cli_ReadAndDump(Client, Area, DBNumber, Start, Amount, WordLen, Buffer, "read DB"); Cli_ReadAndDump(Client, Area, DBNumber, Start, Amount, WordLen, Buffer, "read DB");
sleep(1); PrivTaskDelay(1);
} }
if(bWrite){ if(bWrite){
memset(Buffer, 0, sizeof(Buffer)); memset(Buffer, 0, sizeof(Buffer));
Cli_WriteAndDump(Client, Area, DBNumber, Start, Amount, WordLen, Buffer, "write DB"); Cli_WriteAndDump(Client, Area, DBNumber, Start, Amount, WordLen, Buffer, "write DB");
sleep(29); PrivTaskDelay(29);
} }
if(bRead){ if(bRead){
Cli_ReadAndDump(Client, Area, DBNumber, Start, Amount, WordLen, Buffer, "read DB"); Cli_ReadAndDump(Client, Area, DBNumber, Start, Amount, WordLen, Buffer, "read DB");
sleep(1); PrivTaskDelay(1);
} }
} }
} }
@ -70,3 +71,5 @@ void s7_demo()
// Deletion // Deletion
Cli_Destroy(Client); Cli_Destroy(Client);
} }
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(0),
S7Demo, s7_demo, Test Snap7 s7_demo);

View File

@ -0,0 +1,396 @@
#ifndef snap7_h
#define snap7_h
//---------------------------------------------------------------------------
# include <stdint.h>
# include <unistd.h>
//******************************************************************************
// COMMON
//******************************************************************************
// Exact length types regardless of platform/processor
typedef uint8_t byte;
typedef uint16_t word;
typedef uint32_t longword;
typedef byte *pbyte;
typedef word *pword;
typedef uintptr_t S7Object; // multi platform/processor object reference
// DON'T CONFUSE IT WITH AN OLE OBJECT, IT'S SIMPLY
// AN INTEGER VALUE (32 OR 64 BIT) USED AS HANDLE.
#ifndef __cplusplus
typedef struct
{
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
}tm;
typedef int bool;
#define false 0;
#define true 1;
#endif
const int errLibInvalidParam = -1;
const int errLibInvalidObject = -2;
// CPU status
#define S7CpuStatusUnknown 0x00
#define S7CpuStatusRun 0x08
#define S7CpuStatusStop 0x04
// ISO Errors
const longword errIsoConnect = 0x00010000; // Connection error
const longword errIsoDisconnect = 0x00020000; // Disconnect error
const longword errIsoInvalidPDU = 0x00030000; // Bad format
const longword errIsoInvalidDataSize = 0x00040000; // Bad Datasize passed to send/recv buffer is invalid
const longword errIsoNullPointer = 0x00050000; // Null passed as pointer
const longword errIsoShortPacket = 0x00060000; // A short packet received
const longword errIsoTooManyFragments = 0x00070000; // Too many packets without EoT flag
const longword errIsoPduOverflow = 0x00080000; // The sum of fragments data exceded maximum packet size
const longword errIsoSendPacket = 0x00090000; // An error occurred during send
const longword errIsoRecvPacket = 0x000A0000; // An error occurred during recv
const longword errIsoInvalidParams = 0x000B0000; // Invalid TSAP params
const longword errIsoResvd_1 = 0x000C0000; // Unassigned
const longword errIsoResvd_2 = 0x000D0000; // Unassigned
const longword errIsoResvd_3 = 0x000E0000; // Unassigned
const longword errIsoResvd_4 = 0x000F0000; // Unassigned
// Tag Struct
typedef struct{
int Area;
int DBNumber;
int Start;
int Size;
int WordLen;
}TS7Tag, *PS7Tag;
//------------------------------------------------------------------------------
// PARAMS LIST
//------------------------------------------------------------------------------
const int p_u16_LocalPort = 1;
const int p_u16_RemotePort = 2;
const int p_i32_PingTimeout = 3;
const int p_i32_SendTimeout = 4;
const int p_i32_RecvTimeout = 5;
const int p_i32_WorkInterval = 6;
const int p_u16_SrcRef = 7;
const int p_u16_DstRef = 8;
const int p_u16_SrcTSap = 9;
const int p_i32_PDURequest = 10;
const int p_i32_MaxClients = 11;
const int p_i32_BSendTimeout = 12;
const int p_i32_BRecvTimeout = 13;
const int p_u32_RecoveryTime = 14;
const int p_u32_KeepAliveTime = 15;
// Client/Partner Job status
const int JobComplete = 0;
const int JobPending = 1;
//******************************************************************************
// CLIENT
//******************************************************************************
// Error codes
const longword errNegotiatingPDU = 0x00100000;
const longword errCliInvalidParams = 0x00200000;
const longword errCliJobPending = 0x00300000;
const longword errCliTooManyItems = 0x00400000;
const longword errCliInvalidWordLen = 0x00500000;
const longword errCliPartialDataWritten = 0x00600000;
const longword errCliSizeOverPDU = 0x00700000;
const longword errCliInvalidPlcAnswer = 0x00800000;
const longword errCliAddressOutOfRange = 0x00900000;
const longword errCliInvalidTransportSize = 0x00A00000;
const longword errCliWriteDataSizeMismatch = 0x00B00000;
const longword errCliItemNotAvailable = 0x00C00000;
const longword errCliInvalidValue = 0x00D00000;
const longword errCliCannotStartPLC = 0x00E00000;
const longword errCliAlreadyRun = 0x00F00000;
const longword errCliCannotStopPLC = 0x01000000;
const longword errCliCannotCopyRamToRom = 0x01100000;
const longword errCliCannotCompress = 0x01200000;
const longword errCliAlreadyStop = 0x01300000;
const longword errCliFunNotAvailable = 0x01400000;
const longword errCliUploadSequenceFailed = 0x01500000;
const longword errCliInvalidDataSizeRecvd = 0x01600000;
const longword errCliInvalidBlockType = 0x01700000;
const longword errCliInvalidBlockNumber = 0x01800000;
const longword errCliInvalidBlockSize = 0x01900000;
const longword errCliDownloadSequenceFailed = 0x01A00000;
const longword errCliInsertRefused = 0x01B00000;
const longword errCliDeleteRefused = 0x01C00000;
const longword errCliNeedPassword = 0x01D00000;
const longword errCliInvalidPassword = 0x01E00000;
const longword errCliNoPasswordToSetOrClear = 0x01F00000;
const longword errCliJobTimeout = 0x02000000;
const longword errCliPartialDataRead = 0x02100000;
const longword errCliBufferTooSmall = 0x02200000;
const longword errCliFunctionRefused = 0x02300000;
const longword errCliDestroying = 0x02400000;
const longword errCliInvalidParamNumber = 0x02500000;
const longword errCliCannotChangeParam = 0x02600000;
const int MaxVars = 20; // Max vars that can be transferred with MultiRead/MultiWrite
// Client Connection Type
const word CONNTYPE_PG = 0x0001; // Connect to the PLC as a PG
const word CONNTYPE_OP = 0x0002; // Connect to the PLC as an OP
const word CONNTYPE_BASIC = 0x0003; // Basic connection
// Area ID
const byte S7AreaPE = 0x81;
const byte S7AreaPA = 0x82;
const byte S7AreaMK = 0x83;
const byte S7AreaDB = 0x84;
const byte S7AreaCT = 0x1C;
const byte S7AreaTM = 0x1D;
// Word Length
const int S7WLBit = 0x01;
const int S7WLByte = 0x02;
const int S7WLWord = 0x04;
const int S7WLDWord = 0x06;
const int S7WLReal = 0x08;
const int S7WLCounter = 0x1C;
const int S7WLTimer = 0x1D;
// Block type
const byte Block_OB = 0x38;
const byte Block_DB = 0x41;
const byte Block_SDB = 0x42;
const byte Block_FC = 0x43;
const byte Block_SFC = 0x44;
const byte Block_FB = 0x45;
const byte Block_SFB = 0x46;
// Sub Block Type
const byte SubBlk_OB = 0x08;
const byte SubBlk_DB = 0x0A;
const byte SubBlk_SDB = 0x0B;
const byte SubBlk_FC = 0x0C;
const byte SubBlk_SFC = 0x0D;
const byte SubBlk_FB = 0x0E;
const byte SubBlk_SFB = 0x0F;
// Block languages
const byte BlockLangAWL = 0x01;
const byte BlockLangKOP = 0x02;
const byte BlockLangFUP = 0x03;
const byte BlockLangSCL = 0x04;
const byte BlockLangDB = 0x05;
const byte BlockLangGRAPH = 0x06;
// Read/Write Multivars
typedef struct{
int Area;
int WordLen;
int Result;
int DBNumber;
int Start;
int Amount;
void *pdata;
} TS7DataItem, *PS7DataItem;
//typedef int TS7ResultItems[MaxVars];
//typedef TS7ResultItems *PS7ResultItems;
// List Blocks
typedef struct {
int OBCount;
int FBCount;
int FCCount;
int SFBCount;
int SFCCount;
int DBCount;
int SDBCount;
} TS7BlocksList, *PS7BlocksList;
// Blocks info
typedef struct {
int BlkType; // Block Type (OB, DB)
int BlkNumber; // Block number
int BlkLang; // Block Language
int BlkFlags; // Block flags
int MC7Size; // The real size in bytes
int LoadSize; // Load memory size
int LocalData; // Local data
int SBBLength; // SBB Length
int CheckSum; // Checksum
int Version; // Block version
// Chars info
char CodeDate[11]; // Code date
char IntfDate[11]; // Interface date
char Author[9]; // Author
char Family[9]; // Family
char Header[9]; // Header
} TS7BlockInfo, *PS7BlockInfo ;
typedef word TS7BlocksOfType[0x2000];
typedef TS7BlocksOfType *PS7BlocksOfType;
// Order code
typedef struct {
char Code[21];
byte V1;
byte V2;
byte V3;
} TS7OrderCode, *PS7OrderCode;
// CPU Info
typedef struct {
char ModuleTypeName[33];
char SerialNumber[25];
char ASName[25];
char Copyright[27];
char ModuleName[25];
} TS7CpuInfo, *PS7CpuInfo;
// CP Info
typedef struct {
int MaxPduLengt;
int MaxConnections;
int MaxMpiRate;
int MaxBusRate;
} TS7CpInfo, *PS7CpInfo;
// See §33.1 of "System Software for S7-300/400 System and Standard Functions"
// and see SFC51 description too
typedef struct {
word LENTHDR;
word N_DR;
} SZL_HEADER, *PSZL_HEADER;
typedef struct {
SZL_HEADER Header;
byte Data[0x4000-4];
} TS7SZL, *PS7SZL;
// SZL List of available SZL IDs : same as SZL but List items are big-endian adjusted
typedef struct {
SZL_HEADER Header;
word List[0x2000-2];
} TS7SZLList, *PS7SZLList;
// See §33.19 of "System Software for S7-300/400 System and Standard Functions"
typedef struct {
word sch_schal;
word sch_par;
word sch_rel;
word bart_sch;
word anl_sch;
} TS7Protection, *PS7Protection;
// Client completion callback
typedef void (*pfn_CliCompletion) (void *usrPtr, int opCode, int opResult);
//------------------------------------------------------------------------------
// Import prototypes
//------------------------------------------------------------------------------
S7Object Cli_Create();
void Cli_Destroy(S7Object *Client);
int Cli_ConnectTo(S7Object Client, const char *Address, int Rack, int Slot);
int Cli_SetConnectionParams(S7Object Client, const char *Address, word LocalTSAP, word RemoteTSAP);
int Cli_SetConnectionType(S7Object Client, word ConnectionType);
int Cli_Connect(S7Object Client);
int Cli_Disconnect(S7Object Client);
int Cli_GetParam(S7Object Client, int ParamNumber, void *pValue);
int Cli_SetParam(S7Object Client, int ParamNumber, void *pValue);
int Cli_SetAsCallback(S7Object Client, pfn_CliCompletion pCompletion, void *usrPtr);
// Data I/O main functions
int Cli_ReadArea(S7Object Client, int Area, int DBNumber, int Start, int Amount, int WordLen, void *pUsrData);
int Cli_WriteArea(S7Object Client, int Area, int DBNumber, int Start, int Amount, int WordLen, void *pUsrData);
int Cli_ReadMultiVars(S7Object Client, PS7DataItem Item, int ItemsCount);
int Cli_WriteMultiVars(S7Object Client, PS7DataItem Item, int ItemsCount);
// Data I/O Lean functions
int Cli_DBRead(S7Object Client, int DBNumber, int Start, int Size, void *pUsrData);
int Cli_DBWrite(S7Object Client, int DBNumber, int Start, int Size, void *pUsrData);
int Cli_MBRead(S7Object Client, int Start, int Size, void *pUsrData);
int Cli_MBWrite(S7Object Client, int Start, int Size, void *pUsrData);
int Cli_EBRead(S7Object Client, int Start, int Size, void *pUsrData);
int Cli_EBWrite(S7Object Client, int Start, int Size, void *pUsrData);
int Cli_ABRead(S7Object Client, int Start, int Size, void *pUsrData);
int Cli_ABWrite(S7Object Client, int Start, int Size, void *pUsrData);
int Cli_TMRead(S7Object Client, int Start, int Amount, void *pUsrData);
int Cli_TMWrite(S7Object Client, int Start, int Amount, void *pUsrData);
int Cli_CTRead(S7Object Client, int Start, int Amount, void *pUsrData);
int Cli_CTWrite(S7Object Client, int Start, int Amount, void *pUsrData);
// Directory functions
int Cli_ListBlocks(S7Object Client, TS7BlocksList *pUsrData);
int Cli_GetAgBlockInfo(S7Object Client, int BlockType, int BlockNum, TS7BlockInfo *pUsrData);
int Cli_GetPgBlockInfo(S7Object Client, void *pBlock, TS7BlockInfo *pUsrData, int Size);
int Cli_ListBlocksOfType(S7Object Client, int BlockType, TS7BlocksOfType *pUsrData, int *ItemsCount);
// Blocks functions
int Cli_Upload(S7Object Client, int BlockType, int BlockNum, void *pUsrData, int *Size);
int Cli_FullUpload(S7Object Client, int BlockType, int BlockNum, void *pUsrData, int *Size);
int Cli_Download(S7Object Client, int BlockNum, void *pUsrData, int Size);
int Cli_Delete(S7Object Client, int BlockType, int BlockNum);
int Cli_DBGet(S7Object Client, int DBNumber, void *pUsrData, int *Size);
int Cli_DBFill(S7Object Client, int DBNumber, int FillChar);
// Date/Time functions
int Cli_GetPlcDateTime(S7Object Client, tm *DateTime);
int Cli_SetPlcDateTime(S7Object Client, tm *DateTime);
int Cli_SetPlcSystemDateTime(S7Object Client);
// System Info functions
int Cli_GetOrderCode(S7Object Client, TS7OrderCode *pUsrData);
int Cli_GetCpuInfo(S7Object Client, TS7CpuInfo *pUsrData);
int Cli_GetCpInfo(S7Object Client, TS7CpInfo *pUsrData);
int Cli_ReadSZL(S7Object Client, int ID, int Index, TS7SZL *pUsrData, int *Size);
int Cli_ReadSZLList(S7Object Client, TS7SZLList *pUsrData, int *ItemsCount);
// Control functions
int Cli_PlcHotStart(S7Object Client);
int Cli_PlcColdStart(S7Object Client);
int Cli_PlcStop(S7Object Client);
int Cli_CopyRamToRom(S7Object Client, int Timeout);
int Cli_Compress(S7Object Client, int Timeout);
int Cli_GetPlcStatus(S7Object Client, int *Status);
// Security functions
int Cli_GetProtection(S7Object Client, TS7Protection *pUsrData);
int Cli_SetSessionPassword(S7Object Client, char *Password);
int Cli_ClearSessionPassword(S7Object Client);
// Low level
int Cli_IsoExchangeBuffer(S7Object Client, void *pUsrData, int *Size);
// Misc
int Cli_GetExecTime(S7Object Client, int *Time);
int Cli_GetLastError(S7Object Client, int *LastError);
int Cli_GetPduLength(S7Object Client, int *Requested, int *Negotiated);
int Cli_ErrorText(int Error, char *Text, int TextLen);
// 1.1.0
int Cli_GetConnected(S7Object Client, int *Connected);
//------------------------------------------------------------------------------
// Async functions
//------------------------------------------------------------------------------
int Cli_AsReadArea(S7Object Client, int Area, int DBNumber, int Start, int Amount, int WordLen, void *pUsrData);
int Cli_AsWriteArea(S7Object Client, int Area, int DBNumber, int Start, int Amount, int WordLen, void *pUsrData);
int Cli_AsDBRead(S7Object Client, int DBNumber, int Start, int Size, void *pUsrData);
int Cli_AsDBWrite(S7Object Client, int DBNumber, int Start, int Size, void *pUsrData);
int Cli_AsMBRead(S7Object Client, int Start, int Size, void *pUsrData);
int Cli_AsMBWrite(S7Object Client, int Start, int Size, void *pUsrData);
int Cli_AsEBRead(S7Object Client, int Start, int Size, void *pUsrData);
int Cli_AsEBWrite(S7Object Client, int Start, int Size, void *pUsrData);
int Cli_AsABRead(S7Object Client, int Start, int Size, void *pUsrData);
int Cli_AsABWrite(S7Object Client, int Start, int Size, void *pUsrData);
int Cli_AsTMRead(S7Object Client, int Start, int Amount, void *pUsrData);
int Cli_AsTMWrite(S7Object Client, int Start, int Amount, void *pUsrData);
int Cli_AsCTRead(S7Object Client, int Start, int Amount, void *pUsrData);
int Cli_AsCTWrite(S7Object Client, int Start, int Amount, void *pUsrData);
int Cli_AsListBlocksOfType(S7Object Client, int BlockType, TS7BlocksOfType *pUsrData, int *ItemsCount);
int Cli_AsReadSZL(S7Object Client, int ID, int Index, TS7SZL *pUsrData, int *Size);
int Cli_AsReadSZLList(S7Object Client, TS7SZLList *pUsrData, int *ItemsCount);
int Cli_AsUpload(S7Object Client, int BlockType, int BlockNum, void *pUsrData, int *Size);
int Cli_AsFullUpload(S7Object Client, int BlockType, int BlockNum, void *pUsrData, int *Size);
int Cli_AsDownload(S7Object Client, int BlockNum, void *pUsrData, int Size);
int Cli_AsCopyRamToRom(S7Object Client, int Timeout);
int Cli_AsCompress(S7Object Client, int Timeout);
int Cli_AsDBGet(S7Object Client, int DBNumber, void *pUsrData, int *Size);
int Cli_AsDBFill(S7Object Client, int DBNumber, int FillChar);
int Cli_CheckAsCompletion(S7Object Client, int *opResult);
int Cli_WaitAsCompletion(S7Object Client, int Timeout);
#endif // snap7_h

View File

@ -45,8 +45,6 @@
#define EXPORTSPEC extern "C" #define EXPORTSPEC extern "C"
#define S7API #define S7API
// Exact length types regardless of platform/processor // Exact length types regardless of platform/processor
// We absolute need of them, all structs have an exact size that // We absolute need of them, all structs have an exact size that
// must be the same across the processor used 32/64 bit // must be the same across the processor used 32/64 bit