AddDeviceNotification in C#

awdkmfgefas

Level-1
Beiträge
5
Reaktionspunkte
0
Zuviel Werbung?
-> Hier kostenlos registrieren
Hallo
ich will in meinen Windows Forms Programm die Istposition einer Achse auslesen. Dies habe ich schon geschaft, aber denn Wert dauernd mit einer AddDeviceNotification zu Aktualisieren funktioniert nicht.

Mit diesem Code kann ich die Position auslesen, aber ich muss wenn ich die aktuelle Position wissen will den Button drücken
TcAdsClient tcAds;
private AdsStream ds;
private BinaryReader br;
tcAds = new TcAdsClient();
tcAds.Connect("127.0.0.1.1.1", 501);

private void button4_Click(object sender, EventArgs e)
{
ds = new AdsStream(20);
br = new BinaryReader(ds);
tcAds.Read(0x4101, 0x00010002, ds);
ds.Position = 0;
label2.Text = br.ReadDouble().ToString();
}

Danach habe ich den Code umgeändert, dieser sollte mittels einer Device Notification den Wert andauernd Aktualisieren, jedoch funktioniert es nicht.




TcAdsClient tcAds;
private AdsStream ds;
private BinaryReader br;
tcAds = new TcAdsClient();
tcAds.Connect("127.0.0.1.1.1", 501);

ds = new AdsStream(20);
br = new BinaryReader(ds);
int handle1 = tcAds.AddDeviceNotification(0x4101, 0x00010002, ds, 20, 0, AdsTransMode.OnChange, 100, 0, null);
tcAds.AdsNotification += new AdsNotificationEventHandler(OnNotification);



private void OnNotification(object sender, AdsNotificationEventArgs e)
{
ds.Position = 0;
ergebnis = br.ReadDouble().ToString();


this.Invoke((MethodInvoker)delegate
{
label3.Text = ergebnis;
});
}

Es würde mich freuen wenn mir bei meinen Problem wer helfen kann.
 
Zuviel Werbung?
-> Hier kostenlos registrieren
Nein ich habe ich an diesem Projekt Orientiert.
https://infosys.beckhoff.com/index.php?content=../content/1031/tcadsdevicesoverview/html/tcsystem_adsinterface.htm&id=4138897538473036370


Ich habe das selbe mit Variablen aus dem MAIN POU probiert und das Funktioniert aber

TcAdsClient tcAds;
private AdsStream ds;
private BinaryReader br;
tcAds = new TcAdsClient();
tcAds.Connect("127.0.0.1.1.1", 851);
int handle1 = tcAds.AddDeviceNotification("MAIN.xtest", ds, 0, 1, AdsTransMode.OnChange, 100, 0, null);
tcAds.AdsNotification += new AdsNotificationEventHandler(OnNotification);

private void OnNotification(object sender, AdsNotificationEventArgs e)
{
ds.Position = 0;
ergebnis = br.ReadBoolean().ToString();


this.Invoke((MethodInvoker)delegate
{
label3.Text = ergebnis;
});
}
 
Zuletzt bearbeitet:
... ist zwar schon was her, aber kannst Du nicht mal sagen, wie dein offSet und deine Länge dann waren als es ging? Das würde doch hilfreich sein für jemanden der auch so ein Problem hat!
VG
 
Zuviel Werbung?
-> Hier kostenlos registrieren
Als Tipp würde ich den tcAds.AddDeviceNotificationEx verwenden. Dann brauchst du keine AdsStream oder BinaryReader...

Zweitens würde ich die tcAds.AdsNotification += new AdsNotificationEventHandler(OnNotification); vor die AddDeviceNotification setzen.
Hintergrund ist der, wenn Variablenwerte selten oder nie in der SPS geändert werden, kann es sein, dass diese in C# nicht angezeigt werden.
 
Zurück
Oben