Knowledge Base Nr: 00142 s7koppel.cpp - http://www.swe-kaiser.de

Downloads: Source

Linux: S5/S7 Anbindung mit INAT-IP-Toolkit gekapselt in C++-Klasse

  
class CItem
{
public:
CItem();
virtual ~CItem();

CItem& operator= (const CItem& source);

char m_szName[200];
char m_szValue[200];
long m_lValue;
bool m_bIsGood;
time_t m_timestamp;
};

class CS7Koppel
{
public:
CS7Koppel();
~CS7Koppel();

int Connect(const char* szIPAddr, int nCPUNo);
int Disconnect();

//citems lesen
int ReadItem(CItem& cItem);
int ReadItems(int nItemCount, CItem cItems[]);

int WriteItem(CItem& cItem);
int WriteItems(int nItemCount, CItem cItems[]);

//low level access
//wortweiser zugriff
int ReadDB(int nDB, int nDW, unsigned short* nData);
int WriteDB(int nDB, int nDW, unsigned short nData);

//byteweiser zugriff
int ReadDI(int nByteAdr, unsigned short* nData);
int WriteDO(int nByteAdr, unsigned short nData);

int ReadM(int nByteAdr, unsigned short* nData);
int WriteM(int nByteAdr, unsigned short nData);

protected:
...
};

//sample
extern "C" int main (int argc, char **argv)
{
CS7Koppel s7;

int nErr = 0;
const char* lpszConnect = "153.95.169.172";

nErr = s7.Connect(lpszConnect, 2);
LOG("MAIN: Connect(%s, 2) nErr=%d", lpszConnect, nErr);

LOG("MAIN: High Level Interface ************************\n");

CItem item;
CItem items[10];
int nItemCount = 0;

strcpy(item.m_szName, "DB3.0");
items[nItemCount++] = item;
nErr = s7.ReadItem(item);
LOG("MAIN: ReadItem(%s) : val:%04X isgood:%d nErr=%d", item.m_szName, item.m_lValue, item.m_bIsGood, nErr);

strcpy(item.m_szName, "DI4.7");
items[nItemCount++] = item;
nErr = s7.ReadItem(item);
LOG("MAIN: ReadItem(%s) : val:%04X isgood:%d nErr=%d", item.m_szName, item.m_lValue, item.m_bIsGood, nErr);

strcpy(item.m_szName, "DI4");
items[nItemCount++] = item;
nErr = s7.ReadItem(item);
LOG("MAIN: ReadItem(%s) : val:%04X isgood:%d nErr=%d", item.m_szName, item.m_lValue, item.m_bIsGood, nErr);

strcpy(item.m_szName, "M0.1");
items[nItemCount++] = item;
nErr = s7.ReadItem(item);
LOG("MAIN: ReadItem(%s) : val:%04X isgood:%d nErr=%d", item.m_szName, item.m_lValue, item.m_bIsGood, nErr);

strcpy(item.m_szName, "M0");
items[nItemCount++] = item;
nErr = s7.ReadItem(item);
LOG("MAIN: ReadItem(%s) : val:%04X isgood:%d nErr=%d", item.m_szName, item.m_lValue, item.m_bIsGood, nErr);

nErr = s7.ReadItems(nItemCount, items);
LOG("MAIN: ReadItems(%d) : nErr=%d", nItemCount, nErr);
for (int n=0; n<nItemCount; n++)
LOG("MAIN: name:%s val:%04X isgood:%d", items[n].m_szName, items[n].m_lValue, items[n].m_bIsGood);
...
}