Skip to content

Commit a99127d

Browse files
committed
update: snap7 -> 1.3.0
1 parent 04794da commit a99127d

2 files changed

Lines changed: 164 additions & 2 deletions

File tree

src/snap7.cpp

Lines changed: 135 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*=============================================================================|
2-
| PROJECT SNAP7 1.2.0 |
2+
| PROJECT SNAP7 1.3.0 |
33
|==============================================================================|
44
| Copyright (C) 2013, 2014 Davide Nardella |
55
| All rights reserved. |
@@ -767,4 +767,138 @@ TextString SrvEventText(TSrvEvent *Event)
767767
Srv_EventText(Event, text, TextLen);
768768
return TextString(text);
769769
}
770+
//==============================================================================
771+
// Helper GET routines
772+
//==============================================================================
773+
word SwapWord(pword Value)
774+
{
775+
return ((*Value >> 8) & 0xFF) | ((*Value << 8) & 0xFF00);
776+
}
777+
//---------------------------------------------------------------------------
778+
longword SwapDWord(plongword Value)
779+
{
780+
return (*Value >> 24) | ((*Value << 8) & 0x00FF0000) | ((*Value >> 8) & 0x0000FF00) | (*Value << 24);
781+
}
782+
//---------------------------------------------------------------------------
783+
bool GetBitAt(void *Buffer, int Pos, int Bit)
784+
{
785+
byte Mask[] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
786+
if (Bit < 0) Bit = 0;
787+
if (Bit > 7) Bit = 7;
788+
return (*(pbyte(Buffer)+Pos) & Mask[Bit]) != 0;
789+
}
790+
//---------------------------------------------------------------------------
791+
byte GetByteAt(void *Buffer, int Pos)
792+
{
793+
return *(pbyte(Buffer)+Pos);
794+
}
795+
//---------------------------------------------------------------------------
796+
word GetWordAt(void *Buffer, int Pos)
797+
{
798+
return SwapWord(pword(pbyte(Buffer)+Pos));
799+
}
800+
//---------------------------------------------------------------------------
801+
smallint GetIntAt(void *Buffer, int Pos)
802+
{
803+
return smallint(SwapWord(pword(pbyte(Buffer)+Pos)));
804+
}
805+
//---------------------------------------------------------------------------
806+
longword GetDWordAt(void *Buffer, int Pos)
807+
{
808+
return SwapDWord(plongword(pbyte(Buffer)+Pos));
809+
}
810+
//---------------------------------------------------------------------------
811+
longint GetDIntAt(void *Buffer, int Pos)
812+
{
813+
return longint(SwapDWord(plongword(pbyte(Buffer)+Pos)));
814+
}
815+
//---------------------------------------------------------------------------
816+
float GetRealAt(void *Buffer, int Pos)
817+
{
818+
longword lw=SwapDWord(plongword(pbyte(Buffer)+Pos));
819+
return *pfloat(&lw);
820+
}
821+
//---------------------------------------------------------------------------
822+
byte BCDtoByte(byte B)
823+
{
824+
return ((B >> 4) * 10) + (B & 0x0F);
825+
}
826+
//---------------------------------------------------------------------------
827+
struct tm GetDateTimeAt(void *Buffer, int Pos)
828+
{
829+
struct tm DateTime;
830+
pbyte p = pbyte(Buffer)+Pos;
831+
word Year=BCDtoByte(*p);
770832

833+
if (Year<90) Year+=100;
834+
DateTime.tm_year=Year;
835+
DateTime.tm_mon =BCDtoByte(*(p+1))-1;
836+
DateTime.tm_mday=BCDtoByte(*(p+2));
837+
DateTime.tm_hour=BCDtoByte(*(p+3));
838+
DateTime.tm_min =BCDtoByte(*(p+4));
839+
DateTime.tm_sec =BCDtoByte(*(p+5));
840+
DateTime.tm_wday=(*(p+7) & 0x0F)-1;
841+
return DateTime;
842+
}
843+
//==============================================================================
844+
// Helper SET routines
845+
//==============================================================================
846+
void SetBitAt(void *Buffer, int Pos, int Bit, bool Value)
847+
{
848+
byte Mask[] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
849+
pbyte p = pbyte(Buffer)+Pos;
850+
if (Bit < 0) Bit = 0;
851+
if (Bit > 7) Bit = 7;
852+
(Value) ? *p |= Mask[Bit] : *p &= ~Mask[Bit];
853+
}
854+
//---------------------------------------------------------------------------
855+
void SetByteAt(void *Buffer, int Pos, byte Value)
856+
{
857+
*(pbyte(Buffer)+Pos)=Value;
858+
}
859+
//---------------------------------------------------------------------------
860+
void SetWordAt(void *Buffer, int Pos, word Value)
861+
{
862+
*(pword(pbyte(Buffer)+Pos))=SwapWord(&Value);
863+
}
864+
//---------------------------------------------------------------------------
865+
void SetIntAt(void *Buffer, int Pos, smallint Value)
866+
{
867+
*(psmallint(pbyte(Buffer)+Pos))=SwapWord(pword(&Value));
868+
}
869+
//---------------------------------------------------------------------------
870+
void SetDWordAt(void *Buffer, int Pos, longword Value)
871+
{
872+
*(plongword(pbyte(Buffer)+Pos))=SwapDWord(&Value);
873+
}
874+
//---------------------------------------------------------------------------
875+
void SetDIntAt(void *Buffer, int Pos, longint Value)
876+
{
877+
*(plongint(pbyte(Buffer)+Pos))=SwapDWord(plongword(&Value));
878+
}
879+
//---------------------------------------------------------------------------
880+
void SetRealAt(void *Buffer, int Pos, float Value)
881+
{
882+
*(plongword(pbyte(Buffer)+Pos))=SwapDWord(plongword(&Value));
883+
}
884+
//---------------------------------------------------------------------------
885+
byte WordToBCD(word Value)
886+
{
887+
return ((Value / 10) << 4) | (Value % 10);
888+
}
889+
//---------------------------------------------------------------------------
890+
void SetDateTimeAt(void *Buffer, int Pos, tm Value)
891+
{
892+
pbyte p = pbyte(Buffer)+Pos;
893+
word Year = Value.tm_year;
894+
895+
if (Year>99) Year-=100;
896+
*p=WordToBCD(Year);
897+
*(p+1)=WordToBCD(Value.tm_mon+1);
898+
*(p+2)=WordToBCD(Value.tm_mday);
899+
*(p+3)=WordToBCD(Value.tm_hour);
900+
*(p+4)=WordToBCD(Value.tm_min);
901+
*(p+5)=WordToBCD(Value.tm_sec);
902+
*(p+6)=0;
903+
*(p+7)=Value.tm_wday+1;
904+
}

