Snap7 source code

 

Snap7 is written in ISO C++, all projects/makefiles contain the -pedantic (or equivalent) switch.

The current release is build with 0 errors and 0 warnings except for Visual Studio (platform x64) that shows 3 warnings in snap_msgsock.cpp, but they can be safely ignored.

 

The source code is split into three folders.

[sys]

Contains base classes, the socket communication layer, the threads layer and some platform dependent files, the files are named snap_xxxxx.

These files are the lower layer of the Snap Project, Snap7 is the S7 implementation, SnapModbus … is coming soon (I hope).

[core]

Contains all files related to Snap7, the implementation of the IsoTCP and S7 protocol.

[lib]

Contains the interface files for creating the library.

 

There is a reason for this subdivision

·         If you want to write a multiplatform Ethernet packet driver, you can use only the sys folder.

·         If you want to embed Snap7 into your source code, include all but the lib folder.

 

Style

Snap7 is written in “C with objects” with two level of dependency.

The lowest allows to embed the Snap7MicroClient into your program. It was designed to be as much as possible friend of the “Embedded C++”.

No STL, exceptions, dynamic memory or threads are used, The virtual directive of some members can be deleted.

The resulting code should be “romable”.

Next paragraph will explain how to embed the micro client.

 

Embedding Snap7 MicroClient

 

As said, Snap7Microclient is not accessible from the outside of the library.

To embed it into your source code you need to include into your project these files:

o   sys\snap_msgsock.cpp

o   sys\snap_sysutils.cpp

o   core\s7_isotcp.cpp

o   core\s7_peer.cpp

o   core\s7_micro_client.cpp

 

Descrizione: Descrizione: Descrizione: Descrizione: C:\Deploy-1.2.0\www 1.2\snap7_source_file\image001.png

 

Rebuild Snap7

 

Due to its design (32 and native 64 bit) to rebuild the source code you need of a C99+ compiler, since stdint.h is strictly required.

Windows

 

Normally you do not need to recompile the libraries unless you modified the source code.

The 32 bit release already works in any version of Windows, either 32 or 64 bit, desktop or server, starting from Windows NT4 SP6 up to Windows 8.

The 64 bit release only works in all native 64 bit platforms, including the little-known Windows XP 64 Professional. To link them, your software itself must be native 64 bit.

In the folder build\windows there are 6 solutions ready to run, Host platform indicates where the compiler runs, and Target platform indicates where the library produced runs:

 

 

Host Platform

Target Platform

Notes

MinGW32 – 4.7.2

Win32/Win64

Win32/Win64

1

MinGW64 – 4.7.1

Win32/Win64

Win64

 

VS2008

Win32/Win64

Win32/Win64

2

VS2010

Win32/Win64

Win32/Win64

2

VS2012 (upd.2)

Win32/Win64

Win32/Win64

2,3

 

VS2013

Win32/Win64

Win32/Win64

 

 

1)   This is the only compiler that supports Windows NT 4.0 / Windows 2000 family.

2)   Express release needs Windows Software Development Kit (SDK) to compile 64 bit applications.

3)   Windows Vista 32 bit is the “oldest” platform supported by VS2012 without update 2.

 

Most likely Wine too is a suitable host platform for them, but currently is untested.


 

MinGW 32bit 4.7.2

It is assumed that MinGW compiler is installed in C:\MinGW32 and its release is 4.7.2.
If not, you need to modify make.bat and makefile.

o   In make.bat change the path instruction in the first line to point to the correct compiler path.

o   In makefile change the vars MINGW and MINREL (path and release).
Pay attention to not leave trailing spaces after the text.

If you don’t have it at all:

 

Go to

http://sourceforge.net/projects/orwelldevcpp/files/Compilers/MinGW/

download MinGW 4.7.2.7z and unpack it in c:\ .

 

No further settings are needed.

To build Snap7, open a command prompt into the working folder build\windows\MinGW32  and run "make all" (or "make clean" if you want to clean the project).

