Autor Beitrag
grisu2611
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 28


C/C++; C# (neu)
BeitragVerfasst: Mi 05.09.07 19:14 
Hallo...
Im Anhang meiner Messkarte ist folgender Bsp.-Code für die Ansteuerung der Karte:

ausblenden volle Höhe C#-Quelltext
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
{
   
  /// <summary>
  /// Summary description for Form1.
  /// </summary>
  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;
    
    
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;
    

    public Form1()
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();
    }
    
        
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if (components != null
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }

    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    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();
      // 
      // writeSingleValueButton
      // 
      this.writeSingleValueButton.Location = new System.Drawing.Point(23128);
      this.writeSingleValueButton.Name = "writeSingleValueButton";
      this.writeSingleValueButton.Size = new System.Drawing.Size(8132);
      this.writeSingleValueButton.TabIndex = 3;
      this.writeSingleValueButton.Text = "Write Voltage Value";
      this.writeSingleValueButton.Click += new System.EventHandler(this.btnSetSingleValue_Click);
      // 
      // deviceComboBox
      // 
      this.deviceComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
      this.deviceComboBox.Location = new System.Drawing.Point(8024);
      this.deviceComboBox.Name = "deviceComboBox";
      this.deviceComboBox.Size = new System.Drawing.Size(9621);
      this.deviceComboBox.TabIndex = 67;
      this.deviceComboBox.SelectedIndexChanged += new System.EventHandler(this.deviceComboBox_SelectedIndexChanged);
      // 
      // label1
      // 
      this.label1.Location = new System.Drawing.Point(824);
      this.label1.Name = "label1";
      this.label1.Size = new System.Drawing.Size(5621);
      this.label1.TabIndex = 66;
      this.label1.Text = "Device :";
      this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
      // 
      // statusBar
      // 
      this.statusBar.Location = new System.Drawing.Point(0180);
      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(24624);
      this.statusBar.SizingGrip = false;
      this.statusBar.TabIndex = 82;
      // 
      // statusBarPanel
      // 
      this.statusBarPanel.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Spring;
      this.statusBarPanel.Width = 246;
      // 
      // rawValueParamsGroupBox
      // 
      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(2316);
      this.rawValueParamsGroupBox.Name = "rawValueParamsGroupBox";
      this.rawValueParamsGroupBox.Size = new System.Drawing.Size(19296);
      this.rawValueParamsGroupBox.TabIndex = 83;
      this.rawValueParamsGroupBox.TabStop = false;
      this.rawValueParamsGroupBox.Text = "Single Value Params";
      // 
      // label2
      // 
      this.label2.Location = new System.Drawing.Point(856);
      this.label2.Name = "label2";
      this.label2.Size = new System.Drawing.Size(5621);
      this.label2.TabIndex = 68;
      this.label2.Text = "Channel :";
      this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
      // 
      // channelNumericUpDown
      // 
      this.channelNumericUpDown.Location = new System.Drawing.Point(8056);
      this.channelNumericUpDown.Name = "channelNumericUpDown";
      this.channelNumericUpDown.Size = new System.Drawing.Size(9620);
      this.channelNumericUpDown.TabIndex = 84;
      // 
      // rawValueNumericUpDown
      // 
      this.rawValueNumericUpDown.DecimalPlaces = 2;
      this.rawValueNumericUpDown.Location = new System.Drawing.Point(130136);
      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(5420);
      this.rawValueNumericUpDown.TabIndex = 87;
      // 
      // label3
      // 
      this.label3.Location = new System.Drawing.Point(192136);
      this.label3.Name = "label3";
      this.label3.Size = new System.Drawing.Size(3223);
      this.label3.TabIndex = 88;
      this.label3.Text = "Volts";
      this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
      // 
      // Form1
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(513);
      this.ClientSize = new System.Drawing.Size(246204);
      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

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [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;
        
        // Configure the subsystem for single value operation
        aoutSS.Config ();
        
        // Send the specified value to the specified physical channel as a voltage
        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)
    {
      
      // Dispose the last selected device
      if (device != null)
        device.Dispose();
        
      string deviceName = (string)deviceComboBox.SelectedItem;  
      
      // Allocate the device with the specified device number
      device = deviceMgr.GetDevice (deviceName);
        
      // Allocate the first element of the analog output subsystem 
      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:

ausblenden volle Höhe C#-Quelltext
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 user profile iconChristian S.: C#-Tags hinzugefügt
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Mi 05.09.07 19:24 
Hallo!

Die Warnung gibt eigentlich schon einen guten Hinweis darauf, was schief geht: aoutSS ist ein Objekt. Das musst Du entweder erzeugen oder - wie in diesem Fall - erzeugen lassen. Im Beispiel wird das Objekt in der aller letzten Methode befüllt, so wirst Du das dann auch machen müssen.

Grüße
Christian

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
grisu2611 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 28


C/C++; C# (neu)
BeitragVerfasst: Di 11.09.07 17:16 
Hallo und Danke! Hab den Wald vor lauter Bäumen leider nicht gesehen! War im Endeffekt ganz einfach!

Jetzt aber das nächste Problem! Möchte gerne die deviceComboBox entfernen und habe das Programm so umgeschrieben:

ausblenden volle Höhe C#-Quelltext
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:
namespace WindowsApplication1
{
    public partial class Form1 : Form
    {

        private Device device;
        private DeviceMgr deviceMgr = DeviceMgr.Get ();
        private AnalogOutputSubsystem aoutSS;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {         
            int channel = 0;

            aoutSS.DataFlow = DataFlow.SingleValue;
                
            aoutSS.Config();

            aoutSS.SetSingleValueAsVolts(channel, 0);

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string [] dName = deviceMgr.GetDeviceNames();

            string deviceName = (string) dName;

            device =  deviceMgr.GetDevice(deviceName);

            aoutSS = device.AnalogOutputSubsystem(0);
            
        }
    }
}


Bekomme ich den Fehler:

Eine Konvertierung vom Typ "string[]" in "string" ist nicht möglich.

Auf die Zeile string deviceName = (string) dName bezogen!

Für euer hilfreiche Hinweise wäre ich dankbar!

Gruß Frank
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Di 11.09.07 17:19 
Du versuchst ein Array aus Strings in einen einzelnen String zu casten, das geht natürlich nicht. Was Du willst, ist wohl eher, den ersten Eintrag des Arrays (also mit Index 0) auszuwählen.

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
grisu2611 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 28


C/C++; C# (neu)
BeitragVerfasst: Di 11.09.07 18:03 
ja danke bin dahintergekommen!

ausblenden C#-Quelltext
1:
2:
3:
    string [] dName = deviceMgr.GetDeviceNames();

    device =  deviceMgr.GetDevice((string) dName [0]);


konnte es dann sogar so weit zusammekürzen!

Gruß Frank
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Di 11.09.07 18:12 
Das (string) brauchst Du nicht. dName ist ein String-Array, ein Element daraus ist also schon ein String.

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
grisu2611 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 28


C/C++; C# (neu)
BeitragVerfasst: Di 11.09.07 18:47 
danke man lernt nie aus.....bin ein quereinsteiger für ein paar wochen....mache ein schulprojekt für die technikerschule und werde später wieder in c/c++ programmieren! Bin also für jede Hilfe offen!