Libnodave C Library

repository·master·Indexed 43 days ago

https://github.com/netdata-be/libnodave

A 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.

Tokens
4.4K
Snippets
17
Records
25
Agent score
55%

What's inside Libnodave

  1. Overview of Libnodave

    master
    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.
  2. Define the target OS for libnodave compilation

    master

    When 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., -DLINUX or -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"
  3. Handle Endianness for S7 communication

    master

    S7 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(), and daveGetFloat()), you must define DAVE_LITTLE_ENDIAN during 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"
  4. Interfacing with libnodave in other languages (e.g. VB)

    master

    When porting libnodave to other languages, you may find the daveInterface and daveConnection structures 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 daveInterface structure.

  5. Use Siemens S7 Online via CPs (Win32 only)

    master

    On 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 testS7online utility and specify the name of the "access point" (the one Step7 uses, usually configured in System Controls).

    testS7online /S7ONLINE
  6. Integrate libnodave with MS Excel via VBA

    master

    To use libnodave within Microsoft Excel, you must provide the libnodave.dll binary and import the provided VBA module.

    Setup Steps:

    1. DLL Placement: Copy libnodave.dll into either your active working directory or your system directory (e.g., C:\Windows\System32) so Excel can locate it.
    2. Import Module: Open a blank Excel workbook, open the VBA editor (Alt+F11), and import the Modul12.bas file.
    3. Initialize Spreadsheet: Run the macro initTable. This populates the spreadsheet with initial configuration values.
    4. Configure Parameters: Adjust the values generated by initTable in the spreadsheet to match your hardware requirements.
    5. Protocol Selection: If you are not using the MPI protocol, you must manually modify the code within the sub initialize routine 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
  7. Use libnodave interface declarations in VBA programs

    master
    The Modul12.bas file contains a specific section at the beginning that includes all necessary declarations to interface with libnodave.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.
  8. Requirements for porting libnodave to a new OS

    master

    To port libnodave to a new 32-bit operating system, you must satisfy the following requirements:

    1. Architecture: The OS must be 32-bit and the compiler must treat int as 32-bit.
    2. I/O Implementations: Provide functional replacements for:
      • setport.c or setportw.c (Serial port handling)
      • openSocket.c or openSocketw.c (TCP/IP handling)
    3. Header Definitions: In nodave.h, define a _daveOSserialType record 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).
    4. Core Logic: In nodave.c, implement the stdread() and stdwrite() functions to handle reading/writing bytes between a buffer and the serial port/socket.
  9. Install the Nodave Perl module

    master

    To 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 install
  10. Build libnodave.dll with BorlandC

    master

    To build the library using Borland's free command-line tools:

    1. Prerequisites: Install the free command-line tools from Borland.
    2. Makefile Selection: Use MAKEFILE.MAK instead of the standard Makefile. You can either delete the Linux Makefile or specify the file using make -f MAKEFILE.MAK.
    3. Build Targets:
      • Static Build: Run make with no target to build statically linked versions of the test programs and libnodave.dll.
      • Dynamic Build: Run make dynamic to build dynamically linked versions of the test programs and libnodave.dll.
    # To build dynamically linked versions using BorlandC
    make -f MAKEFILE.MAK dynamic
  11. Use Siemens USB-MPI-Adapters

    master

    Libnodave supports Siemens USB-MPI-Adapters via two methods:

    Under Linux, the kernel module maps the USB device to a serial port (e.g., /dev/ttyUSB0). You must use the -3 option with testMPI to select the specific envelope variant required by this device.

    testMPI -3 /dev/ttyUSB0

    2. libUSB (Userspace)

    If you cannot use the kernel module, use testUSB. This requires libUSB to handle the device from userspace.

    testUSB anyNonsense

    (Note: anyNonsense is a placeholder; the program will print usage hints if no argument is provided.)

  12. Use libnodave.dll with C#

    master

    C# implementations are available in the dot-net directory.

    • 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-net directory.