C
moin
Ich möchte eine kurze Zeitspanne in ms Sekunden messen. Dazu habe ich in VS2005 einen timer mit einem Intervall von 1 ms erstellt. Dieser erhöht dann eine Zählvariable um 1.
Das Problem ist das der Timer aber nicht startet.
Der andere timer in meinem Projekt startet einwandfrei.
Hier mal mein Code:
#pragma once
namespace com {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Summary for Form1
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
int time;
public:
Form1(void)
{
InitializeComponent();
serialPort1->Open();
time=0;
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::TextBox^ textBox1;
private: System::IO::Ports::SerialPort^ serialPort1;
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::Timer^ timer1;
private: System::Windows::Forms::Timer^ timer2;
private: System::ComponentModel::IContainer^ components;
protected:
private:
/// <summary>
/// Required designer variable.
/// </summary>
#pragma 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>
void InitializeComponent(void)
{
this->components = (gcnew System::ComponentModel::Container());
this->button1 = (gcnew System::Windows::Forms::Button());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->serialPort1 = (gcnew System::IO::Ports::SerialPort(this->components));
this->button2 = (gcnew System::Windows::Forms::Button());
this->timer1 = (gcnew System::Windows::Forms::Timer(this->components));
this->timer2 = (gcnew System::Windows::Forms::Timer(this->components));
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(12, 289);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(123, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"Check Connectivity";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point(13, 13);
this->textBox1->Multiline = true;
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(313, 256);
this->textBox1->TabIndex = 1;
//
// button2
//
this->button2->Location = System::Drawing::Point(142, 289);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(75, 23);
this->button2->TabIndex = 2;
this->button2->Text = L"Stop";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
//
// timer1
//
this->timer1->Interval = 5000;
this->timer1->Tick += gcnew System::EventHandler(this, &Form1::timer1_Tick);
//
// timer2
//
this->timer2->Interval = 1;
this->timer2->Tick += gcnew System::EventHandler(this, &Form1::timer2_Tick);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(338, 324);
this->Controls->Add(this->button2);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
timer1->Enabled=true;//Messung starten
textBox1->AppendText("Messung gestartet\n");
}
private: System::Void backgroundWorker1_DoWork(System::Object^ sender, System::ComponentModel::DoWorkEventArgs^ e) {
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
timer1->Enabled=false;//Messung stoppen
textBox1->AppendText("Messung beendet\n");
}
private: System::Void process1_Exited(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) {
bool dsr=false;
time=0;
serialPort1->DtrEnable=true;//BlackBox einschalten
timer2->Enabled=true;//ms zählen (timer2 startet aber nicht!)
while(dsr == false) {//warten bis Elko aufgeladen
//textBox1->AppendText(time.ToString());
//wenn elko aufgeladen
if(serialPort1->DsrHolding==true) {dsr=true;timer2->Enabled=false; textBox1->AppendText("DSR: On after "+time.ToString()+" ms\n");}
//wenn 2000 ms vorbei
if(time>2000) {dsr=true;timer2->Enabled=false;textBox1->AppendText("DSR: Off\n");}
}
serialPort1->DtrEnable=false;//BlackBox ausschalten
}
private: System::Void timer2_Tick(System::Object^ sender, System::EventArgs^ e) {
textBox1->AppendText("tick2");//debug ob timer2 startet
time++;
}
};
}
kurze Erklärung dazu:
Ich habe am ComPort eine kleine Schaltung hängen mit der mann eine Spannung bzw einen Widerstand über die Ladezeit eines Kondensators messen kann.
Im meinem Skript soll mir nur die Ladezeit des Kondesators in Millisekunden angezeigt werden. Dies funktioniert aber nicht, da der timer2 nicht startet.
danke
grüße