PLC Variablen per C++ ADS API auslesen

LewAshby

Level-1
Beiträge
2
Reaktionspunkte
0
Zuviel Werbung?
-> Hier kostenlos registrieren
Hallo Leute

ich bin ganz neu was Beckhoff und EtherCat angeht. Ich habe nun TwinCAT3, eine Intel Netzwerkkarte, für welche ich dein TwinCat Treiber installiert habe und eine Ethercat Sklave Karte. Beide Karten sind über ein CAT6 Kabel verbunden. Wenn ich in Visual Studui 2012 nun ein neues TwinCAT Projekt erstelle, nach DEvices Scane, dann werden auch beide gefunden, ein Master und ein Slave. Soweit so gut.

Nun habe ich mir auf der Beckhoff Seite die Beispiele für die C++ Programmierung mit der ADS Api geladen und ausgeführt.
http://infosys.beckhoff.de/index.ph...tml/tcadsdll_api_cpp_sampleintro.htm&id=16480

Hier bekomme ich jedoch keine Verbindung zum Master und mir ist noch nicht klar wieso.
In Beispiel 9 wird die Variablen Deklaration des Masters ausgelesen:

// Read the length of the variable declaration
nErr = AdsSyncReadReq(pAddr, ADSIGRP_SYM_UPLOADINFO, 0x0, sizeof(tAdsSymbolUploadInfo), &tAdsSymbolUploadInfo);

Dies liefert jedoch Fehlercode 6 zurück.
Weiß jemand was ich falsch mache und wo es vielleicht eine genauere Anleitung gibt, wie man die Beispiele nutzt?
 
Zuviel Werbung?
-> Hier kostenlos registrieren
ethercat1.PNG

Ich habe einen Ethercat Master in TwinCat3 eingerichtet und nutze dafür meine Intel Netzwerkkarte. Siehe Screenshot. Zu diesem möchte ich mich gerne Verbinden. Wo seh ich denn die Portnummer?


Hier der Original Quellcode für das Beispiel von Beckhoff:
der Port Steht auf 851

Code:
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <assert.h>
#include "C:\TwinCAT3\AdsApi\TcAdsDll\Include\TcAdsDef.h"
#include "C:\TwinCAT3\AdsApi\TcAdsDll\Include\TcAdsAPI.h"


using namespace std;


void main()
{
  long                  nErr, nPort; 
  char                  *pchSymbols = NULL; 
  UINT                  uiIndex; 
  AmsAddr               Addr; 
  PAmsAddr              pAddr = &Addr; 
  AdsSymbolUploadInfo   tAdsSymbolUploadInfo; 
  PAdsSymbolEntry       pAdsSymbolEntry; 








  // Open communication port on the ADS router
  nPort = AdsPortOpen();
  nErr = AdsGetLocalAddress(pAddr);
  if (nErr) cerr << "Error: AdsGetLocalAddress: " << nErr << '\n';
  pAddr->port = 851;


  // Read the length of the variable declaration
  nErr = AdsSyncReadReq(pAddr, ADSIGRP_SYM_UPLOADINFO, 0x0, sizeof(tAdsSymbolUploadInfo), &tAdsSymbolUploadInfo);
  if (!nErr)
  {
      pchSymbols = new char[tAdsSymbolUploadInfo.nSymSize]; 
      assert(pchSymbols); 


      // Read information of the PLC-variable 
      nErr = AdsSyncReadReq(pAddr, ADSIGRP_SYM_UPLOAD, 0, tAdsSymbolUploadInfo.nSymSize, pchSymbols); 
      if (nErr) cerr << "Error: AdsSyncReadReq: " << nErr << '\n'; 


      // Print out the information of the PLC-variable 
      pAdsSymbolEntry = (PAdsSymbolEntry)pchSymbols; 
      for (uiIndex = 0; uiIndex < tAdsSymbolUploadInfo.nSymbols; uiIndex++)
      { 
        cout << PADSSYMBOLNAME(pAdsSymbolEntry) << "\t\t" 
             << pAdsSymbolEntry->iGroup << '\t' 
             << pAdsSymbolEntry->iOffs << '\t' 
             << pAdsSymbolEntry->size << '\t' 
             << PADSSYMBOLTYPE(pAdsSymbolEntry) << '\t' 
             << PADSSYMBOLCOMMENT(pAdsSymbolEntry) << '\n'; 
        pAdsSymbolEntry = PADSNEXTSYMBOLENTRY(pAdsSymbolEntry); cout.flush();
      }
  }
  else
  {
      cerr << "Error: AdsSyncReadReq: " << nErr << '\n'; 
  }
  getch();


  // Close the communication port 
  nErr = AdsPortClose(); 
  if (nErr) cerr << "Error: AdsPortClose: " << nErr << '\n';


  // Release the allocated memory
  if (pchSymbols) delete(pchSymbols);
}

Ich denke mir fehlen noch etwas die Grundlagen, aber es fällt auch schwer da einen Überblick zu bekommen. Die Beckhoff Seite ist etwas unübersichtlich was Doku angeht.
 
Hallo LewAshby,

der Port 851 steht für die erste PLC in TwinCAT3.

Du kannst nicht direkt vom EtherCAT Master Daten per ADS abholen, da dieser selbst bzw. den Feldbus ohne Task gar nicht aktualisiert.
Du musst zuvor unter System-Tasks eine Task mit Prozessabbild einfügen und dort Variablen für den Feldbus anlegen, die du dann verknüpfst.
Der Zugriff aus C++ findet dann auf den Port genau dieser Task statt.

Gruß,
mac203
 
Zurück
Oben