Vai al contenuto
PLC Forum


variabile 64bit tra s7-1200 e arduino con libreria settimino


baraz

Messaggi consigliati

Buongiorno,

sono a richiedere aiuto ai guru del forum ... mi spiego:

  • s7-1214 ac/dc/rly fw4.0
  • arduino eth     192.168.1.6

stavo scrivendo un programma per arduino utilizzando la libreria di settimino , programma compilato e caricato su arduino dice di trasferire al plc 192.168.1.36 una variabile da 64byte in DB1.array 6.0 , il programma di arduoino non da errori ma nel db del plc non riesco a far visualizzare la variabile.

Penso di sbagliare le dichiarazioni su DB1 ,qualcuno sa come posso risolvere il problema?non deve essere una array ? devono essere byte?

s7-db1.PNGs7-db1.PNGarduino.PNG

grazie in anticipo per il tempo speso .

 

Link al commento
Condividi su altri siti


Il tipo di variabile non ha importanza, per Arduino (Settimino o Snap7) è sempre vista come area contigua di byte, ad esempio una doppia word o un array di 4 byte sono la stessa cosa.

Quello che è importante (nel caso di S71200/1500) è che la DB non sia ottimizzata e che sia permesso l'accesso GET/PUT.

http://settimino.sourceforge.net/comp_1200.html

Puoi mostrare il pezzo di codice che utilizzi per scrivere ?

Link al commento
Condividi su altri siti

Risolto ora devo aggiungere altri dati il codice è questo......

#include <SPI.h>
#include <Ethernet.h>
#include "Settimino.h"
#include <Wire.h>
#include "Wire.h"
#include "SRF02.h"


/////////////////////////////////////////////////////////////////////////////////////////////////

#include <TinyGPS++.h>
#include <SoftwareSerial.h>
/*
   This sample code demonstrates the normal use of a TinyGPS++ (TinyGPSPlus) object.
   It requires the use of SoftwareSerial, and assumes that you have a
   4800-baud serial GPS device hooked up on pins 4(rx) and 3(tx).
*/
static const int RXPin = 3, TXPin = 4;
static const uint32_t GPSBaud = 115200;

// The TinyGPS++ object
TinyGPSPlus gps;

// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);

////////////////////////////////////////////////////////////////////////////////////////////////

#define ADDRESS 0x60 //defines address of compass

SRF02 sensori[2]={
  SRF02(0x70, SRF02_CENTIMETERS),
  SRF02(0x72, SRF02_CENTIMETERS),
};
unsigned long nextPrint = 0;

// Uncomment next line to perform small and fast data access
#define DO_IT_SMALL

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 
  0x90, 0xA2, 0xDA, 0x0F, 0x08, 0xE11 };
  
IPAddress Local(192,168,1,70); // Local Address
IPAddress PLC(192,168,1,36);   // PLC Address

byte Buffer[2];

