RS232 auslesen mit VisualStudio2010-WindowsForms

Gast #2092236
Lesenswert?

Hallo zusammen,

Ich möchte gerne Daten anhand eines WindowsForms-Programm über den 
SerialPort auswerten. Bin leider ziemlich neu in diesem Bereich und 
benötige deshalb eure Hilfe. Wenn ich auf den Start-Button klicke, 
sollte die Schnittstelle die Daten im richTextBox1-Fenster anzeigen. 
Leider passiert gar nichts...

Hier mein bisheriger Code:
1
#pragma once
2

3
namespace Log_Programm {
4

5
  using namespace System;
6
  using namespace System::ComponentModel;
7
  using namespace System::Collections;
8
  using namespace System::Windows::Forms;
9
  using namespace System::Data;
10
  using namespace System::Drawing;
11

12
  /// <summary>
13
  /// Zusammenfassung für Seite
14
  /// </summary>
15
  public ref class Seite : public System::Windows::Forms::Form
16
  {
17
  public:
18
    Seite(void)
19
    {
20
      InitializeComponent();
21
      //
22
      //TODO: Konstruktorcode hier hinzufügen.
23
      //
24
    }
25

26
  protected:
27
    /// <summary>
28
    /// Verwendete Ressourcen bereinigen.
29
    /// </summary>
30
    ~Seite()
31
    {
32
      if (components)
33
      {
34
        delete components;
35
      }
36
    }
37
  private: System::Windows::Forms::RichTextBox^  richTextBox1;
38
  private: System::Windows::Forms::Button^  BEENDEN;
39
  public: System::IO::Ports::SerialPort^  serialPort1;
40
  private: System::Windows::Forms::Button^  START;
41
  public: 
42
  private: 
43

44
  private: System::ComponentModel::IContainer^  components;
45

46
  protected: 
47

48
  private:
49
    /// <summary>
50
    /// Erforderliche Designervariable.
51
    /// </summary>
52

53

54
#pragma region Windows Form Designer generated code
55
    /// <summary>
56
    /// Erforderliche Methode für die Designerunterstützung.
57
    /// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
58
    /// </summary>
59
    void InitializeComponent(void)
60
    {
61
      this->components = (gcnew System::ComponentModel::Container());
62
      this->richTextBox1 = (gcnew System::Windows::Forms::RichTextBox());
63
      this->BEENDEN = (gcnew System::Windows::Forms::Button());
64
      this->serialPort1 = (gcnew System::IO::Ports::SerialPort(this->components));
65
      this->START = (gcnew System::Windows::Forms::Button());
66
      this->SuspendLayout();
67
      // 
68
      // richTextBox1
69
      // 
70
      this->richTextBox1->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom) 
71
        | System::Windows::Forms::AnchorStyles::Left) 
72
        | System::Windows::Forms::AnchorStyles::Right));
73
      this->richTextBox1->Location = System::Drawing::Point(12, 12);
74
      this->richTextBox1->Name = L"richTextBox1";
75
      this->richTextBox1->Size = System::Drawing::Size(625, 408);
76
      this->richTextBox1->TabIndex = 0;
77
      this->richTextBox1->Text = L"";
78
      // 
79
      // BEENDEN
80
      // 
81
      this->BEENDEN->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Right));
82
      this->BEENDEN->BackColor = System::Drawing::Color::Red;
83
      this->BEENDEN->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 16.2F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
84
        static_cast<System::Byte>(0)));
85
      this->BEENDEN->Location = System::Drawing::Point(449, 346);
86
      this->BEENDEN->Name = L"BEENDEN";
87
      this->BEENDEN->Size = System::Drawing::Size(190, 74);
88
      this->BEENDEN->TabIndex = 1;
89
      this->BEENDEN->Text = L"BEENDEN";
90
      this->BEENDEN->UseVisualStyleBackColor = false;
91
      this->BEENDEN->Click += gcnew System::EventHandler(this, &Seite::BEENDEN_Click);
92
      // 
93
      // serialPort1
94
      // 
95
      this->serialPort1->DtrEnable = true;
96
      this->serialPort1->RtsEnable = true;
97
      this->serialPort1->PortName = "COM1";
98
            this->serialPort1->BaudRate = 9600;
99
            this->serialPort1->DataBits = 8;
100

101
      // 
102
      // START
103
      // 
104
      this->START->BackColor = System::Drawing::Color::Lime;
105
      this->START->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 16.2F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
106
        static_cast<System::Byte>(0)));
107
      this->START->Location = System::Drawing::Point(296, 346);
108
      this->START->Name = L"START";
109
      this->START->Size = System::Drawing::Size(147, 74);
110
      this->START->TabIndex = 4;
111
      this->START->Text = L"START";
112
      this->START->UseVisualStyleBackColor = false;
113
      this->START->Click += gcnew System::EventHandler(this, &Seite::START_Click);
114
      // 
115
      // Seite
116
      // 
117
      this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
118
      this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
119
      this->ClientSize = System::Drawing::Size(651, 432);
120
      this->Controls->Add(this->START);
121
      this->Controls->Add(this->BEENDEN);
122
      this->Controls->Add(this->richTextBox1);
123
      this->Name = L"Seite";
124
      this->Text = L"Seite";
125
      this->ResumeLayout(false);
126

127
    }
128
#pragma endregion
129

130
  private: System::Void BEENDEN_Click(System::Object^  sender, System::EventArgs^  e)
131
       {
132
         MessageBox::Show("Log-Program wird beendet");
133
       }
134

135

136
  private: System::Void START_Click(System::Object^  sender, System::EventArgs^  e)               {               
137
        serialPort1->Open();
138
        String^ s1 = serialPort1->ReadLine();
139
        richTextBox1->Text = s1;
140
        serialPort1->Close();
141
       }
142
};
143
}
#2092497
Lesenswert?

Für den Anfang:
ReadLine wartet auf zwei Sachen: Das Zeilenende und/oder ein Timeout 
(ReadTimeout/WriteTimeout sind beide normalerweise 
SerialPort.InfiniteTimeout). D.h. da wird kein Timeout kommen, also 
dürfte es am NewLine liegen (normalerweise ist das nur "\n", nicht 
"\r\n" oder "\r").

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren