-> Hier kostenlos registrieren
Hallo Forum,
Ich habe folgendes Problem: Ich habe in Simulink ein Blockschaltbild in ein TcCom Modul gewandelt und in TwinCat 3 eingebunden. Nun möchte ich mit einer Windows Forms Anwendung auf die Ein-Ausgänge zugreifen.
Hier der C# Code der Anwendung.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using TwinCAT.Ads;
namespace HMI_ADS
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
adsClient = new TcAdsClient();
adsClient.AdsNotificationEx += new AdsNotificationExEventHandler(adsClient_AdsNotificationEx);
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
}
void adsClient_AdsNotificationEx(object sender, AdsNotificationExEventArgs e)
{
}
private void btn_verbinden_Click(object sender, EventArgs e)
{
AmsAddress serverAddress = null;
try
{
serverAddress = new AmsAddress(tb_idnet.Text, 350);
}
catch
{
MessageBox.Show("Invalid AMS NetId " + tb_idnet.Text);
return;
}
//connect
try
{
adsClient.Connect(serverAddress.NetId, serverAddress.Port);
symbolLoader = adsClient.CreateSymbolInfoLoader();
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
}
private void btn_schreiben_Click(object sender, EventArgs e)
{
ITcAdsSymbol currentSymbol = symbolLoader.FindSymbol("TcOutputBlocks.TCOutport");
string symbolvalue_string = adsClient.ReadSymbol(currentSymbol).ToString();
tb_status.Text = symbolvalue_string;
UInt16 symbolvalue_int = Convert.ToUInt16(symbolvalue_string);
ITcAdsSymbol2 currentSymbol2 = symbolLoader.FindSymbol("TcInputBlocks.Eingang1");
string symbolvalue2_string = tb_schreiben.Text;
adsClient.WriteSymbol(currentSymbol2, symbolvalue2_string);
}
}
}
Wenn ich den Button verbinden klicke bekomme ich noch keine Fehlermeldung , erst beim betätigen des Knopfes schreiben bekomme ich die Fehlermeldung:
Eine nicht behandelte Ausnahme des Typs "System.NullReferenceException" ist in TwinCAT.Ads.dll aufgetreten.
Zusätzliche Informationen: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.
Was mache ich falsch.
Hoffe jemand kann mir helfen.
Gruß
Frank
Ich habe folgendes Problem: Ich habe in Simulink ein Blockschaltbild in ein TcCom Modul gewandelt und in TwinCat 3 eingebunden. Nun möchte ich mit einer Windows Forms Anwendung auf die Ein-Ausgänge zugreifen.
Hier der C# Code der Anwendung.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using TwinCAT.Ads;
namespace HMI_ADS
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
adsClient = new TcAdsClient();
adsClient.AdsNotificationEx += new AdsNotificationExEventHandler(adsClient_AdsNotificationEx);
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
}
void adsClient_AdsNotificationEx(object sender, AdsNotificationExEventArgs e)
{
}
private void btn_verbinden_Click(object sender, EventArgs e)
{
AmsAddress serverAddress = null;
try
{
serverAddress = new AmsAddress(tb_idnet.Text, 350);
}
catch
{
MessageBox.Show("Invalid AMS NetId " + tb_idnet.Text);
return;
}
//connect
try
{
adsClient.Connect(serverAddress.NetId, serverAddress.Port);
symbolLoader = adsClient.CreateSymbolInfoLoader();
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
}
private void btn_schreiben_Click(object sender, EventArgs e)
{
ITcAdsSymbol currentSymbol = symbolLoader.FindSymbol("TcOutputBlocks.TCOutport");
string symbolvalue_string = adsClient.ReadSymbol(currentSymbol).ToString();
tb_status.Text = symbolvalue_string;
UInt16 symbolvalue_int = Convert.ToUInt16(symbolvalue_string);
ITcAdsSymbol2 currentSymbol2 = symbolLoader.FindSymbol("TcInputBlocks.Eingang1");
string symbolvalue2_string = tb_schreiben.Text;
adsClient.WriteSymbol(currentSymbol2, symbolvalue2_string);
}
}
}
Wenn ich den Button verbinden klicke bekomme ich noch keine Fehlermeldung , erst beim betätigen des Knopfes schreiben bekomme ich die Fehlermeldung:
Eine nicht behandelte Ausnahme des Typs "System.NullReferenceException" ist in TwinCAT.Ads.dll aufgetreten.
Zusätzliche Informationen: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.
Was mache ich falsch.
Hoffe jemand kann mir helfen.
Gruß
Frank