谁能帮我看下这程序错在哪
using System;using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using TwinCAT.Ads;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private int hbool1;
private int hint1;
private int hstr1;
private TcAdsClient adsClient;
private ArrayList notificationHandles;
private int hcomplexStruct;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
//read by handle
//the second parameter specifies the type of the variable
textBox1.Text = adsClient.ReadAny(hbool1, typeof(Boolean)).ToString();
textBox2.Text = adsClient.ReadAny(hint1, typeof(int)).ToString();
textBox3.Text = adsClient.ReadAny(hstr1, typeof(String), new int[] { 5 }).ToString();
FillStructControls((ComplexStruct)adsClient.ReadAny(hcomplexStruct, typeof(ComplexStruct)));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Form1_Load(object sender, EventArgs e)
{
adsClient = new TcAdsClient();
notificationHandles = new ArrayList();
try
{
adsClient.AdsNotificationEx += new AdsNotificationExEventHandler(adsClient_AdsNotificationEx);
button4.Enabled = false;
adsClient.Connect(801);
hbool1 = adsClient.CreateVariableHandle("MAIN.bool1");
hint1 = adsClient.CreateVariableHandle("MAIN.int1");
hstr1 = adsClient.CreateVariableHandle("MAIN.str1");
hcomplexStruct = adsClient.CreateVariableHandle("MAIN.ComplexStruct1");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void adsClient_AdsNotificationEx(object sender, AdsNotificationExEventArgs e)
{
TextBox textBox = (TextBox)e.UserData;
Type type = e.Value.GetType();
if (type == typeof(string) || type.IsPrimitive)
textBox.Text = e.Value.ToString();
else if (type == typeof(ComplexStruct))
FillStructControls((ComplexStruct)e.Value);
}
private void button2_Click(object sender, EventArgs e)
{
try
{
adsClient.WriteAny(hbool1, Boolean.Parse(textBox1.Text));
adsClient.WriteAny(hint1, byte.Parse(textBox2.Text));
adsClient.WriteAny(hstr1, textBox3.Text, new int[] { 5 });
adsClient.WriteAny(hcomplexStruct, GetStructFromControls());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
private void textBox3_TextChanged_1(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
notificationHandles.Clear();
try
{
//register notification
notificationHandles.Add(adsClient.AddDeviceNotificationEx("MAIN.int1", AdsTransMode.OnChange, 100, 0, textBox2, typeof(int)));
notificationHandles.Add(adsClient.AddDeviceNotificationEx("MAIN.bool1", AdsTransMode.OnChange, 100, 0, textBox1, typeof(Boolean)));
notificationHandles.Add(adsClient.AddDeviceNotificationEx("MAIN.str1", AdsTransMode.OnChange, 100, 0, textBox3, typeof(String), new int[] { 5 }));
notificationHandles.Add(adsClient.AddDeviceNotificationEx("MAIN.complexStruct1", AdsTransMode.OnChange, 100, 0, textBox6, typeof(ComplexStruct)));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
button4.Enabled = true;
button3.Enabled = false;
}
private void FillStructControls(ComplexStruct structure)
{
textBox4.Text= structure.boolVal.ToString();
textBox5.Text = structure.stringVal;
textBox6.Text = String.Format("{0:d}, {1:d}, {2:d}, {3:d}", structure.dintArr[0],
structure.dintArr[1], structure.dintArr[2], structure.dintArr[3]);
}
private ComplexStruct GetStructFromControls()
{
ComplexStruct structure = new ComplexStruct();
String[] stringArr = textBox6.Text.Split(new char[] { ',' });
for (int i = 0; i < stringArr.Length; i++)
structure.dintArr[i] = int.Parse(stringArr[i]);
structure.boolVal = Boolean.Parse(textBox4.Text);
structure.stringVal = textBox5.Text;
return structure;
}
private void button4_Click(object sender, EventArgs e)
{
//delete registered notifications.
try
{
foreach (int handle in notificationHandles)
adsClient.DeleteDeviceNotification(handle);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
notificationHandles.Clear();
button3.Enabled = true;
button4.Enabled = false;
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
private void textBox5_TextChanged(object sender, EventArgs e)
{
}
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public class ComplexStruct
{
public bool boolVal;
public string stringVal = "";
public int[] dintArr = new int[4];
}
} --------------------编程问答-------------------- 第一次见这么发帖的,忍不住吐糟一下。
哪里报错了不会截个图么?
错误现象能大概描述一下么?
上来就一堆不规范的代码,而且没有代码格式,谁会给你找错误......
TX的找茬,还搞几个半裸女的图... ... --------------------编程问答-------------------- 第一次见这么发帖的,忍不住吐糟一下。
哪里报错了不会截个图么?
错误现象能大概描述一下么?
上来就一堆不规范的代码,而且没有代码格式,谁会给你找错误......
TX的找茬,还搞几个半**裸**女的图... ... --------------------编程问答-------------------- 第一次见这么发帖的,忍不住吐糟一下。
哪里报错了不会截个图么?
错误现象能大概描述一下么?
上来就一堆不规范的代码,而且没有代码格式,谁会给你找错误......
TX的找茬,还搞几个半**裸**女的图... ... --------------------编程问答-------------------- 楼上淡定。。。
注意身体。。。 --------------------编程问答-------------------- using TwinCAT.Ads;
这个是么子。 --------------------编程问答-------------------- 截个图再放上来看看吧 , 你发那么多真的很难看得出 错在什么地方! --------------------编程问答-------------------- VS有那么强大的调试的机制,看看出错的提示不就行了,针对出错再解决 --------------------编程问答-------------------- 大哥。。连调试都不会呀,我那个去,C#调试最好玩了
补充:.NET技术 , C#