src/snap7.h

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*=============================================================================|
2-
| PROJECT SNAP7 1.2.0 |
2+
| PROJECT SNAP7 1.3.0 |
33
|==============================================================================|
44
| Copyright (C) 2013, 2014 Davide Nardella |
55
| All rights reserved. |
@@ -143,9 +143,15 @@ extern "C" {
143143
// Exact length types regardless of platform/processor
144144
typedef uint8_t byte;
145145
typedef uint16_t word;
146+
typedef int16_t smallint;
146147
typedef uint32_t longword;
148+
typedef int32_t longint;
147149
typedef byte *pbyte;
148150
typedef word *pword;
151+
typedef longword *plongword;
152+
typedef smallint *psmallint;
153+
typedef longint *plongint;
154+
typedef float *pfloat;
149155
typedef uintptr_t S7Object; // multi platform/processor object reference
150156
// DON'T CONFUSE IT WITH AN OLE OBJECT, IT'S SIMPLY
151157
// AN INTEGER VALUE (32 OR 64 BIT) USED AS HANDLE.
@@ -728,6 +734,28 @@ int S7API Par_GetLastError(S7Object Partner, int *LastError);
728734
int S7API Par_GetStatus(S7Object Partner, int *Status);
729735
int S7API Par_ErrorText(int Error, char *Text, int TextLen);
730736

737+
//******************************************************************************
738+
// HELPER DATA ACCESS FUNCTIONS
739+
//******************************************************************************
740+
// GET
741+
bool GetBitAt(void *Buffer, int Pos, int Bit);
742+
byte GetByteAt(void *Buffer, int Pos);
743+
word GetWordAt(void *Buffer, int Pos);
744+
smallint GetIntAt(void *Buffer, int Pos);
745+
longword GetDWordAt(void *Buffer, int Pos);
746+
longint GetDIntAt(void *Buffer, int Pos);
747+
float GetRealAt(void *Buffer, int Pos);
748+
struct tm GetDateTimeAt(void *Buffer, int Pos);
749+
// SET
750+
void SetBitAt(void *Buffer, int Pos, int Bit, bool Value);
751+
void SetByteAt(void *Buffer, int Pos, byte Value);
752+
void SetWordAt(void *Buffer, int Pos, word Value);
753+
void SetIntAt(void *Buffer, int Pos, smallint Value);
754+
void SetDWordAt(void *Buffer, int Pos, longword Value);
755+
void SetDIntAt(void *Buffer, int Pos, longint Value);
756+
void SetRealAt(void *Buffer, int Pos, float Value);
757+
void SetDateTimeAt(void *Buffer, int Pos, tm Value);
758+
731759

732760
#pragma pack()
733761
#ifdef __cplusplus

0 commit comments

Comments
 (0)