Read analog value from arduino via modbus tcp

alverman
2017-10-11
2020-08-21
  • alverman - 2017-10-11

    Good evening,
    I have problems reading analog values from an arduin one connected in modbus tcp.
    Virtually I can control the status of inputs and outputs but no value comes from analog pins.
    I attach the arduino code and the codesys screen.

    Where am I wrong?

    Thank you, Alberto

    \#include <SPI.h>
    \#include <Ethernet.h>
    \#include "MgsModbus.h"
    MgsModbus Mb;
    int inByte = 0; // incoming serial byte
    // Ethernet settings (depending on MAC and Local network)
    byte mac[] = {0x90, 0xA2, 0xDA, 0x0E, 0x94, 0xB6 };
    IPAddress ip(10, 0, 0, 46);  // Unwrapper   192, 168, 0, 5
    IPAddress gateway(10, 0, 0, 1);
    IPAddress subnet(255, 255, 255, 0);
    //Configuration Digital IN
    int IN1 = 2;
    int IN2 = 3;
    int IN3 = 5;
    int IN4 = 6;
    //Configuration Digital OUT
    int OU1 = 7;
    int OU2 = 8;
    int OU3 = 9;
    //Configuration Analog IN
    int AN0 = A0;
    int AN1 = A1;
    int AN2 = A2;
    int AN3 = A3;
    int AN4 = A4;
    int AN5 = A5;
    void setup()
    {
      io_setup(); // I/O settings
      // serial setup
      Serial.begin(9600);
      Serial.println("Serial interface started");
      // initialize the ethernet device
      Ethernet.begin(mac, ip, gateway, subnet);   // start etehrnet interface
      Serial.println("Ethernet interface started"); 
      // print your local IP address:
      Serial.print("My IP address: ");
      for (byte thisByte = 0; thisByte < 4; thisByte++) {
        // print the value of each byte of the IP address:
        Serial.print(Ethernet.localIP()[thisByte], DEC);
        Serial.print("."); 
      }
      Serial.println();
    }
    void loop()
    {
      io_poll(); // I/O settings
      unsigned int j;
      //In monitor seriale inserire 10 per vedere lo stato dei bit
      if (Serial.available() > 0) {
        // get incoming byte:
        inByte = Serial.read();
        if (inByte == '0') {         // print MbData
         int i = 0;
         int j = 0;
         Serial.println();
          for (i=0;i<5;i++) { //Number of channel
            Serial.print("Mb.MbData["); Serial.print(i); Serial.print("]="); Serial.print(Mb.MbData[i]);
            Serial.print(" BIN: "); 
            for ( j=0;j<16;j++) { // Number of bit
                  Serial.print(bitRead(Mb.MbData[i], j));      
            }  
             Serial.println();
           }
           Serial.println();
        } 
     }
      Mb.MbsRun();
    }
    void io_poll() {
    //INPUT DIGITAL
        bitWrite(Mb.MbData[0], 0 , digitalRead(IN1));
        bitWrite(Mb.MbData[0], 1 , digitalRead(IN2));
        bitWrite(Mb.MbData[0], 2 , digitalRead(IN3));
        bitWrite(Mb.MbData[0], 3 , digitalRead(IN4));
    //OUTPUT DIGITAL
        digitalWrite(OU1, bitRead(Mb.MbData[1], 0));
        digitalWrite(OU2, bitRead(Mb.MbData[1], 1));
        digitalWrite(OU3, bitRead(Mb.MbData[1], 2));
    //INPUT ANALOG
        Serial.println (Mb.MbData[2]);
          
        bitWrite(Mb.MbData[2], 0 , analogRead(AN0 * 0.48875));
        bitWrite(Mb.MbData[2], 1 , analogRead(AN1));
        bitWrite(Mb.MbData[2], 2 , analogRead(AN2));
        bitWrite(Mb.MbData[2], 3 , analogRead(AN3));
        bitWrite(Mb.MbData[2], 4 , analogRead(AN4));
        bitWrite(Mb.MbData[2], 5 , analogRead(AN5));
    }
    void io_setup() {
     //Configuration Digital OUT
      pinMode(OU1, OUTPUT);  
      pinMode(OU2, OUTPUT);  
      pinMode(OU3, OUTPUT);  
      //Configuration Digital IN  
      pinMode(IN1, INPUT);     
      pinMode(IN2, INPUT);     
      pinMode(IN3, INPUT);     
      pinMode(IN4, INPUT);
      //Configuration Analog IN  
      pinMode(AN0, INPUT);     
      pinMode(AN1, INPUT);     
      pinMode(AN2, INPUT);
      pinMode(AN3, INPUT);  
      pinMode(AN4, INPUT);  
      pinMode(AN5, INPUT);
    }
    

    IMG: D3.png

     

    Related

    Talk.ru: 1
    Talk.ru: 2

  • alverman - 2017-10-14

    Nothing help ?

     
  • alverman - 2017-10-18

    Good morning,
    I did a simple program to figure out how to read an analog value from an arduino one connected to modbus tcp.
    Using a test program (jbus) to see the values that pass on the bus I can see the value sent but in codesys i do not see other than 0.

    Can you help me understand what's wrong?

    Thank you, Alberto

    IMG: D1.png

     
  • alverman - 2017-10-20

    Solved

     
  • lgmuss - 2020-08-08

    What was the problem? I am having similar issues...

     
  • i-campbell

    i-campbell - 2020-08-08

    From Alberto's screenshot, it looks like he had "always update variables" set to the default of disabled, and didn't use the values anywhere in the code. The hint is that the values appear grey in the Io mapping tab.

     
    • nothinrandom - 2020-08-21

      This is such a common issue that it rivals IT's "Have you tried turning it off and on again?"

       
      👍
      1
  • lgmuss - 2020-08-09

    Ahhh.. I feel like a fool! Yes that solved it! Thank you for your help!

     

Log in to post a comment.