Probleme mit Twincat TcADSDLL

domhol

Level-1
Beiträge
6
Reaktionspunkte
0
Zuviel Werbung?
-> Hier kostenlos registrieren
Hallo Forum,

ich bin angehender Elektrotechniker, und schreibe gerade meine Projektarbeit. In dieser Schreibe ich eine Visualisierung die später auf einem Pannel PC unter Windows CE von Beckhoff CP6608-0001 und der SPS CX9000-1001 laufen.

Ich versuch nun schon seit gut einer Woche mittels VB2008 und der TcADSDLL eine Verbindung zur Software SPS von Beckhoff herzustellen.
Um mich in die Programmierung der Visualisierung einzuarbeiten.

Die Beispiele von Beckhoff bringen mich nicht wirklich weiter da diese in VB6 geschreiben sind.

Im Forum habe ich einen Beitrag zu diesem Thema gefunden, aber ich kriege das Programm trotzdem nicht ans laufen.

Ich binde die tcadsdll über Verweise in das Projekt ein.

hier mal der Code den ich bis jetzt geschrieben habe.
Code:
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]
Option[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Explicit[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]On
Imports[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] TcAdsDll

[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Form1
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]WithEvents[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ads [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] TcAdsSync
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Declare[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] CopyMemory [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Lib[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"kernel32"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Alias[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"RtlMoveMemory"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] _
([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] lpDest [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] lpSrc [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Length [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Long[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])


[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Form1_Load([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Load
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]On[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Error[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]GoTo[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] errFunc
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] client [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] TcAdsDll.TcClient
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] netId [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] AmsNetId

client = CreateObject([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"TcAdsDll.TcClient"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])
netId.b(0) = 0
netId.b(1) = 0
netId.b(2) = 0
netId.b(3) = 0
netId.b(4) = 0
netId.b(5) = 0

ads = client.Connect(netId, 800)
ads.Timeout = 8000

[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Exit[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]errFunc:
MsgBox([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Error: (0x"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] & Format(Hex(Err.Number), [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"00000000"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]) & [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"), "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] & Err.Description)
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub

[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][/COLOR][/SIZE][/COLOR][/SIZE]

Wer kann mir helfen ich sehe den Fehler nicht.
Die SPS ist im Run Modus, und das Programm läuft.

Hoffe auf Hilfe

Gruß

DomHol
 
Ist die AMS Net ID von deiner CX wirklich 0.0.0.0.0.0 ?
Falls die Net ID falsch ist kann keine Verbindung aufgebaut werden. Vieleicht liegts daran?
 
Änder sich nix, habe es mit der ip, des Rechner probiert und mit localhost also 127.0.0.1
Aber hier ist mal die Fehlermeldung:
Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.

Erbemängelt nur noch das ich die b Variable der netid verwende bevor ich ihr einen Wert zuweise. Was ich nicht verstehe, im Code ist der Aufruf der NetId doch erst nach der Werte zuweisung.

Gruß
DomHol
 
Zuviel Werbung?
-> Hier kostenlos registrieren
die "b" Variable kommt aus dem Datentyp "AmsNetId", der in der DLL deklariert ist.
Ich sehe auch, dass du in deinem Beispielcode den Port "800" angibst, statt "801" für die SPS-Runtime.

Code:
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]
[COLOR=Black][...][/COLOR][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] client [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] TcAdsDll.TcClient
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [B]netId [/B][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [B]AmsNetId[/B]

client = CreateObject([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"TcAdsDll.TcClient"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])
netId.b(0) = 0
netId.b(1) = 0
netId.b(2) = 0
netId.b(3) = 0
netId.b(4) = 0
netId.b(5) = 0

ads = client.Connect(netId, [B]800[/B])
[...][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][/COLOR][/SIZE]

Zunächst schau mal auf die NetId deines PCs, auf dem die SPS läuft (In der Taskleiste auf das TwinCAT-Symbol und dort auf Eigenschaften => AMS Router).
Diese NetID trägst du in deinem Programm ein, sofern die SPS auch auf deinem lokalen PC läuft. Ansonsten die der Steuerung, sofern du schon eine "Route" zu diesem PC eingerichtet hast.
Steht da überall ne "0", dann trag irgend was ein, z.B. 192.168.1.1.1.1
(Beachte: 6 Stellen !!!). Die NetId hat KEINEN direkten Bezug zur IP-Adresse des PCs!
 
Hallo,

so hat etwas gedauert, aber jetzt ist das Thema mit der tcADSdll vom Tisch.
Ich nehme jetzt die .net dll.
Der Connect funktioniert auch ohne Probleme.
Jetzt hängt er an der Anbindung der Variablen der SPS.
Fehlercode : ADS-Error 0x710: Symbol could not be found
Hier mal ein VB-Code Beispiel:
Code:
[SIZE=2][COLOR=#0000ff]
[SIZE=2][COLOR=#0000ff]...[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] tcClient.IsConnected = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2]MsgBox([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Connect"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][SIZE=2]hblcmdstart = tcClient.AddDeviceNotification([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"MAIN.blcmdstart"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], dataStream, 0, 1, AdsTransMode.OnChange, 10, 0, DBNull.Value)[/SIZE]
[/SIZE]

der code mit blcmdstart klappt jetz, nur der unter noch nicht.
Code:
[SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000][COLOR=black]hblstart = tcClient.AddDeviceNotification("MAIN.blstart", dataStream, 1, 1, AdsTransMode.OnChange, 10, 0, DBNull.Value)[/COLOR][/COLOR][/SIZE][COLOR=#008000]
[/COLOR][/COLOR][/SIZE][/SIZE][SIZE=2]...[/SIZE]
und hier der SPS-Code
Code:
PROGRAM MAIN
VAR
 blcmdstart: BOOL;
 blstart:BOOL;
 END_VAR

Hat einer eine Idee????
Ich glaube ich gehe ins Bett, es ist schon wieder ziemlich spät geworden.

Gruß

DomHol
 
Also ich benutze c# und dort schaut der Code so aus....

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
using TwinCAT.Ads;

namespace ADS_Programm
{
public class ADS
{
#region Globale Variablen
int[] hConnectReadWrite, hConncetOnChange;
TcAdsClient tcClient;
Form frmForm1;
#endregion

#region Konstruktor
public ADS()
{
tcClient = null;
hConnectReadWrite = null;
hConncetOnChange = null;
frmForm1 = null;
}
#endregion

#region Init ADS
public void InitAds()
{
// Instanz der Klasse TcAdsClient erzeugen
tcClient = new TcAdsClient();

hConnectReadWrite = new int[1];
hConncetOnChange = new int[1];

try
{
// Verbindung mit Port 801 auf dem lokalen Computer herstellen
tcClient.Connect("192.168.0.10.1.1", 801);
tcClient.AdsNotificationEx += new AdsNotificationExEventHandler(tcClient_AdsNotificationEx);

//ReadWrite Variablen
hConnectReadWrite[0] = tcClient.CreateVariableHandle(".gISTWERT_0_bis_1150_Grad_KACHELOFEN");
//hConnectReadWrite[1] = tcClient.CreateVariableHandle("MAIN.intIstwertAchse");
//hConnectReadWrite[2] = tcClient.CreateVariableHandle("MAIN.dintSollwertAchse");
//hConnectReadWrite[3] = tcClient.CreateVariableHandle("MAIN.realDruck");
//hConnectReadWrite[4] = tcClient.CreateVariableHandle("MAIN.realGeschwindigkeit");

//OnChange Variablen
hConncetOnChange[0] = tcClient.AddDeviceNotificationEx(".iPT1000_SOLAR", AdsTransMode.OnChange, 100, 0, null, typeof(int));
//hConncetOnChange[1] = tcClient.AddDeviceNotificationEx("MAIN.intIstwertAchse", AdsTransMode.OnChange, 100, 0, null, typeof(short));
//hConncetOnChange[2] = tcClient.AddDeviceNotificationEx("MAIN.dintSollwertAchse", AdsTransMode.OnChange, 100, 0, null, typeof(int));
//hConncetOnChange[3] = tcClient.AddDeviceNotificationEx("MAIN.realDruck", AdsTransMode.OnChange, 100, 0, null, typeof(float));
//hConncetOnChange[4] = tcClient.AddDeviceNotificationEx("MAIN.realGeschwindigkeit", AdsTransMode.OnChange, 100, 0, null, typeof(float));
//hConncetOnChange[5] = tcClient.AddDeviceNotificationEx("MAIN.stringVal", AdsTransMode.OnChange, 100, 0, null, typeof(string), new int[] { 20 });
}
catch (Exception err)
{
MessageBox.Show(err.Message, ":::...InitAds meldet einen Fehler...:::",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
#endregion

Ich hoffe, dass ich dir helfen konnte

Gruß Martin
 
Vielen Dank,

so das erste problem habe ich mit eurer Hilfe gelöst!
Ich kann inzwischen auch Boolsche Variablen in die SPS schreiben und über die
AddDeviceNotification wird wird mir die änderung in VB angezeigt.
So weit so gut,
Boolsche Variablen schreibe ich mit
tcClient.WriteAny(hbldali, False)

Aber wie schreibe ich eine Integer Variable in die SPS?

egal was ich probiere ich kriege immer Fehler 0x705 raus.

Gruß

Dominic
 
Zuviel Werbung?
-> Hier kostenlos registrieren
public int Set_irgend_ein_Wert
{
set
{
try
{
tcClient.WriteAny(hConnectReadWrite[1], value);
}
catch (Exception err)
{
MessageBox.Show(err.Message, ":::...Set_irgend_ein_Wert meldet einen Fehler...:::",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}

So lautet bei mir der Code
 

egal was ich probiere ich kriege immer Fehler 0x705 raus.
Hi Dominic,

Error Code 0x705 bedeutet laut InfoSys "parameter size not correct"
Eine Integer in c# ist 32bit lang, eine INT Variable in TwinCAT hat 16bit. Versuche bitte deine c# integer in eine DINT Variable in TwinCAT zu schreiben, oder für eine INT in TwinCAT eine Int16 in C# zu nehmen.
War das die Ursache?

Gruß,
witkatz
 
Zuviel Werbung?
-> Hier kostenlos registrieren
Es würde auch einfach reichen eine UINT-datentyp zu verwenden. Ansonsten arbeite doch mit Merkern in der SPS. Dort kannst Du die Speicheradresse angeben, die Du nutzen willst. Für nicht BOOL Variablen (z.B. INT, UINT, DINT, REAL usw.) nutzt Du 0x4020 und für BOOL Variablen 0x4021.

Dann würde eine Zuweisung folgendermaßen aussehen:

TcClient.WriteAny(0x4021, 80, ckb_M_10_0.Checked);

Dort wird Merker 10.0 beschrieben bzw. über ReadAny ausgelesen. Sehr einfach und komfortabel zu programmieren.
Zu Deiner vorherigen Frage: Du solltest für die Localhost Adresse nicht 127.0.0.1.1.1 nehmen sondern wirklich die ID umbenennen in z.B. 172.16.17.100.1.1. Das Problem hatte ich auch schon einmal, dabei konnte ich von einem Windows CE Gerät nicht auf die Software SPS auf meinem Rechner zugreifen.

Gruß Triac
 
Zuletzt bearbeitet:
Hallo Forum,

Erstmal vielen Dank für eure Hilfe, dank euern Tips und Beispielen,
habe ich die Visu am laufen:)
Bis auf eine Kleinigkeit,
beim klicken auf eine Taste in der VISU soll eine Variable gesetzt werden
und beim loslassen wieder zurückgesetzt werden.
In der VISU von Twincat läuft der Taster, aber in VB kriege ich das Ding
nicht ans laufen;).

Weiß jemand einen Rat?

Gruß

DomHol
 
In der visu (VB) machst du das uber die Ereignisse der Maustaste mousdown und mouseup.

ganz einfach...:)

Leider hat nicht jedes Objekt diese Ereignisse :confused:

MfG CAS
 
Zurück
Oben