fg71
I'm trying to setup OpenPLC Runtime to use WiringPi
 
I could see from the compile_script that it use g++. I think its here I have problem.
Some say to include -lwiringPi to g++ flag, I have try but no difference.
 
Here is the error:
Generating C files...
POUS.c
POUS.h
LOCATED_VARIABLES.h
VARIABLES.csv
Config0.c
Config0.h
Res0.c
Moving Files...
Compiling for Raspberry Pi
Generating object files...
Generating glueVars...
varName: __QX0_2  varType: BOOL
Compiling main program...
/usr/bin/ld: /tmp/cc6qyXiI.o: in function `initializeHardware()':
hardware_layer.cpp:(.text+0x8): undefined reference to `wiringPiSetupGpio'
/usr/bin/ld: hardware_layer.cpp:(.text+0x28): undefined reference to `pinMode'
/usr/bin/ld: hardware_layer.cpp:(.text+0x58): undefined reference to `pullUpDnControl'
/usr/bin/ld: hardware_layer.cpp:(.text+0x90): undefined reference to `pinMode'
/usr/bin/ld: hardware_layer.cpp:(.text+0xc8): undefined reference to `pinMode'
/usr/bin/ld: /tmp/cc6qyXiI.o: in function `updateBuffersIn()':
hardware_layer.cpp:(.text+0x178): undefined reference to `digitalRead'
/usr/bin/ld: /tmp/cc6qyXiI.o: in function `updateBuffersOut()':
hardware_layer.cpp:(.text+0x2d0): undefined reference to `digitalWrite'
/usr/bin/ld: hardware_layer.cpp:(.text+0x340): undefined reference to `pwmWrite'
collect2: error: ld returned 1 exit status
Error compiling C files
Compilation finished with errors!
 
Here is the hardware_layer I have updated.
//-----------------------------------------------------------------------------
// Copyright 2015 Thiago Alves
//
// Based on the LDmicro software by Jonathan Westhues
// This file is part of the OpenPLC Software Stack.
//
// OpenPLC is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenPLC is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with OpenPLC.  If not, see <http://www.gnu.org/licenses/>.
//------
//
// This file is the hardware layer for the OpenPLC. If you change the platform
// where it is running, you may only need to change this file. All the I/O
// related stuff is here. Basically it provides functions to read and write
// to the OpenPLC internal buffers in order to update I/O state.
// Thiago Alves, Dec 2015
//-----------------------------------------------------------------------------

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <wiringPi.h>
#include <wiringSerial.h>
#include <pthread.h>

#include "ladder.h"

#if !defined(ARRAY_SIZE)
    #define ARRAY_SIZE(x) (sizeof((x)) / sizeof((x)[0]))
#endif

#define MAX_INPUT               14
#define MAX_OUTPUT              11
#define MAX_ANALOG_OUT  1

/********************I/O PINS CONFIGURATION*********************
* A good source for RaspberryPi I/O pins information is:
* http://pinout.xyz
*
* The buffers below works as an internal mask, so that the
* OpenPLC can access each pin sequentially
****************************************************************/
//inBufferPinMask: pin mask for each input, which
//means what pin is mapped to that OpenPLC input
int inBufferPinMask[MAX_INPUT] = { 2, 3, 4, 17, 27, 22, 10, 9, 11, 5, 6, 13, 19, 26 };

//outBufferPinMask: pin mask for each output, which
//means what pin is mapped to that OpenPLC output
int outBufferPinMask[MAX_OUTPUT] =      { 14, 15, 23, 24, 25, 8, 7, 12, 16, 20, 21 };

//analogOutBufferPinMask: pin mask for the analog PWM
//output of the RaspberryPi
int analogOutBufferPinMask[MAX_ANALOG_OUT] = { 18 };

//-----------------------------------------------------------------------------
// This function is called by the main OpenPLC routine when it is initializing.
// Hardware initialization procedures should be here.
//-----------------------------------------------------------------------------
void initializeHardware()
{

    wiringPiSetupGpio();

    //set pins as input
    for (int i = 0; i < MAX_INPUT; i++)
    {
        pinMode(inBufferPinMask[i], INPUT);
        if (i != 0 && i != 1) //pull down can't be enabled on the first two pins
        {
            pullUpDnControl(inBufferPinMask[i], PUD_DOWN); //pull down enabled
        }
    }

    //set pins as output
    for (int i = 0; i < MAX_OUTPUT; i++)
    {
        pinMode(outBufferPinMask[i], OUTPUT);
    }

    //set PWM pins as output
    for (int i = 0; i < MAX_ANALOG_OUT; i++)
    {
        pinMode(analogOutBufferPinMask[i], PWM_OUTPUT);
        // gpioSetPWMrange(analogOutBufferPinMask[i], 1024);
    }
}

//-----------------------------------------------------------------------------
// This function is called by the main OpenPLC routine when it is finalizing.
// Resource clearing procedures should be here.
//-----------------------------------------------------------------------------
void finalizeHardware()
{
    //gpioTerminate();
}

//-----------------------------------------------------------------------------
// This function is called by the OpenPLC in a loop. Here the internal buffers
// must be updated to reflect the actual state of the input pins. The mutex buffer_lock
// must be used to protect access to the buffers on a threaded environment.
//-----------------------------------------------------------------------------
void updateBuffersIn()
{
    pthread_mutex_lock(&bufferLock); //lock mutex

    //INPUT
    for (int i = 0; i < MAX_INPUT; i++)
    {
        if (bool_input[i/8][i%8] != NULL) *bool_input[i/8][i%8] = digitalRead(inBufferPinMask[i]);
    }

    pthread_mutex_unlock(&bufferLock); //unlock mutex
}

//-----------------------------------------------------------------------------
// This function is called by the OpenPLC in a loop. Here the internal buffers
// must be updated to reflect the actual state of the output pins. The mutex buffer_lock
// must be used to protect access to the buffers on a threaded environment.
//-----------------------------------------------------------------------------
void updateBuffersOut()
{
    pthread_mutex_lock(&bufferLock); //lock mutex

    //OUTPUT
    for (int i = 0; i < MAX_OUTPUT; i++)
    {
        if (bool_output[i/8][i%8] != NULL) digitalWrite(outBufferPinMask[i], *bool_output[i/8][i%8]);
    }

    //ANALOG OUT (PWM)
    for (int i = 0; i < MAX_ANALOG_OUT; i++)
    {
        if (int_output[i] != NULL) pwmWrite(analogOutBufferPinMask[i], (*int_output[i] / 64));
    }

    pthread_mutex_unlock(&bufferLock); //unlock mutex
}
 
Reply 0 0
thiagoralves
You need to change the compile_program.sh script (/webserver/scripts) to include the -lwiringPi argument, otherwise it will still try to include the pigpio library instead.
Reply 0 0
fg71
I did try to include it in the openplc_platform rpi, or are you saying its somewhere else ?
If I do cat openplc_platform it will return rpi so I thought is most be here.
 
If I do something like this it does compile
g++ -o example example.c -lwiringPi
 
Its very strange.
 
elif [ "$OPENPLC_PLATFORM" = "rpi" ]; then
    echo "Compiling for Raspberry Pi"
    echo "Generating object files..."
    if [ "$OPENPLC_DRIVER" = "sequent" ]; then
        g++ -std=gnu++11 -I ./lib -c Config0.c -lasiodnp3 -lasiopal -lopendnp3 -lopenpal -lwiringPi -w -DSEQUENT
    else
        g++ -std=gnu++11 -I ./lib -c Config0.c -lasiodnp3 -lasiopal -lopendnp3 -lopenpal -lwiringPi -w
    fi
 
 
I tested this code and it work, I used: g++ -o blink blink.c -lwiringPi
 
/*
* blink.c:
*  Standard "blink" program in wiringPi. Blinks an LED connected
*  to the first GPIO pin.
*
* Copyright (c) 2012-2013 Gordon Henderson.
***********************************************************************
* This file is part of wiringPi:
*      https://github.com/WiringPi/WiringPi
*
*    wiringPi is free software: you can redistribute it and/or modify
*    it under the terms of the GNU Lesser General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    wiringPi is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU Lesser General Public License for more details.
*
*    You should have received a copy of the GNU Lesser General Public License
*    along with wiringPi.  If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/

#include <stdio.h>
#include <wiringPi.h>

// LED Pin - wiringPi pin 0 is BCM_GPIO 17.

#define  LED  0

int main (void)
{
  printf ("Raspberry Pi blink\n") ;

  wiringPiSetup () ;
  pinMode (LED, OUTPUT) ;

  for (;;)
  {
    digitalWrite (LED, HIGH) ;  // On
    delay (500) ;    // mS
    digitalWrite (LED, LOW) ;  // Off
    delay (500) ;
  }
  return 0 ;
}
 
 
 
Reply 0 0
thiagoralves
You're adding it at the wrong part. This is the part where it compiles the MatIEC files. You must do this for the main program compilation, where -lpigpio currently is.
Reply 1 0
fg71
I could inform I have solve it, at least I got this. This time I read every line in compile_program.sh and did other change.
In other words it was not so simple as changing 1 line of code 😉 as I thought first.
 
Generating C files...
POUS.c
POUS.h
LOCATED_VARIABLES.h
VARIABLES.csv
Config0.c
Config0.h
Res0.c
Moving Files...
Compiling for Raspberry Pi
Generating object files...
Generating glueVars...
varName: __QX0_2  varType: BOOL
Compiling main program...
Compilation finished successfully!
 
Reply 0 0
fg71
main program compilation, where -lpigpio currently is.
I find it... thanks
Reply 0 0
fg71
Its looks like I'm able to compile program.st but I couldn't set the runtime in running mode.
Is there any logging to know more why is doesn't start??
 
Not sure what this means
OpenPLC Runtime starting...
Interactive Server: Listening on port 43628
Interactive Server: waiting for new client...
pinMode PWM: Unable to do this when using /dev/gpiomem. Try sudo?
Openning database
Enabling Modbus on port 502
Socket error during start_modbus(502), is the runtime active?
Disabling DNP3
Enabling EtherNet/IP on port 44818
Disabling Persistent Storage
persistent.file removed!
Name: blink_led
Location: %QX0.0
Type: BOOL
 
Reply 0 0
thiagoralves
Make sure your runtime is running as root (sudo ./start_openplc.sh)
Reply 1 0
fg71
That was it... the strange thing is I have set the PLC in running mode before without root.
It could be that when I changed to WiringPi the /dev/gpiomem need it
I could set pigpio to running as standard user.
 
As you know the reason I like to use WiringPi is to make Rpi5 to work. 🙂
 
Thanks, again.
Reply 0 0
Reply