CODESYS - the IEC 61131-3 automation software

Welcome to the official CODESYS Forum
Deutsche Version English version russian version 
It is currently Sat Nov 18, 2017 5:35 am

All times are UTC+01:00




Post new topic  Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Wed Oct 11, 2017 4:55 pm 
Offline

Joined: Thu Feb 23, 2017 5:07 pm
Posts: 54
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

Code:
#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);
}


You do not have the required permissions to view the files attached to this post.


Top
   
PostPosted: Sat Oct 14, 2017 9:23 am 
Offline

Joined: Thu Feb 23, 2017 5:07 pm
Posts: 54
Nothing help ?


Top
   
PostPosted: Wed Oct 18, 2017 12:33 pm 
Offline

Joined: Thu Feb 23, 2017 5:07 pm
Posts: 54
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


You do not have the required permissions to view the files attached to this post.


Top
   
PostPosted: Fri Oct 20, 2017 7:59 pm 
Offline

Joined: Thu Feb 23, 2017 5:07 pm
Posts: 54
Solved :D


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 4 posts ] 

All times are UTC+01:00


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited