Libnodave C Library
repository·master·Indexed 43 days ago
https://github.com/netdata-be/libnodaveA C library for connecting to and exchanging data with Siemens S7 300/400 PLCs, with extended support for S7 200 and S5 families. It includes support for Siemens USB-MPI-Adapters, a Nodave Perl module, and VBA integration for Microsoft Excel. The library supports multiple architectures including i386, ARM, and MIPS, and provides utilities for reading multiple variables in a single request using daveResultSet.
What's inside Libnodave
- Libnodave is a C library designed to facilitate communication with Siemens S7 300/400 PLCs. It provides the necessary functions to establish connections and exchange data with these controllers. Additionally, it supports the S7 200 family and the S5 family.
Define the target OS for libnodave compilation
masterWhen compiling source code that includes
nodave.h, you must specify the target operating system via a preprocessor definition. Failure to do so will result in the error:#error Fill in what you need for your OS or API.Use the following definitions:
LINUX: For Linux or other Unix-style operating systems.BCCWIN: For Windows.
You can pass these via compiler flags (e.g.,
-DLINUXor-DBCCWIN) or by defining them directly in your source code before including the headers.Warning: Defining these in your source code makes your code non-portable to other operating systems without modification.
#define LINUX // or #define BCCWIN for Windows #include "nodave.h" #include "setport.h" #include "openSocket.h"Handle Endianness for S7 communication
masterS7 uses Motorola (big endian, high byte first) byte order. Most Intel-based processors use little endian.
To ensure correct multibyte value conversion (used in functions like
daveGetS2(),daveGetU2(), anddaveGetFloat()), you must defineDAVE_LITTLE_ENDIANduring compilation. If this is not defined, libnodave assumes big endian and will not perform the necessary conversions on Intel machines.#define DAVE_LITTLE_ENDIAN #include "nodave.h"Interfacing with libnodave in other languages (e.g. VB)
masterWhen porting libnodave to other languages, you may find the
daveInterfaceanddaveConnectionstructures difficult to map.Key Concept: For 99% of applications, you do not need to map the internal member fields of these structures. Most fields are internal to libnodave or can be accessed via helper functions. Instead, you only need to maintain a pointer to the
daveInterfacestructure.Use Siemens S7 Online via CPs (Win32 only)
masterOn Windows, libnodave can communicate with S7 CPUs using existing Siemens libraries and drivers for transport. This requires the original Siemens drivers and DLLs to be installed.
To test this, use the
testS7onlineutility and specify the name of the "access point" (the one Step7 uses, usually configured in System Controls).testS7online /S7ONLINEIntegrate libnodave with MS Excel via VBA
masterTo use libnodave within Microsoft Excel, you must provide the
libnodave.dllbinary and import the provided VBA module.Setup Steps:
- DLL Placement: Copy
libnodave.dllinto either your active working directory or your system directory (e.g.,C:\Windows\System32) so Excel can locate it. - Import Module: Open a blank Excel workbook, open the VBA editor (Alt+F11), and import the
Modul12.basfile. - Initialize Spreadsheet: Run the macro
initTable. This populates the spreadsheet with initial configuration values. - Configure Parameters: Adjust the values generated by
initTablein the spreadsheet to match your hardware requirements. - Protocol Selection: If you are not using the MPI protocol, you must manually modify the code within the
sub initializeroutine in the VBA module as directed by the comments in that subroutine.
# Setup Summary: # 1. Copy libnodave.dll to work/system directory # 2. Import Modul12.bas into VBA editor # 3. Execute Macro: initTable # 4. Adjust spreadsheet values # 5. (Optional) Modify 'sub initialize' for non-MPI protocols- DLL Placement: Copy
Use libnodave interface declarations in VBA programs
masterTheModul12.basfile contains a specific section at the beginning that includes all necessary declarations to interface withlibnodave.dll. If you are building your own custom VBA application, you can copy this block of declarations and helper functions directly into your own project to enable communication with the libnodave library.Requirements for porting libnodave to a new OS
masterTo port libnodave to a new 32-bit operating system, you must satisfy the following requirements:
- Architecture: The OS must be 32-bit and the compiler must treat
intas 32-bit. - I/O Implementations: Provide functional replacements for:
setport.corsetportw.c(Serial port handling)openSocket.coropenSocketw.c(TCP/IP handling)
- Header Definitions: In
nodave.h, define a_daveOSserialTyperecord containing:rfd: A data type for read access (serial port or TCP socket).wfd: A data type for write access (serial port or TCP socket).
- Core Logic: In
nodave.c, implement thestdread()andstdwrite()functions to handle reading/writing bytes between a buffer and the serial port/socket.
- Architecture: The OS must be 32-bit and the compiler must treat
Install the Nodave Perl module
masterTo install the Nodave Perl module, use the standard Perl module installation sequence via
Makefile.PL. Ensure you have the necessary build tools (like a C compiler) available on your system before running these commands.perl Makefile.PL make make test make installBuild libnodave.dll with BorlandC
masterTo build the library using Borland's free command-line tools:
- Prerequisites: Install the free command-line tools from Borland.
- Makefile Selection: Use
MAKEFILE.MAKinstead of the standardMakefile. You can either delete the LinuxMakefileor specify the file usingmake -f MAKEFILE.MAK. - Build Targets:
- Static Build: Run
makewith no target to build statically linked versions of the test programs andlibnodave.dll. - Dynamic Build: Run
make dynamicto build dynamically linked versions of the test programs andlibnodave.dll.
- Static Build: Run
# To build dynamically linked versions using BorlandC make -f MAKEFILE.MAK dynamicUse Siemens USB-MPI-Adapters
masterLibnodave supports Siemens USB-MPI-Adapters via two methods:
1. Linux Kernel Module (Recommended)
Under Linux, the kernel module maps the USB device to a serial port (e.g.,
/dev/ttyUSB0). You must use the-3option withtestMPIto select the specific envelope variant required by this device.testMPI -3 /dev/ttyUSB02. libUSB (Userspace)
If you cannot use the kernel module, use
testUSB. This requireslibUSBto handle the device from userspace.testUSB anyNonsense(Note:
anyNonsenseis a placeholder; the program will print usage hints if no argument is provided.)Use libnodave.dll with C#
masterC# implementations are available in the
dot-netdirectory.- The provided test files (e.g.,
testMPI) have been verified to work with MONO 1.0.5 on Windows 2000. - For detailed instructions, refer to the specific README located within the
dot-netdirectory.
- The provided test files (e.g.,