S7Client Client;
unsigned long Elapsed; // To calc the execution time
//----------------------------------------------------------------------
// Setup : Init Ethernet and Serial port
//----------------------------------------------------------------------
void setup() {
    // Open serial communications and wait for port to open:
//    Serial.begin(9600);
     while (!Serial) {
      ; // wait for serial port to connect. Needed for Leonardo only
    }
    // Start the Ethernet Library
    Ethernet.begin(mac, Local);
    // Setup Time, someone said me to leave 2000 because some 
    // rubbish compatible boards are a bit deaf.
    delay(2000); 
    Serial.println(Ethernet.localIP());

      Wire.begin();
      SRF02::setInterval(200);

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  Serial.begin(115200);
  ss.begin(GPSBaud);

    
}
//----------------------------------------------------------------------
// Connects to the PLC
//----------------------------------------------------------------------
bool Connect()
{
    int Result=Client.ConnectTo(PLC, 
                                  0,  // Rack (see the doc.)
                                  0); // Slot (see the doc.)
    Serial.print("Connecting to ");Serial.println(PLC);  
    if (Result==0) 
    {
      Serial.print("Connected ! PDU Length = ");Serial.println(Client.GetPDULength());
    }
    else
      Serial.println("Connection error");
    return Result==0;
}
//----------------------------------------------------------------------
// Dumps a buffer (a very rough routine)
//----------------------------------------------------------------------
void Dump(void *Buffer, int Length)
{
  int i, cnt=0;
  pbyte buf;
  
  if (Buffer!=NULL)
    buf = pbyte(Buffer);
  else  
    buf = pbyte(&PDU.DATA[0]);

  Serial.print("[ Dumping ");Serial.print(Length);
  Serial.println(" bytes ]==========================");
  for (i=0; i<Length; i++)
  {
    cnt++;
    if (buf[i]<0x10)
      Serial.print("0");
    Serial.print(buf[i], HEX);
    Serial.print(" ");
    if (cnt==16)
    {
      cnt=0;
      Serial.println();
    }
  }  
  Serial.println("===============================================");
}
//----------------------------------------------------------------------
// Prints the Error number
//----------------------------------------------------------------------
void CheckError(int ErrNo)
{
  Serial.print("Error No. 0x");
  Serial.println(ErrNo, HEX);
  
  // Checks if it's a Severe Error => we need to disconnect
  if (ErrNo & 0x00FF)
  {
    Serial.println("SEVERE ERROR, disconnecting.");
    Client.Disconnect(); 
  }
}
//----------------------------------------------------------------------
// Profiling routines
//----------------------------------------------------------------------
void MarkTime()
{
  Elapsed=millis();
}
//----------------------------------------------------------------------
void ShowTime()
{
  // Calcs the time
  Elapsed=millis()-Elapsed;
  Serial.print("Job time (ms) : ");
  Serial.println(Elapsed);   
}
//----------------------------------------------------------------------
// Main Loop
//----------------------------------------------------------------------
void loop() 
{

 byte highByte;
  byte lowByte;
  
   Wire.beginTransmission(ADDRESS);      //starts communication with cmps03
   Wire.write(2);                         //Sends the register we wish to read
   Wire.endTransmission();

   Wire.requestFrom(ADDRESS, 2);        //requests high byte
   while(Wire.available() < 2);         //while there is a byte to receive
   highByte = Wire.read();           //reads the byte as an integer
   lowByte = Wire.read();
   int bearing = ((highByte<<8)+lowByte)/10; 
   
   Serial.println(bearing);
   delay(100);
  

  SRF02::update();
    if (millis() > nextPrint) {
      Serial.print("sensore 1 ");
      Serial.print(sensori[0].read());
      Serial.print("\n");
      Serial.print("sensore 2 ");
      Serial.print(sensori[1].read());
      Serial.print("\n");
      nextPrint = millis () + 1000;
      }

  
  int myval=sensori[0].read();
  int Size, Result;
  void *Target;

  int myval1=sensori[1].read();
  int Size1, Result1;
  void *Target1;

  int myval2=bearing;
  int Size2, Result2;
  void *Target2;

  int myval3=gps.location.lat();
  int Size3, Result3;
  void *Target3;

   
void* ptr = (void*)myval;
printf("%d",(int)ptr);

void* ptr1 = (void*)myval1;
printf("%d",(int)ptr1);  

void* ptr2 = (void*)myval2;
printf("%d",(int)ptr2);  

void* ptr3 = (void*)myval3;
printf("%d",(int)ptr3);
   
  
/*int Size, Result, Result1;
  void *Target;  */
   
#ifdef DO_IT_SMALL
  Size=2;
  Target = ptr; // Uses the internal Buffer (PDU.DATA[])
#else
  Size=1024;
  Target = &Buffer; // Uses a larger buffer
#endif

#ifdef DO_IT_SMALL
  Size1=2;
  Target1 = ptr1; // Uses the internal Buffer (PDU.DATA[])
#else
  Size=1024;
  Target1 = &Buffer; // Uses a larger buffer
#endif

#ifdef DO_IT_SMALL
  Size2=2;
  Target2 = ptr2; // Uses the internal Buffer (PDU.DATA[])
#else
  Size=1024;
  Target2 = &Buffer; // Uses a larger buffer
#endif

#ifdef DO_IT_SMALL
  Size3=64;
  Target3 = ptr3; // Uses the internal Buffer (PDU.DATA[])
#else
  Size=1024;
  Target3 = &Buffer; // Uses a larger buffer
#endif

  
  // Connection
  while (!Client.Connected)
  {
    if (!Connect())
     delay(200)
     ;
  }


//----------------------------------------------------------------------
// modifica void...
//----------------------------------------------------------------------
  
int VAR1 = sensori[0].read();
 
      byte* bytes = (byte*)&VAR1;

int VAR2 = sensori[1].read();
 
      byte* bytes1 = (byte*)&VAR2;

int VAR3 = bearing;
 
      byte* bytes2 = (byte*)&VAR3;


int VAR4 = gps.location.lat();
 
      byte* bytes3 = (byte*)&VAR4;
      
  
//----------------------------------------------------------------------
// Reverse word/int
//----------------------------------------------------------------------
void Reverse2(void *bytes);
{
  byte *pb;
  byte tmp;
  pb=(byte*)(bytes);
  // Swap byte 2 with byte 1
  tmp=*(pb+1);
  *(pb+1)=*pb;
  *pb=tmp;
 }  

 void Reverse21(void *bytes1);
{
  byte *pb1;
  byte tmp1;
  pb1=(byte*)(bytes1);
  // Swap byte 2 with byte 1
  tmp1=*(pb1+1);
  *(pb1+1)=*pb1;
  *pb1=tmp1;
 }  

  void Reverse22(void *bytes2);
{
  byte *pb2;
  byte tmp2;
  pb2=(byte*)(bytes2);
  // Swap byte 2 with byte 1
  tmp2=*(pb2+1);
  *(pb2+1)=*pb2;
  *pb2=tmp2;
 }  
  
//----------------------------------------------------------------------
// Serial check
//----------------------------------------------------------------------
 
  Serial.print("Write ");Serial.print(Size);Serial.println(" bytes from DB1.DBW0"); 
  // Put the current tick
  MarkTime();
  Result=Client.WriteArea(S7AreaDB, // We are requesting DB access
                         1,        // DB Number = 1
                         0,        // Start from byte N.0
                         Size,     // We need "Size" bytes
                         bytes);  // Put them into our target (Buffer or PDU)
  if (Result==0)
  {
    ShowTime();
    Dump(Target, Size);
    
    
  }
  else
    CheckError(Result);
    
  delay(100);  

  Serial.print("Write ");Serial.print(Size);Serial.println(" bytes from DB1.DBW2"); 
  // Put the current tick
  MarkTime();
  Result=Client.WriteArea(S7AreaDB, // We are requesting DB access
                         1,        // DB Number = 1
                         2,        // Start from byte N.0
                         Size1,     // We need "Size" bytes
                         bytes1);  // Put them into our target (Buffer or PDU)
  if (Result1==0)
  {
    ShowTime();
    Dump(Target1, Size1);
    
    
  }
  else
    CheckError(Result1);
    
  delay(100); 

    Serial.print("Write ");Serial.print(Size);Serial.println(" bytes from DB1.DBW4"); 
  // Put the current tick
  MarkTime();
  Result=Client.WriteArea(S7AreaDB, // We are requesting DB access
                         1,        // DB Number = 1
                         4,        // Start from byte N.0
                         Size2,     // We need "Size" bytes
                         bytes2);  // Put them into our target (Buffer or PDU)
  if (Result2==0)
  {
    ShowTime();
    Dump(Target2, Size2);
    
    
  }
  else
    CheckError(Result2);
    
  delay(100); 

      Serial.print("Write ");Serial.print(Size);Serial.println(" bytes from DB1.array 6.0"); 
  // Put the current tick
  MarkTime();
  Result=Client.WriteArea(S7AreaDB, // We are requesting DB access
                         1,        // DB Number = 1
                         6,        // Start from byte N.0
                         Size3,     // We need "Size" bytes
                         bytes3);  // Put them into our target (Buffer or PDU)
  if (Result3==0)
  {
    ShowTime();
    Dump(Target3, Size3);
    
    
  }
  else
    CheckError(Result3);
    
  delay(100); 
  
}

ora devo spostare altri dati a 64 bit... compreso data e ora :wallbash::wallbash:   vi terrò aggiornati.

Modificato: da baraz
Link al commento
Condividi su altri siti

Crea un account o accedi per commentare

Devi essere un utente per poter lasciare un commento

Crea un account

Registrati per un nuovo account nella nostra comunità. è facile!

Registra un nuovo account

Accedi

Hai già un account? Accedi qui.

Accedi ora
×
×
  • Crea nuovo/a...