Into build\bin\win32 you will find snap7.dll and snap7.lib, the latter is the dynamic library import file to be used with C/C++ compilers (other languages don't need it).

Libstdc++ are statically linked, so you don’t need to distribute them with your software.


 

MinGW 64 bit 4.7.1

It is assumed that MinGW compiler is installed in C:\MinGW64 and its release is 4.7.1.
If not, you need to modify make.bat and makefile.

o   In make.bat change the path instruction in the first line to point to the correct compiler path.

o   In makefile change the vars MINGW and MINREL (path and release).
Pay attention to not leave trailing spaces after the text.

If you don’t have it at all:

 

Go to

http://sourceforge.net/projects/orwelldevcpp/files/Compilers/TDM-GCC/

 

download TDM-GCC 4.7.1 (4.7.1-tdm64-3).7z and unpack it in c:\ .

No further settings are needed.

To build Snap7, open a command prompt into the working folder build\windows\MinGW64  and run "make all" (or "make clean" if you want to clean the project).

Into build\bin\win64 you will find snap7.dll and snap7.lib, the latter is the dynamic library import file to be used with C/C++ compilers (other languages don't need it).

Libstdc++ are statically linked, so you don’t need to distribute them with your software.

 

Remarks

Libraries produced by MinGW compilers works well in all editions of Windows, including Windows NT4.0 and Windows 2000 family.

 

Microsoft Visual Studio

There are four solutions ready into the folders build\windows, open them with the IDE of Visual Studio, choose Win32 or x64 in the combo box of the platform and build the solution.

Base libraries are statically linked, you don’t need to distribute Microsoft Visual C++ 20xx Redistributable with your application.

Into build\bin\win32 or build\bin\win64 you will find snap7.dll and snap7.lib, the latter is the dynamic library import file to be used with C/C++ compilers (other languages don't need it).

Visual Studio 2012/2013

All 32 bit DLLs (not only the snap7 one) generated by this compiler don’t work in Windows NT4.0/ 2000 / XP.
This because an (unrequested) reference to the GetTickCount64 function is generated.

This function is present in kernel32.dll starting from Vista.

To produce DLL compatible with Windows XP you need to install the Update 2 and choose into the project options Visual Studio 2012 - Windows XP (v110_xp) as platform set.

Visual Studio 2008 should not be suitable since it is not compliant with C99. I made a small patch in snap7_platform.h to include it since it is a very largely used compiler.

Visual Studio Express (2008-2013) is suitable but you need to download Windows Software Development Kit (SDK) to compile the 64 bit release of the library.

Finally, if you need the compatibility with NT 4.0 and Windows 2000 family, these compilers cannot be used at all.

 

Embarcadero C++ builder

Snap7 is compiled fine by this compiler but there is an issue : all the dynamic library import files generated (.lib) are not compatible with all the other Windows C/C++ compilers that use the Microsoft standard, included MinGW, and vice-versa.

Microsoft lib files are in coff format.

Borland lib files are in omf format.

In any case :

o   In the bin folder of C++Builder there is the utility coff2omf.exe that converts from coff to omf.

o   In the Microsoft masm package (V8+) there is the utility omf2coff.exe that converts omf to coff.

This issue only affects the .lib file, the dll generated works fine.

Finally, to produce 64 bit libraries, you need at least Embarcadero C++ Builder XE3 upd.1.

Currently there is no ready project for this compiler.

 

Unix

 

The libraries rebuilding is normal under Unix even though you don't modify the source code because Libgcc (the only dependence of snap7) changes accordingly to the OS/distribution.

If you experience an error regarding Libgcc or other libraries version with libsnap7.so released, don’t try strange alchemy (such as downloading them), Snap7 relies only on system libraries, your OS may malfunction if you replace them.

You have the full source code of Snap7 and to rebuild it is a very simple task, since all makefiles are ready.

The only requirement is that GNU ToolChain (g++ and Make) is present, and this is true for the main OS and distributions : BSD, Solaris, Linux (Debian, Ubuntu, Red Hat, SlackWare,  etc.), even small ARM cards that I tested had them.

Otherwise, you need to install them using the current package manager of your OS (apt, pkgtool, Yum, etc).

Anyhow, if you type “GNU ToolChain” followed by your OS name in Google, surely you will find very detailed information.

To know if the GNU Toolchain is correctly installed, open a terminal and enter:

g++ --version and make –-version.

You should see the compiler and make utility release.


 

Linux x86/x64

Open a terminal and go to build/unix, there type :

make –f <architecture>_linux.mk all to rebuild the library

make –f <architecture>_linux.mk clean to clean the project

make –f <architecture>_linux.mk install to rebuild and copy the library in usr/lib

for the third option you need be root or use sudo make …

Where <architecture> can be i386 or x86_64.

 

Let’s suppose that you have a 32 bit release of Ubuntu, you must type:

make –f i386_linux.mk all

In the folder bin/<architecture>-linux/  you will find libsnap7.so.

You need to copy it into /usr/lib (if you didn't use install switch) or set accordingly LD_PATH_LIBRARY.

From Version 1.2.1 there is an additional optional parameter that can be passed to the makefile that overrides the default target lib path  LibInstall=<path>

So, if you want to rebuild the library and copy it into /mylib instead of /usr/lib the above example becames:

make –f i386_linux.mk install LibInstall=/mylib

 

Warning

Some 64 bit distributions (CentOS and Red Hat) need libsnap7.so into /usr/lib64 instead of /usr/lib So you should use LibInstall=/usr/lib64

 

Linux Arm boards

There are two makefiles ready to use with single/dual-core ARM boards.

·       arm_v6_linux.mk for V6 ARMHF boards (like Raspberry PI)

·       arm_v7_linux.mk for V7 ARMHF boards (like BeagleBone Black, pcDuino and CubieBoard 2).

(They are a bit different, the second one needs the -mword-relocations switch)

As usually, in build/unix , type :

make –f arm_v6_linux.mk all (or clean or install)

or

make –f arm_v7_linux.mk all (or clean or install)

 

In the folder bin/arm_vX-linux/  you will find libsnap7.so.

You need to copy it into /usr/lib (if you didn't use install switch) or set accordingly LD_PATH_LIBRARY.

Remarks

The build process is quite slow on these boards cause the switch –pedantic that ensures the ISO compliance. You can disable it temporarily to speed up the process.

From Version 1.2.1 there is an additional optional parameter that can be passed to the makefile that overrides the default target lib path  LibInstall=<path>

So, if you want to rebuild the library and copy it into /mylib instead of /usr/lib the above example becames:

make –f arm_v6_linux.mk install LibInstall=/mylib

 

Linux Mips boards

Starting from 1.3.0 you will find the makefile mips_linux.mk into /build/unix folder.

As usually, in build/unix , type :

make –f mips_linux.mk all (or clean or install)

In the folder bin/mips-linux/  you will find libsnap7.so.

You need to copy it into /usr/lib or set accordingly LD_PATH_LIBRARY.

Remarks

The build process is quite slow on these boards cause the switch –pedantic that ensures the ISO compliance. You can disable it temporarily to speed up the process.

From Version 1.2.1 there is an additional optional parameter that can be passed to the makefile that overrides the default target lib path  LibInstall=<path>

So, if you want to rebuild the library and copy it into /mylib instead of /usr/lib the above example becames:

make –f mips_linux.mk install LibInstall=/mylib

 

OpenWRT crosscompile

Thanks to Fulvio Bosco and Stefano for this procedure.

1.   Install OpenWRT buildroot (Successfully Tested in Debian Wheezy 32 
bit architecture) following this :
http://wiki.openwrt.org/doc/howto/buildroot.exigence

2.   Build Snap7.

a.    Unpack snap7-full-x.y.z (starting from 1.3.0) into your /home/<user> folder, where <user> is the user logged name.

b.  Go to /snap7-full-x.y.z/build/unix and run
make –f mips_linux.mk all ToolPath=/home/<user>/openwrt/staging_dir/

c.  Copy libsnap7.so from /build/bin/mips-linux into your target hardware in /usr/lib

 

This procedure was tested on ARDUINO YUN board

To compile c snap7 examples directly on the Yun platform, expand yun disc with procedure described here:

http://arduino.cc/en/Tutorial/ExpandingYunDiskSpace

and follow this

http://playground.arduino.cc/Hardware/Yun#Compiling_on_the_Yun

You can compile client.c under your yun with:

gcc -Wall -o client client.c -lm -lpthread -ldl ./libsnap7.so


BSD

The working folder is, as usual, build/unix, but the command must be gmake instead of make:

gmake –f <architecture>_bsd.mk all (or clean or install).

Where <architecture> can be i386 or x86_64.

In the folder bin/<architecture>-bsd/  you will find libsnap7.so.

You need to copy it into /usr/lib (if you didn't use install switch) or set accordingly LD_PATH_LIBRARY.

Remarks

The resulting library is a native BSD library, not a binary compatible one.

From Version 1.2.1 there is an additional optional parameter that can be passed to the makefile that overrides the default target lib path  LibInstall=<path>

So, if you want to rebuild the library and copy it into /mylib instead of /usr/lib you should write:

gmake –f i386_bsd.mk install LibInstall=/mylib


 

Oracle Solaris 10/11 -  OpenIndiana 151a7

Starting from 1.3.0, thanks to Rolf Stalder, Snap7 can be rebuilt using either Oracle Solaris Studio 12.3 or GNU g++ using also pthreads.

Oracle Solaris Studio 12.3 can be downloaded from Here.

In this case, the apache stdcxx has to be installed.

The package is available in the Oracle IPS repository  Here

Simply install it with 'pkg install library/c++/stdcxx'

Two separate makefiles are provided for this purpose:

i386_solaris_cc.mk and x86_64_solaris_cc.mk for OSS.

i386_solaris_gcc.mk and x86_64_solaris_gcc.mk for GNU.

 

Use the makefile that you like:

gmake –f i386_solaris_xx.mk all (or clean or install)

or

gmake –f x86_64_solaris_xx.mk all (or clean or install)

Where xx = cc or gcc

As usual, in the folder /bin/<architecture>-solaris/ you will find libsnap7.so

Remarks

From Version 1.2.1 there is an additional optional parameter that can be passed to the makefile that overrides the default target lib path  LibInstall=<path>

So, if you want to rebuild the library and copy it into /mylib instead of /usr/lib you should write:

gmake –f i386_solaris_xx.mk install LibInstall=/mylib 

 

 Apple OSX

Apple OSX derived from BSD i.e. it belongs to the Unix family, however there is a separated folder to build it.

To rebuild Snap7, Xcode command line tools are needed.

From Xcode 4.3 they are not automatically installed with the main software and you need to download them.

It’s very simple, open a terminal and type:

xcode-select --install

If you already have Xcode you can press Install button. That’s all.

Descrizione: Descrizione: Descrizione: Descrizione: File:xcode select.png

Open a terminal and go to build/osx, there type :

make –f <architecture>_osx.mk all to rebuild the library

make –f <architecture>_osx.mk clean to clean the project

make –f <architecture>_osx.mk install to rebuild and copy the library in usr/lib

for the third option you need be root or use sudo make …

Where <architecture> can be i386 or x86_64.

In the folder /build/bin/<architecture>-osx/ you will find libsnap7.dylib

You need to copy it into /usr/lib or set accordingly LD_PATH_LIBRARY.

 

Remarks

 From Version 1.2.1 there is are two additional optional parameters that can be passed to the makefile

LibInstall=<path> that overrides the default target lib path

LibExt=<ext> that overrides the default library extension (dylib)

So, if you want to rebuild the library, copy it into /mylib instead of /usr/lib and have as extension .so instead of .dylib you should write:

make –f x86_64_osx.mk install LibInstall=/mylib LibExt=so