Hallo...
Im Anhang meiner Messkarte ist folgender Bsp.-Code für die Ansteuerung der Karte:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261:
| namespace WriteSingleValueAsVolts { public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Label label1; private System.Windows.Forms.StatusBar statusBar; private System.Windows.Forms.StatusBarPanel statusBarPanel; private System.Windows.Forms.Label label2; private System.Windows.Forms.ComboBox deviceComboBox; private System.Windows.Forms.GroupBox rawValueParamsGroupBox; private System.Windows.Forms.NumericUpDown channelNumericUpDown; private System.Windows.Forms.NumericUpDown rawValueNumericUpDown; private Device device; private DeviceMgr deviceMgr = DeviceMgr.Get(); private AnalogOutputSubsystem aoutSS; private System.Windows.Forms.Button writeSingleValueButton; private System.Windows.Forms.Label label3; private System.ComponentModel.Container components = null;
public Form1() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); }
#region Windows Form Designer generated code private void InitializeComponent() { System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1)); this.writeSingleValueButton = new System.Windows.Forms.Button(); this.deviceComboBox = new System.Windows.Forms.ComboBox(); this.label1 = new System.Windows.Forms.Label(); this.statusBar = new System.Windows.Forms.StatusBar(); this.statusBarPanel = new System.Windows.Forms.StatusBarPanel(); this.rawValueParamsGroupBox = new System.Windows.Forms.GroupBox(); this.label2 = new System.Windows.Forms.Label(); this.channelNumericUpDown = new System.Windows.Forms.NumericUpDown(); this.rawValueNumericUpDown = new System.Windows.Forms.NumericUpDown(); this.label3 = new System.Windows.Forms.Label(); ((System.ComponentModel.ISupportInitialize)(this.statusBarPanel)).BeginInit(); this.rawValueParamsGroupBox.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.channelNumericUpDown)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.rawValueNumericUpDown)).BeginInit(); this.SuspendLayout(); this.writeSingleValueButton.Location = new System.Drawing.Point(23, 128); this.writeSingleValueButton.Name = "writeSingleValueButton"; this.writeSingleValueButton.Size = new System.Drawing.Size(81, 32); this.writeSingleValueButton.TabIndex = 3; this.writeSingleValueButton.Text = "Write Voltage Value"; this.writeSingleValueButton.Click += new System.EventHandler(this.btnSetSingleValue_Click); this.deviceComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.deviceComboBox.Location = new System.Drawing.Point(80, 24); this.deviceComboBox.Name = "deviceComboBox"; this.deviceComboBox.Size = new System.Drawing.Size(96, 21); this.deviceComboBox.TabIndex = 67; this.deviceComboBox.SelectedIndexChanged += new System.EventHandler(this.deviceComboBox_SelectedIndexChanged); this.label1.Location = new System.Drawing.Point(8, 24); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(56, 21); this.label1.TabIndex = 66; this.label1.Text = "Device :"; this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; this.statusBar.Location = new System.Drawing.Point(0, 180); this.statusBar.Name = "statusBar"; this.statusBar.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] { this.statusBarPanel}); this.statusBar.ShowPanels = true; this.statusBar.Size = new System.Drawing.Size(246, 24); this.statusBar.SizingGrip = false; this.statusBar.TabIndex = 82; this.statusBarPanel.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Spring; this.statusBarPanel.Width = 246; this.rawValueParamsGroupBox.Controls.Add(this.label2); this.rawValueParamsGroupBox.Controls.Add(this.label1); this.rawValueParamsGroupBox.Controls.Add(this.deviceComboBox); this.rawValueParamsGroupBox.Controls.Add(this.channelNumericUpDown); this.rawValueParamsGroupBox.Location = new System.Drawing.Point(23, 16); this.rawValueParamsGroupBox.Name = "rawValueParamsGroupBox"; this.rawValueParamsGroupBox.Size = new System.Drawing.Size(192, 96); this.rawValueParamsGroupBox.TabIndex = 83; this.rawValueParamsGroupBox.TabStop = false; this.rawValueParamsGroupBox.Text = "Single Value Params"; this.label2.Location = new System.Drawing.Point(8, 56); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(56, 21); this.label2.TabIndex = 68; this.label2.Text = "Channel :"; this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; this.channelNumericUpDown.Location = new System.Drawing.Point(80, 56); this.channelNumericUpDown.Name = "channelNumericUpDown"; this.channelNumericUpDown.Size = new System.Drawing.Size(96, 20); this.channelNumericUpDown.TabIndex = 84; this.rawValueNumericUpDown.DecimalPlaces = 2; this.rawValueNumericUpDown.Location = new System.Drawing.Point(130, 136); this.rawValueNumericUpDown.Maximum = new System.Decimal(new int[] { 10, 0, 0, 0}); this.rawValueNumericUpDown.Minimum = new System.Decimal(new int[] { 10, 0, 0, -2147483648}); this.rawValueNumericUpDown.Name = "rawValueNumericUpDown"; this.rawValueNumericUpDown.Size = new System.Drawing.Size(54, 20); this.rawValueNumericUpDown.TabIndex = 87; this.label3.Location = new System.Drawing.Point(192, 136); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(32, 23); this.label3.TabIndex = 88; this.label3.Text = "Volts"; this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(246, 204); this.Controls.Add(this.label3); this.Controls.Add(this.rawValueNumericUpDown); this.Controls.Add(this.rawValueParamsGroupBox); this.Controls.Add(this.statusBar); this.Controls.Add(this.writeSingleValueButton); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D; this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.Name = "Form1"; this.Text = "WriteSingleValueAsVolts"; this.Load += new System.EventHandler(this.Form1_Load); ((System.ComponentModel.ISupportInitialize)(this.statusBarPanel)).EndInit(); this.rawValueParamsGroupBox.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.channelNumericUpDown)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.rawValueNumericUpDown)).EndInit(); this.ResumeLayout(false);
} #endregion
[STAThread] static void Main() { Application.Run(new Form1()); }
private void Form1_Load(object sender, System.EventArgs e) { string[] deviceNames = deviceMgr.GetDeviceNames(); for (int i=0; i<deviceNames.Length; ++i) deviceComboBox.Items.Add (deviceNames[i]); deviceComboBox.SelectedIndex = 0; }
private void btnSetSingleValue_Click(object sender, System.EventArgs e) { statusBarPanel.Text = "No Error"; try { int channel = Convert.ToInt32(channelNumericUpDown.Value); aoutSS.DataFlow = DataFlow.SingleValue; aoutSS.Config (); aoutSS.SetSingleValueAsVolts(channel, Convert.ToDouble(rawValueNumericUpDown.Value)); } catch (OlException ex) { statusBarPanel.Text = ex.Message; MessageBox.Show (ex.Message,"OlException"); } }
private void deviceComboBox_SelectedIndexChanged(object sender, System.EventArgs e) { if (device != null) device.Dispose(); string deviceName = (string)deviceComboBox.SelectedItem; device = deviceMgr.GetDevice (deviceName); aoutSS = device.AnalogOutputSubsystem (0); } } } |
Der funktioniert auch, wäre ja blöd wenn nicht.....
Habe mir dann für meine Testzwecke um die Karte und das Programm kennenzu lernen den Code hier zum geschrieben:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31:
| namespace WindowsApplication1 { public partial class Form1 : Form { private AnalogOutputSubsystem aoutSS;
public Form1() { InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e) { }
private void button1_Click(object sender, EventArgs e) {
int channel = 0;
aoutSS.DataFlow = DataFlow.SingleValue;
aoutSS.Config();
aoutSS.SetSingleValueAsVolts(channel, 5);
} } } |
So dies funktioniert aber nicht!!! Bekomme immer diesen Warnmeldung:
Warnung 1 Dem Feld "WindowsApplication1.Form1.aoutSS" wird nie etwas zugewiesen, und es hat immer seinen Standardwert von "null".
Das Programm lässt sich starten und wenn ich dann den button klicke um die Karte anzusteuern kommt der Fehler:
System.NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.
bei WindowsApplication1.Form1.button1_Click(Object sender, EventArgs e) in C:\Documents and Settings\diplhw1\Desktop\Test_CSharp_Messkarte\TestMesskarte\TestMesskarte\Form1.cs:Zeile 35.
Meine Frage wo und wie kann ich diesen NullReferenceException initalisieren oder beschreiben!!
Für euer mühen schon mal im Vorraus Danke!!!!
MfG
Frank
Moderiert von
Christian S.: C#-Tags hinzugefügt