Autor Beitrag
stormman
Hält's aus hier
Beiträge: 1



BeitragVerfasst: Fr 22.08.08 21:16 
Hallo zusammen!
Ich bin ein ziemlicher Neuling in c# und habe einige Fragen:
Das Ziel meines Programmes ist es, beim Drücken eines Buttons mit der seriellen Schnittstelle zu verbinden und die empfangenen Daten in einer Textbox darzustellen. Mit einem anderen Button wird die Verbindung geschlossen. Das Programm habe ich einmal in einer Konsolenanwendung geschrieben (Funktioniert ohne Probleme) und möchte dies in einer WindowsForm haben:

hier die Form1.Designer.cs:
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:
using System.IO.Ports;
using System.Text;
using System;
namespace RS232_win
{
    partial class Form1
    {
        SerialPort port = new SerialPort();
        /// <summary>
        /// Erforderliche Designervariable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
        public string test;
        /// <summary>
        /// Verwendete Ressourcen bereinigen.
        /// </summary>
        /// <param name="disposing">True, wenn verwaltete Ressourcen gelöscht werden sollen; andernfalls False.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Vom Windows Form-Designer generierter Code

        /// <summary>
        /// Erforderliche Methode für die Designerunterstützung.
        /// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
        /// </summary>
        private void InitializeComponent()
        {
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // textBox1
            // 
            this.textBox1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
            this.textBox1.Location = new System.Drawing.Point(7969);
            this.textBox1.Multiline = true;
            this.textBox1.Name = "textBox1";
            this.textBox1.ReadOnly = true;
            this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.textBox1.Size = new System.Drawing.Size(277208);
            this.textBox1.TabIndex = 0;
            this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
            // 
            // button1
            // 

            this.button1.Location = new System.Drawing.Point(67314);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(12228);
            this.button1.TabIndex = 1;
            this.button1.Text = "Connect";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(234314);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(12228);
            this.button2.TabIndex = 2;
            this.button2.Text = "Disconnect";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(9913);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(3513);
            this.label1.TabIndex = 3;
            this.label1.Text = "label1";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(454354);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.textBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.Activated += new System.EventHandler(this.Form1_Activated);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        public void teststart()
        {
            SerialPort port = new SerialPort();
            Form1 form1 = new Form1();
            port.PortName = "COM4";
            port.BaudRate = 38400;
            port.Parity = Parity.None;
            port.DataBits = 8;
            port.StopBits = StopBits.One;
            port.Encoding = Encoding.ASCII;
            port.DtrEnable = true;
            port.RtsEnable = true;
            port.ReceivedBytesThreshold = 20;
            port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
            port.Open();







            

        }
        public void testend()
        {
            port.Close();
        }
        public void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {

            textBox1.Text=port.ReadExisting();


        }

        #endregion

        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Label label1;
    }
}

und hier die Form1.cs:

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:
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.Ports;

namespace RS232_win
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            
        }

        
        private void button1_Click(object sender, EventArgs e)
        {
            teststart();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            testend();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void Form1_Activated(object sender, EventArgs e)
        {
            
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }
    }
}

Das Öffnen des Ports funktioniert ohne Probleme, wenn allerdings eine Nachricht empfangen wird kommt folgende Fehlermeldung:
InvalidOperationException: Der Anschluss ist geschlossen!

Nun noch weitere Fragen:
Wie kann ich die RS232-Methoden in eine andere Klasse auslagern?
Wie kann ich dann das Event SerialDataReceivedEvent in der Form1-Klasse auswerten?

Bitte helft mir weiter...

André

Moderiert von user profile iconChristian S.: C#-Tags hinzugefügt
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Fr 22.08.08 23:26 
:welcome:

user profile iconstormman hat folgendes geschrieben:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
        SerialPort port = new SerialPort(); // 1
[...]
        public void teststart()
        {
            SerialPort port = new SerialPort(); // 2
[...]
            port.Open(); // bezieht sich auf 2

Du hast innerhalb der Methode noch einmal eine gleichnamige lokale Variable festgelegt. Damit wird die eigentliche Instanz, die du dann auch im Eventhandler benutzt, nie geöffnet.

PS: Eigener Code hat in der Designer.cs nichts zu suchen, verschiebe ihn lieber in Form1.cs