Paolo
Hi there,
I understand that I need to use an Arduino Uno (for example) to read the signal of my rotary encoder and use it as a modbus slave to send the value to the Unipi host.
My Arduino code work :
volatile unsigned long temp, counter = 0;  //This variable will increase or decrease depending on the rotation of encoder

void setup() {
  Serial.begin(9600);

  pinMode(2, INPUT_PULLUP);  // internal pullup input pin 2

  pinMode(3, INPUT_PULLUP);  // internalเป็น pullup input pin 3
                             //Setting up interrupt
  //A rising pulse from encodenren activated ai0(). AttachInterrupt 0 is DigitalPin nr 2 on moust Arduino.
  attachInterrupt(0, ai0, RISING);
}

void loop() {
  // Send the value of counter
  if (counter != temp) {
    Serial.println(counter);
    temp = counter;
  }
}

void ai0() {
  // ai0 is activated if DigitalPin nr 2 is going from LOW to HIGH
  // Check pin 3 to determine the direction
  if (digitalRead(3) == LOW) {
    counter++;
  } else {
    counter--;
  }
}
 
So I modify OpenPLC_Uno.ino like that :
//-----------------------------------------------------------------------------
// Copyright 2018 Thiago Alves
// 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 is the main file for the OpenPLC Arduino firmware. It contains the a
// Modbus RTU library to communciate with OpenPLC host as a slave device.
//
// Thiago Alves, Aug 2018
//-----------------------------------------------------------------------------

/******************PINOUT CONFIGURATION*******************

Digital In: 2, 3, 4, 5, 6           (%IX100.0 - %IX100.4)

Digital Out: 7, 8, 12, 13           (%QX100.0 - %QX100.3)

Analog In: A0, A1, A2, A3, A4, A5   (%IW100 - %IW105)

Analog Out: 9, 10, 11               (%QW100 - %QW102)

**********************************************************/

#include <Arduino.h>
#include "Modbus.h"
#include "ModbusSerial.h"

//ModBus Port information
#define BAUD 115200
#define ID 0
#define TXPIN -1

//Define the number of registers, inputs and coils to be created
#define NUM_DISCRETE_INPUT 5
#define NUM_INPUT_REGISTERS 6
#define NUM_COILS 4
#define NUM_HOLDING_REGISTERS 3

//Create the I/O pin masks
uint8_t pinMask_DIN[] = { 2, 3, 4, 5, 6 };
uint8_t pinMask_AIN[] = { A0, A1, A2, A3, A4, A5 };
uint8_t pinMask_DOUT[] = { 7, 8, 12, 13 };
uint8_t pinMask_AOUT[] = { 9, 10, 11 };

//Modbus Object
ModbusSerial modbus;

volatile unsigned int temp, counter = 0;  //This variable will increase or decrease depending on the rotation of encoder

void configurePins() {
  for (int i = 0; i < NUM_DISCRETE_INPUT; i++) {
    pinMode(pinMask_DIN[i], INPUT);
  }

  for (int i = 0; i < NUM_INPUT_REGISTERS; i++) {
    pinMode(pinMask_AIN[i], INPUT);
  }

  for (int i = 0; i < NUM_COILS; i++) {
    pinMode(pinMask_DOUT[i], OUTPUT);
  }

  for (int i = 0; i < NUM_HOLDING_REGISTERS; i++) {
    pinMode(pinMask_AOUT[i], OUTPUT);
  }
}

void setup() {
  //Setup board I/O
  configurePins();

  //Config Modbus Serial (port, speed, rs485 tx pin)
  modbus.config(&Serial, BAUD, TXPIN);

  //Set the Slave ID
  modbus.setSlaveId(ID);

  //Add all modbus registers
  for (int i = 0; i < NUM_DISCRETE_INPUT; ++i) {
    modbus.addIsts(i);
  }
  for (int i = 0; i < NUM_INPUT_REGISTERS; ++i) {
    modbus.addIreg(i);
  }
  for (int i = 0; i < NUM_COILS; ++i) {
    modbus.addCoil(i);
  }
  for (int i = 0; i < NUM_HOLDING_REGISTERS; ++i) {
    modbus.addHreg(i);
  }
  pinMode(2, INPUT_PULLUP);  // internal pullup input pin 2

  pinMode(3, INPUT_PULLUP);  // internal pullup input pin 3
                             //Setting up interrupt
  //A rising pulse from counter activated ai0(). AttachInterrupt 0 is DigitalPin nr 2 on most Arduino.
  attachInterrupt(0, ai0, RISING);

}

void loop() {
  //Run the main modbus task
  modbus.task();

  //Update modbus registers
  for (int i = 0; i < NUM_DISCRETE_INPUT; ++i) {
    modbus.Ists(i, digitalRead(pinMask_DIN[i]));
  }
  for (int i = 0; i < NUM_INPUT_REGISTERS; ++i) {
    if (i == 0)
      modbus.Ireg(i, counter);  //where counter is the variable that has the current count of the encoder
    else
      modbus.Ireg(i, (analogRead(pinMask_AIN[i]) * 64));
  }
  for (int i = 0; i < NUM_COILS; ++i) {
    digitalWrite(pinMask_DOUT[i], modbus.Coil(i));
  }
  for (int i = 0; i < NUM_HOLDING_REGISTERS; ++i) {
    analogWrite(pinMask_AOUT[i], (modbus.Hreg(i) / 256));
  }
  // Send the value of counter
  if (counter != temp) {
    temp = counter;
  }
}
void ai0() {
  // ai0 is activated if DigitalPin nr 2 is going from LOW to HIGH
  // Check pin 3 to determine the direction
  if (digitalRead(3) == LOW) {
    counter++;
  } else {
    counter--;
  }
}
I'm trying RMMS to read the value but I have error connection timed out.
 
Now how can I read %IW100.0 on my Unipi host ( I have already configured the slave device attached to the host )? What simple ladder diagram can I make ?
Thank you very much
Reply 0 0
Paolo
Find the solution by myself.
Just read %IW100 instead of %IW100.0
Reply 0 0
TheKevinShow13
Where did you find the .ino file? I can't seem to find it.
Reply 0 0
thiagoralves
That ino file is very outdated and was replaced almost 4 years ago by the micro runtime. You can make similar changes to the driver file of your platform of choice by editing the respective file in here:  https://github.com/thiagoralves/OpenPLC_Editor/tree/master/editor/arduino/src/hal
Reply 0 0
TheKevinShow13
That ino file is very outdated and was replaced almost 4 years ago by the micro runtime. You can make similar changes to the driver file of your platform of choice by editing the respective file in here:  https://github.com/thiagoralves/OpenPLC_Editor/tree/master/editor/arduino/src/hal
Gotcha. So if I were editing the uno file, where in the program would I put my encoder variable to appear in %IW100?
Reply 0 0
thiagoralves
IW100 is not in the range of an Arduino Uno. It is better for you to place it on IW0, which is *int_input[0] buffer. 
Reply 0 0
TheKevinShow13
Thank you! So what should that whole line of code look like? I'm not super experienced in C++. Would I change the value from the pin to the variable or is it a different line of code altogether?
Reply 0 0
Reply