#pragma once
#include "Form2.h"
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>

//#include "Header1.h"


namespace RS232Empang {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace System::IO::Ports;

	static unsigned char DatenVonSeiall[10];

	/// <summary>
	/// Zusammenfassung für Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{

		static	int Second = 0;
		static int Minute = 0;
		static int SekDiv10=0;
		String^ Sec;
		String^ Min;
		String^ dataInShow;
	    static  char* EmpfangsDatenPuffer=0;
	    static	unsigned long StelleInEmpfangsDatenPuffer = 0;;
		static char SchrittBeimZeichenEmpfang=0;
		static unsigned short ZeitBeimEmpfang = 0;
		static unsigned short StempelNullKomma1Sek;
		static unsigned short Stempel1Sek;
		static unsigned short Stempel1Min;
		static unsigned short ZeitZaehlerStempel1;
		static unsigned short ZeitZaehlerStempel2;
		//static unsigned char DatenEmpfangen = 0;
		static unsigned char Falle = 0;
		static unsigned char FalleGeschnapt = 1;
		static String^ AnKontrollBox = "";
		static char ReadActive = 0;
		static char PufferIstVoll = 0;

	private: System::Windows::Forms::ComboBox^  comboBox1;
	private: System::Windows::Forms::ComboBox^  comboBox2;
	private: System::Windows::Forms::ProgressBar^  progressBar1;
	private: System::Windows::Forms::GroupBox^  groupBox1;
	private: System::Windows::Forms::TextBox^  textBox1;
	private: System::Windows::Forms::Button^  button3;
	private: System::Windows::Forms::Button^  Recive;

	private: System::Windows::Forms::GroupBox^  groupBox2;
	public: System::Windows::Forms::TextBox^  textBox2;
	private:



	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::Label^  label2;

	private: System::Windows::Forms::Button^  button8;

	public: System::Windows::Forms::TextBox^  textBox3;
	private:

	private: System::Windows::Forms::Button^  button9;
	private: System::Windows::Forms::Button^  button5;
	private: System::Windows::Forms::CheckBox^  LF;

	private: System::Windows::Forms::CheckBox^  CR;


	public: System::Windows::Forms::TextBox^  textBox4;
	private: System::Windows::Forms::CheckBox^  TimeStamp;

	private: System::Windows::Forms::ComboBox^  StampZeit;
	private: System::Windows::Forms::CheckBox^  RTS;
	private: System::Windows::Forms::Button^  Clearing;
	private: System::Windows::Forms::Button^  TimerClr;

	private: System::Windows::Forms::Label^  label4;
	private: System::Windows::Forms::Button^  DisplayStopOn;
	public:
	private:


	public:
	private:



	private: System::Windows::Forms::Label^  label3;




	public:
		Form1(void)
		{
			InitializeComponent();
			findPorts();
			textBox4->Text = "10";
			Recive->Text = "Receive OFF";
			ReadActive = 0;
			DisplayStopOn->Text = "Display On";
			//StampZeit->Text ="1 Sek." ;
			//
			//TODO: Konstruktorcode hier hinzufügen.

			//
		}



	protected:
		/// <summary>
		/// Verwendete Ressourcen bereinigen.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Button^  PufferSave;
	protected:

	private: System::Windows::Forms::SaveFileDialog^  saveFileDialog1;
	private: System::Windows::Forms::OpenFileDialog^  openFileDialog1;
	private: System::Windows::Forms::Button^  FileOpen;

	private: System::Windows::Forms::Timer^  timer1;
	private: System::IO::Ports::SerialPort^  serialPort1;
	private: System::Windows::Forms::Label^  Time;

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

	protected:

	private:
		/// <summary>
		/// Erforderliche Designervariable.
		/// </summary>


#pragma region Windows Form Designer generated code
		/// <summary>
		/// Erforderliche Methode für die Designerunterstützung.
		/// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
		/// </summary>
		void InitializeComponent(void)
		{
			this->components = (gcnew System::ComponentModel::Container());
			this->PufferSave = (gcnew System::Windows::Forms::Button());
			this->saveFileDialog1 = (gcnew System::Windows::Forms::SaveFileDialog());
			this->openFileDialog1 = (gcnew System::Windows::Forms::OpenFileDialog());
			this->FileOpen = (gcnew System::Windows::Forms::Button());
			this->timer1 = (gcnew System::Windows::Forms::Timer(this->components));
			this->serialPort1 = (gcnew System::IO::Ports::SerialPort(this->components));
			this->Time = (gcnew System::Windows::Forms::Label());
			this->comboBox1 = (gcnew System::Windows::Forms::ComboBox());
			this->comboBox2 = (gcnew System::Windows::Forms::ComboBox());
			this->progressBar1 = (gcnew System::Windows::Forms::ProgressBar());
			this->groupBox1 = (gcnew System::Windows::Forms::GroupBox());
			this->textBox1 = (gcnew System::Windows::Forms::TextBox());
			this->button3 = (gcnew System::Windows::Forms::Button());
			this->Recive = (gcnew System::Windows::Forms::Button());
			this->groupBox2 = (gcnew System::Windows::Forms::GroupBox());
			this->DisplayStopOn = (gcnew System::Windows::Forms::Button());
			this->textBox2 = (gcnew System::Windows::Forms::TextBox());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->label3 = (gcnew System::Windows::Forms::Label());
			this->button8 = (gcnew System::Windows::Forms::Button());
			this->textBox3 = (gcnew System::Windows::Forms::TextBox());
			this->button9 = (gcnew System::Windows::Forms::Button());
			this->button5 = (gcnew System::Windows::Forms::Button());
			this->LF = (gcnew System::Windows::Forms::CheckBox());
			this->CR = (gcnew System::Windows::Forms::CheckBox());
			this->textBox4 = (gcnew System::Windows::Forms::TextBox());
			this->TimeStamp = (gcnew System::Windows::Forms::CheckBox());
			this->StampZeit = (gcnew System::Windows::Forms::ComboBox());
			this->RTS = (gcnew System::Windows::Forms::CheckBox());
			this->Clearing = (gcnew System::Windows::Forms::Button());
			this->TimerClr = (gcnew System::Windows::Forms::Button());
			this->label4 = (gcnew System::Windows::Forms::Label());
			this->groupBox1->SuspendLayout();
			this->groupBox2->SuspendLayout();
			this->SuspendLayout();
			// 
			// PufferSave
			// 
			this->PufferSave->Enabled = false;
			this->PufferSave->Location = System::Drawing::Point(280, 234);
			this->PufferSave->Name = L"PufferSave";
			this->PufferSave->Size = System::Drawing::Size(84, 38);
			this->PufferSave->TabIndex = 0;
			this->PufferSave->Text = L"Save Buffer";
			this->PufferSave->UseVisualStyleBackColor = true;
			this->PufferSave->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
			// 
			// openFileDialog1
			// 
			this->openFileDialog1->FileName = L"openFileDialog1";
			// 
			// FileOpen
			// 
			this->FileOpen->Location = System::Drawing::Point(630, 38);
			this->FileOpen->Name = L"FileOpen";
			this->FileOpen->Size = System::Drawing::Size(75, 23);
			this->FileOpen->TabIndex = 1;
			this->FileOpen->Text = L"FileOpen";
			this->FileOpen->UseVisualStyleBackColor = true;
			this->FileOpen->Visible = false;
			this->FileOpen->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
			// 
			// timer1
			// 
			this->timer1->Enabled = true;
			this->timer1->Tick += gcnew System::EventHandler(this, &Form1::timer1_Tick);
			// 
			// serialPort1
			// 
			this->serialPort1->ReadBufferSize = 2;
			this->serialPort1->ReadTimeout = 1;
			this->serialPort1->WriteBufferSize = 100;
			this->serialPort1->WriteTimeout = 1;
			// 
			// Time
			// 
			this->Time->AutoSize = true;
			this->Time->Font = (gcnew System::Drawing::Font(L"Courier New", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
				static_cast<System::Byte>(0)));
			this->Time->Location = System::Drawing::Point(66, 269);
			this->Time->Name = L"Time";
			this->Time->Size = System::Drawing::Size(38, 18);
			this->Time->TabIndex = 3;
			this->Time->Text = L"0:0";
			this->Time->TextAlign = System::Drawing::ContentAlignment::MiddleCenter;
			this->Time->Click += gcnew System::EventHandler(this, &Form1::Time_Click);
			// 
			// comboBox1
			// 
			this->comboBox1->FormattingEnabled = true;
			this->comboBox1->Location = System::Drawing::Point(45, 60);
			this->comboBox1->Name = L"comboBox1";
			this->comboBox1->Size = System::Drawing::Size(121, 21);
			this->comboBox1->TabIndex = 4;
			// 
			// comboBox2
			// 
			this->comboBox2->FormattingEnabled = true;
			this->comboBox2->Items->AddRange(gcnew cli::array< System::Object^  >(5) { L"9600", L"19200", L"4800", L"2400", L"115200" });
			this->comboBox2->Location = System::Drawing::Point(264, 64);
			this->comboBox2->Name = L"comboBox2";
			this->comboBox2->Size = System::Drawing::Size(121, 21);
			this->comboBox2->TabIndex = 5;
			// 
			// progressBar1
			// 
			this->progressBar1->Location = System::Drawing::Point(458, 60);
			this->progressBar1->Name = L"progressBar1";
			this->progressBar1->Size = System::Drawing::Size(100, 23);
			this->progressBar1->TabIndex = 6;
			// 
			// groupBox1
			// 
			this->groupBox1->Controls->Add(this->textBox1);
			this->groupBox1->Controls->Add(this->button3);
			this->groupBox1->Location = System::Drawing::Point(49, 87);
			this->groupBox1->Name = L"groupBox1";
			this->groupBox1->Size = System::Drawing::Size(128, 162);
			this->groupBox1->TabIndex = 7;
			this->groupBox1->TabStop = false;
			this->groupBox1->Text = L"SEND";
			// 
			// textBox1
			// 
			this->textBox1->Enabled = false;
			this->textBox1->Location = System::Drawing::Point(16, 21);
			this->textBox1->Multiline = true;
			this->textBox1->Name = L"textBox1";
			this->textBox1->Size = System::Drawing::Size(100, 109);
			this->textBox1->TabIndex = 0;
			// 
			// button3
			// 
			this->button3->Enabled = false;
			this->button3->Location = System::Drawing::Point(21, 136);
			this->button3->Name = L"button3";
			this->button3->Size = System::Drawing::Size(89, 23);
			this->button3->TabIndex = 8;
			this->button3->Text = L"Send";
			this->button3->UseVisualStyleBackColor = true;
			this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
			// 
			// Recive
			// 
			this->Recive->Enabled = false;
			this->Recive->Location = System::Drawing::Point(33, 133);
			this->Recive->Name = L"Recive";
			this->Recive->Size = System::Drawing::Size(89, 23);
			this->Recive->TabIndex = 100;
			this->Recive->Text = L"Receive On/Off";
			this->Recive->UseVisualStyleBackColor = true;
			this->Recive->Click += gcnew System::EventHandler(this, &Form1::button4_Click);
			// 
			// groupBox2
			// 
			this->groupBox2->Controls->Add(this->DisplayStopOn);
			this->groupBox2->Controls->Add(this->textBox2);
			this->groupBox2->Controls->Add(this->Recive);
			this->groupBox2->Location = System::Drawing::Point(370, 101);
			this->groupBox2->Name = L"groupBox2";
			this->groupBox2->Size = System::Drawing::Size(264, 162);
			this->groupBox2->TabIndex = 9;
			this->groupBox2->TabStop = false;
			this->groupBox2->Text = L"RECEIVE";
			// 
			// DisplayStopOn
			// 
			this->DisplayStopOn->Location = System::Drawing::Point(156, 134);
			this->DisplayStopOn->Name = L"DisplayStopOn";
			this->DisplayStopOn->Size = System::Drawing::Size(89, 23);
			this->DisplayStopOn->TabIndex = 108;
			this->DisplayStopOn->Text = L"Display Stop";
			this->DisplayStopOn->UseVisualStyleBackColor = true;
			this->DisplayStopOn->Click += gcnew System::EventHandler(this, &Form1::DisplayStopOn_Click);
			// 
			// textBox2
			// 
			this->textBox2->AcceptsReturn = true;
			this->textBox2->AcceptsTab = true;
			this->textBox2->AllowDrop = true;
			this->textBox2->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Right));
			this->textBox2->Enabled = false;
			this->textBox2->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12.5F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
				static_cast<System::Byte>(0)));
			this->textBox2->Location = System::Drawing::Point(6, 19);
			this->textBox2->Multiline = true;
			this->textBox2->Name = L"textBox2";
			this->textBox2->ReadOnly = true;
			this->textBox2->Size = System::Drawing::Size(252, 109);
			this->textBox2->TabIndex = 0;
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(42, 44);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(57, 13);
			this->label1->TabIndex = 13;
			this->label1->Text = L"Port Name";
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Location = System::Drawing::Point(240, 44);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(58, 13);
			this->label2->TabIndex = 14;
			this->label2->Text = L"Baud Rate";
			// 
			// label3
			// 
			this->label3->AutoSize = true;
			this->label3->Location = System::Drawing::Point(455, 44);
			this->label3->Name = L"label3";
			this->label3->Size = System::Drawing::Size(37, 13);
			this->label3->TabIndex = 15;
			this->label3->Text = L"Status";
			// 
			// button8
			// 
			this->button8->Location = System::Drawing::Point(640, 196);
			this->button8->Name = L"button8";
			this->button8->Size = System::Drawing::Size(75, 82);
			this->button8->TabIndex = 17;
			this->button8->Text = L"Open Port";
			this->button8->UseVisualStyleBackColor = true;
			this->button8->Click += gcnew System::EventHandler(this, &Form1::button8_Click);
			// 
			// textBox3
			// 
			this->textBox3->Location = System::Drawing::Point(36, 12);
			this->textBox3->Name = L"textBox3";
			this->textBox3->ReadOnly = true;
			this->textBox3->Size = System::Drawing::Size(691, 20);
			this->textBox3->TabIndex = 0;
			this->textBox3->TextChanged += gcnew System::EventHandler(this, &Form1::textBox3_TextChanged);
			// 
			// button9
			// 
			this->button9->Enabled = false;
			this->button9->Location = System::Drawing::Point(640, 76);
			this->button9->Name = L"button9";
			this->button9->Size = System::Drawing::Size(75, 82);
			this->button9->TabIndex = 20;
			this->button9->Text = L"Close Port";
			this->button9->UseVisualStyleBackColor = true;
			this->button9->Click += gcnew System::EventHandler(this, &Form1::button9_Click);
			// 
			// button5
			// 
			this->button5->Location = System::Drawing::Point(2, 10);
			this->button5->Name = L"button5";
			this->button5->Size = System::Drawing::Size(28, 23);
			this->button5->TabIndex = 21;
			this->button5->Text = L"Q:";
			this->button5->UseVisualStyleBackColor = true;
			this->button5->Click += gcnew System::EventHandler(this, &Form1::button5_Click);
			// 
			// LF
			// 
			this->LF->AutoSize = true;
			this->LF->Checked = true;
			this->LF->CheckState = System::Windows::Forms::CheckState::Checked;
			this->LF->Location = System::Drawing::Point(326, 186);
			this->LF->Name = L"LF";
			this->LF->Size = System::Drawing::Size(38, 17);
			this->LF->TabIndex = 22;
			this->LF->Text = L"LF";
			this->LF->UseVisualStyleBackColor = true;
			// 
			// CR
			// 
			this->CR->AutoSize = true;
			this->CR->Checked = true;
			this->CR->CheckState = System::Windows::Forms::CheckState::Checked;
			this->CR->Location = System::Drawing::Point(280, 186);
			this->CR->Name = L"CR";
			this->CR->Size = System::Drawing::Size(41, 17);
			this->CR->TabIndex = 23;
			this->CR->Text = L"CR";
			this->CR->UseVisualStyleBackColor = true;
			this->CR->CheckedChanged += gcnew System::EventHandler(this, &Form1::LF_CheckedChanged);
			// 
			// textBox4
			// 
			this->textBox4->Location = System::Drawing::Point(280, 160);
			this->textBox4->Name = L"textBox4";
			this->textBox4->Size = System::Drawing::Size(22, 20);
			this->textBox4->TabIndex = 24;
			this->textBox4->TextChanged += gcnew System::EventHandler(this, &Form1::textBox4_TextChanged);
			// 
			// TimeStamp
			// 
			this->TimeStamp->AutoSize = true;
			this->TimeStamp->Checked = true;
			this->TimeStamp->CheckState = System::Windows::Forms::CheckState::Checked;
			this->TimeStamp->Location = System::Drawing::Point(280, 209);
			this->TimeStamp->Name = L"TimeStamp";
			this->TimeStamp->Size = System::Drawing::Size(80, 17);
			this->TimeStamp->TabIndex = 101;
			this->TimeStamp->Text = L"Time stamp";
			this->TimeStamp->UseVisualStyleBackColor = true;
			// 
			// StampZeit
			// 
			this->StampZeit->Anchor = static_cast<System::Windows::Forms::AnchorStyles>(((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom)
				| System::Windows::Forms::AnchorStyles::Left));
			this->StampZeit->FormattingEnabled = true;
			this->StampZeit->Items->AddRange(gcnew cli::array< System::Object^  >(4) { L"1 Sek.", L"0.1 Sek.", L"1 Min.", L"DateTime" });
			this->StampZeit->Location = System::Drawing::Point(206, 205);
			this->StampZeit->Name = L"StampZeit";
			this->StampZeit->Size = System::Drawing::Size(70, 21);
			this->StampZeit->TabIndex = 102;
			this->StampZeit->Text = L"1 Sek.";
			// 
			// RTS
			// 
			this->RTS->AutoSize = true;
			this->RTS->Location = System::Drawing::Point(188, 64);
			this->RTS->Name = L"RTS";
			this->RTS->Size = System::Drawing::Size(48, 17);
			this->RTS->TabIndex = 103;
			this->RTS->Text = L"RTS";
			this->RTS->UseVisualStyleBackColor = true;
			// 
			// Clearing
			// 
			this->Clearing->Enabled = false;
			this->Clearing->Location = System::Drawing::Point(280, 107);
			this->Clearing->Name = L"Clearing";
			this->Clearing->Size = System::Drawing::Size(84, 38);
			this->Clearing->TabIndex = 104;
			this->Clearing->Text = L"Clear Buffer";
			this->Clearing->UseVisualStyleBackColor = true;
			this->Clearing->Click += gcnew System::EventHandler(this, &Form1::button4_Click_1);
			// 
			// TimerClr
			// 
			this->TimerClr->Location = System::Drawing::Point(110, 267);
			this->TimerClr->Name = L"TimerClr";
			this->TimerClr->Size = System::Drawing::Size(49, 23);
			this->TimerClr->TabIndex = 105;
			this->TimerClr->Text = L"<-Clear";
			this->TimerClr->TextAlign = System::Drawing::ContentAlignment::MiddleLeft;
			this->TimerClr->UseVisualStyleBackColor = true;
			this->TimerClr->Click += gcnew System::EventHandler(this, &Form1::TimerClr_Click);
			// 
			// label4
			// 
			this->label4->AutoSize = true;
			this->label4->Location = System::Drawing::Point(308, 163);
			this->label4->Name = L"label4";
			this->label4->Size = System::Drawing::Size(53, 13);
			this->label4->TabIndex = 107;
			this->label4->Text = L"Separator";
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(727, 302);
			this->Controls->Add(this->label4);
			this->Controls->Add(this->TimerClr);
			this->Controls->Add(this->Clearing);
			this->Controls->Add(this->RTS);
			this->Controls->Add(this->StampZeit);
			this->Controls->Add(this->TimeStamp);
			this->Controls->Add(this->textBox4);
			this->Controls->Add(this->CR);
			this->Controls->Add(this->LF);
			this->Controls->Add(this->button5);
			this->Controls->Add(this->button9);
			this->Controls->Add(this->textBox3);
			this->Controls->Add(this->button8);
			this->Controls->Add(this->label3);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->label1);
			this->Controls->Add(this->groupBox2);
			this->Controls->Add(this->groupBox1);
			this->Controls->Add(this->progressBar1);
			this->Controls->Add(this->comboBox2);
			this->Controls->Add(this->comboBox1);
			this->Controls->Add(this->Time);
			this->Controls->Add(this->FileOpen);
			this->Controls->Add(this->PufferSave);
			this->MaximizeBox = false;
			this->Name = L"Form1";
			this->Text = L"RS232 Serial Communikation";
			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
			this->groupBox1->ResumeLayout(false);
			this->groupBox1->PerformLayout();
			this->groupBox2->ResumeLayout(false);
			this->groupBox2->PerformLayout();
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion

		

	public: void findPorts(void) {
		//get port names
		array<Object^>^ objectArray = SerialPort::GetPortNames();
		//ad String array in ComBox
		this->comboBox1->Items->AddRange(objectArray);
	}


			
  
		

	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
		char TestV[3];
		TestV[0] = '0';
		TestV[1] = '1';
		TestV[2] = '1';
		if (!StelleInEmpfangsDatenPuffer)
		{
			textBox3->Text="Receiver Puffer is Empty";
		}
		else
		{


			// Set the properties on SaveFileDialog1 so the user is 
			// prompted to create the file if it doesn't exist 
			// or overwrite the file if it does exist.
			saveFileDialog1->CreatePrompt = true;
			saveFileDialog1->OverwritePrompt = true;

			// Set the file name to myText.txt, set the type filter
			// to text files, and set the initial directory to the
			// MyDocuments folder.
			saveFileDialog1->FileName = "";
			// DefaultExt is only used when "All files" is selected from 
			// the filter box and no extension is specified by the user.
			saveFileDialog1->DefaultExt = "txt";
			saveFileDialog1->Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
			saveFileDialog1->FilterIndex = 2;
			saveFileDialog1->RestoreDirectory = true;
			saveFileDialog1->Title = "Save  File";



			// Call ShowDialog and check for a return value of DialogResult.OK,
			// which indicates that the file was saved. 
			saveFileDialog1->ShowDialog();

			if (saveFileDialog1->FileName != "")
			{
				System::IO::FileStream ^ fs;
				fs = safe_cast<System::IO::FileStream^>(saveFileDialog1->OpenFile());
				if (fs)

				{
					//EmpfangsDatenPuffer[StelleInEmpfangsDatenPuffer]
					unsigned long i;
					unsigned char zz;
				/*	for (i = 0; i < StelleInEmpfangsDatenPuffer; ++i)
					{
						zz = EmpfangsDatenPuffer[i];
						//zz = 'A';
						fs->WriteByte(zz);
					}*/

					array< Byte >^ byteArray = gcnew array< Byte >(StelleInEmpfangsDatenPuffer + 4);
					//interior_ptr<Byte> pbArr2 = &byteArray[0];
					for (i = 0; i < StelleInEmpfangsDatenPuffer; ++i)
					{
						zz = EmpfangsDatenPuffer[i];
						byteArray[i] = zz;
					}
					fs->Write(byteArray, 0, StelleInEmpfangsDatenPuffer);
										
					delete(byteArray);

					// Set the stream position to the beginning of the file.
					 //fs->Seek( 1, SeekOrigin::Begin );

					fs->Close();

				}

			}


		}

		//END

	}
	private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {

		//button1->Text="Cipa";
		System::IO::FileStream ^ fs;
		openFileDialog1->FileName = "";
		openFileDialog1->Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
		openFileDialog1->FilterIndex = 2;
		openFileDialog1->RestoreDirectory = true;
		openFileDialog1->ShowDialog();

		if (openFileDialog1->FileName != "")
		{
			fs = safe_cast<System::IO::FileStream^>(openFileDialog1->OpenFile());

			if (fs != nullptr)
			{
				// Insert code to read the stream here.

				fs->Close();
			}
		}




		//END

	}
	private: System::Void button2_Click_1(System::Object^  sender, System::EventArgs^  e) {

		//timer1->Start();
		//timer1->Interval = 10;

	}
	private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
	}
	private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) {
		
		
		++StempelNullKomma1Sek;

		++ZeitZaehlerStempel1;
		if (ZeitZaehlerStempel1 >= 10)
		{
			ZeitZaehlerStempel1 = 0;
			++Stempel1Sek;
			++ZeitZaehlerStempel2;
   	    }

		if (ZeitZaehlerStempel2 >= 60)
		{
			ZeitZaehlerStempel2 = 0;
			++Stempel1Min;

		}
		
		SekDiv10++;
		if (SekDiv10 >= 10)
		{
			Second++;
			SekDiv10 = 0;
		}
		if (Second >= 60)
		{
			Second = 0;
			++Minute;
		}
		Sec = Convert::ToString(Second);
		Min = Convert::ToString(Minute);
		Time->Text = Min + ":" + Sec;
	}
			
	

	
	private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) {

		serialPort1->WriteLine(textBox1->Text);
		textBox1->Text = "";
	}
	private: System::Void button4_Click(System::Object^  sender, System::EventArgs^  e) {
		if (ReadActive)
		{
			ReadActive = 0;
			Recive->Text = "Receive OFF";
		}
		else
		{
			Recive->Text = "Receive ON";
			ReadActive = 1;
		}
		
		
	} 

		ref	class 
			   DatenVonSeriell {
		public: 	 unsigned char AnzahlByteImPuffer;
					 unsigned char ByteNr0;
					 unsigned char ByteNr1;
					 unsigned char ByteNr2;
					 unsigned char ByteNr3;

};

	

			 //Anfang
private: delegate void UpdateUiTextDelegate(DatenVonSeriell^ XX);
		 UpdateUiTextDelegate^  myDelegate;

	public: void AddDataMethod(DatenVonSeriell^ DatenVonRS232)
		

	{
		char *P;
		String^ kurz = "";
		String^ culture;
		unsigned char StrLang;
		char EmpfZeichen,ZeichenAnzahl;
		char ZeichenEnde = 10;
		char *PP;
		char CR_Zusatz;
		char CR_JA, LF_JA;
		char MitZeitStempel;
		char DatumUndZeit;
		if (TimeStamp->Checked)
			MitZeitStempel = 1;
		else
			MitZeitStempel = 0;
		if (PufferIstVoll)
		{
			textBox3->Text = "Buffer is full. Save Buffer";

		}
		else if (Recive->Text == "Receive ON")
		{
			Clearing->Enabled = true;
			PufferSave->Enabled = true;

			ZeichenEnde = Convert::ToByte(textBox4->Text);
			if (CR->Checked)
			{
		    	CR_JA = 1;
		     }else
				CR_JA = 0;
			 if (LF->Checked)
			 {
				 LF_JA = 1;
			 }
			 else
				 LF_JA = 0;

			
			
			DatenVonSeiall[0] = DatenVonRS232->ByteNr0;
			DatenVonSeiall[1] = DatenVonRS232->ByteNr1;
			DatenVonSeiall[2] = DatenVonRS232->ByteNr2;
			DatenVonSeiall[3] = DatenVonRS232->ByteNr3;
			ZeichenAnzahl = DatenVonRS232->AnzahlByteImPuffer;
			if (ZeichenAnzahl > 4)
				ZeichenAnzahl = 4;

			if (!StelleInEmpfangsDatenPuffer)
			{
				StempelNullKomma1Sek = 0;
				Stempel1Sek = 0;
				Stempel1Min = 0;
				ZeitZaehlerStempel1 = 0;
				ZeitZaehlerStempel2 = 0;
				ZeitBeimEmpfang = 0;
			}
			DatumUndZeit = 0;
			if (StampZeit->Text == "1 Sek.")
				ZeitBeimEmpfang = Stempel1Sek;
			else if (StampZeit->Text == "0.1 Sek.")
				ZeitBeimEmpfang = StempelNullKomma1Sek;
			else if (StampZeit->Text == "1 Min.")
				ZeitBeimEmpfang = Stempel1Min;
			else if (StampZeit->Text == "DateTime")
			{
				DatumUndZeit = 1;
				DateTime localDate = DateTime::Now;
				culture = Convert::ToString(localDate);
				ZeitBeimEmpfang = Stempel1Sek;
			}
				

				
			
			

			for (unsigned char i = 0; i < ZeichenAnzahl; ++i)
			{
				EmpfZeichen = DatenVonSeiall[i];
				/*
				if ((Falle == 0) && (!FalleGeschnapt))
				{
					FalleGeschnapt = 1;

					AnKontrollBox = Convert::ToString(EmpfZeichen);
					textBox2->Text = AnKontrollBox;
				}
				--Falle;*/


				switch (SchrittBeimZeichenEmpfang)
				{
				case 0://Zeit und ersteszeichen in Puffer schreiben
					if (MitZeitStempel)
					{
						
						if (DatumUndZeit)
						{
							unsigned char StingLang = culture->Length;
							array<wchar_t>^bytes = gcnew array< wchar_t >(StingLang);
							
							bytes = (culture->ToCharArray());
							unsigned char s,p;
							
							for(p = 0;p<StingLang;++p)
							{
							    	s = bytes[p];
									EmpfangsDatenPuffer[StelleInEmpfangsDatenPuffer] = s;
									++StelleInEmpfangsDatenPuffer;
								
								
							}
							delete(bytes);


							AnKontrollBox += culture;


						}
						else
						{
							P = _ultoa(ZeitBeimEmpfang, &(EmpfangsDatenPuffer[StelleInEmpfangsDatenPuffer]), 10);
							StrLang = strlen(P);
							AnKontrollBox = Convert::ToString(ZeitBeimEmpfang);
						}
						StelleInEmpfangsDatenPuffer += StrLang;
						EmpfangsDatenPuffer[StelleInEmpfangsDatenPuffer] = '>';
						++StelleInEmpfangsDatenPuffer;
						AnKontrollBox += ">";
					}
					/*else
						AnKontrollBox = "";
					EmpfangsDatenPuffer[StelleInEmpfangsDatenPuffer] = EmpfZeichen;
					++StelleInEmpfangsDatenPuffer;*/

					if (EmpfZeichen == ZeichenEnde)
					{
						if (StelleInEmpfangsDatenPuffer >= 5000000)
						{
							textBox3->Text = "Buffer is full. Save Buffer";
							PufferIstVoll = 1;
						}
						//AnKontrollBox += "";
						if (DisplayStopOn->Text == "Display On")
						{
							AnKontrollBox += Convert::ToString((wchar_t)13);
							AnKontrollBox += Convert::ToString((wchar_t)10);
							textBox2->Text = AnKontrollBox + textBox2->Text;
							AnKontrollBox = "";
						}

						if (CR_JA)
						{
							EmpfangsDatenPuffer[StelleInEmpfangsDatenPuffer] = 13;// ZeichenEnde;
							++StelleInEmpfangsDatenPuffer;
						}
						if (LF_JA)
						{
							EmpfangsDatenPuffer[StelleInEmpfangsDatenPuffer] = 10;// ZeichenEnde;
							++StelleInEmpfangsDatenPuffer;
						}

						if((!CR_JA)&& (!LF_JA))
						{
						EmpfangsDatenPuffer[StelleInEmpfangsDatenPuffer] = EmpfZeichen;// ZeichenEnde;
						++StelleInEmpfangsDatenPuffer;
						}
						
						/*if (CR_JA)
						{
							EmpfangsDatenPuffer[StelleInEmpfangsDatenPuffer] = 'A';// CR_Zusatz;
							++StelleInEmpfangsDatenPuffer;

						}*/

					}
					else
					{
						EmpfangsDatenPuffer[StelleInEmpfangsDatenPuffer] = EmpfZeichen;
						++StelleInEmpfangsDatenPuffer;
						AnKontrollBox += Convert::ToString((wchar_t)EmpfZeichen);
						++SchrittBeimZeichenEmpfang;

					}
					break;

				case 1:
					
					
					if (EmpfZeichen == ZeichenEnde)
					{
						if (StelleInEmpfangsDatenPuffer >= 5000000)
						{
							textBox3->Text = "Buffer is full. Save Buffer";
							PufferIstVoll = 1;
						}
						if (CR_JA)
						{
							EmpfangsDatenPuffer[StelleInEmpfangsDatenPuffer] = 13;// ZeichenEnde;
							++StelleInEmpfangsDatenPuffer;
						}
						if (LF_JA)
						{
							EmpfangsDatenPuffer[StelleInEmpfangsDatenPuffer] = 10;// ZeichenEnde;
							++StelleInEmpfangsDatenPuffer;
						}

						if ((!CR_JA) && (!LF_JA))
						{
							EmpfangsDatenPuffer[StelleInEmpfangsDatenPuffer] = EmpfZeichen;// ZeichenEnde;
							++StelleInEmpfangsDatenPuffer;
						}

						//AnKontrollBox += "";
						if (DisplayStopOn->Text == "Display On")
						{
							AnKontrollBox += Convert::ToString((wchar_t)13);
							AnKontrollBox += Convert::ToString((wchar_t)10);
							textBox2->Text = AnKontrollBox + textBox2->Text;
							AnKontrollBox = "";
						}
						--SchrittBeimZeichenEmpfang;
					}
					else
					{
						EmpfangsDatenPuffer[StelleInEmpfangsDatenPuffer] = EmpfZeichen;
						++StelleInEmpfangsDatenPuffer;
						AnKontrollBox += Convert::ToString((wchar_t)EmpfZeichen);
					}
					break;


				}

			}
		}
	}


	 //Ende

			

		
			 //using namespace System;
			 //using namespace System::IO::Ports;
			 ref class PortDataReceived
			 {
				static unsigned char *DateInP;
				static long int  BytesToRead;
				static TextBox^ DatenZiel ;
				static String^ indataX="";
				static UpdateUiTextDelegate^  MyDelegate;
				static DatenVonSeriell^ XX = gcnew DatenVonSeriell();
			 public:
				 
				 //static void Main(String^ PortName, unsigned long  BaudRate )
				static  void Main(unsigned char Aktion,SerialPort^ PortName, TextBox^ ddd, UpdateUiTextDelegate^  myDelegate)
				 {
					if(Aktion)
					{
						DatenZiel = ddd;
					MyDelegate = myDelegate;
					//DatenZiel->Text ="Test";
					SerialPort^ mySerialPort;
					mySerialPort = PortName;
					//SerialPort^ mySerialPort = gcnew SerialPort(PortName);
					//SerialPort^ = PortName;
					/*mySerialPort->BaudRate = BaudRate;
					mySerialPort->Parity = Parity::None;
					mySerialPort->StopBits = StopBits::One;
					mySerialPort->DataBits = 8;
					mySerialPort->Handshake = Handshake::None;
					mySerialPort->RtsEnable = true;*/
					// mySerialPort->ReadByte = 1;
					mySerialPort->DataReceived += gcnew SerialDataReceivedEventHandler(DataReceivedHandler);

					
					}
					else
					{
						SerialPort^ mySerialPort;
						mySerialPort = PortName;
						//delete mySerialPort->DataReceived;
						
					}
				 }
			 
				
			 public:
				 static  void DataReceivedHandler(
					 Object^ sender,
					 SerialDataReceivedEventArgs^ e)
				 {
					
					  String^ indata;
					  SerialPort^ sp = (SerialPort^)sender;
					 //String^ indata = sp->ReadExisting();
					 BytesToRead = sp->BytesToRead;
					 XX->AnzahlByteImPuffer= BytesToRead;
					 switch(BytesToRead)
					 {
					 case 1:
						 XX->ByteNr0 = (unsigned char)sp->ReadByte();
						 break;
					 case 2:
						 XX->ByteNr0 = (unsigned char)sp->ReadByte();
						 XX->ByteNr1 = (unsigned char)sp->ReadByte();
						 break;
					 case 3:
						 XX->ByteNr0 = (unsigned char)sp->ReadByte();
						 XX->ByteNr1 = (unsigned char)sp->ReadByte();
						 XX->ByteNr2 = (unsigned char)sp->ReadByte();
						 break;
					  case 4:
						 XX->ByteNr0 = (unsigned char)sp->ReadByte();
						 XX->ByteNr1 = (unsigned char)sp->ReadByte();
						 XX->ByteNr2 = (unsigned char)sp->ReadByte();
						 XX->ByteNr3 = (unsigned char)sp->ReadByte();
						 break;
					  default:
						  XX->ByteNr0 = (unsigned char)sp->ReadByte();
						  XX->ByteNr1 = (unsigned char)sp->ReadByte();
						  XX->ByteNr2 = (unsigned char)sp->ReadByte();
						  XX->ByteNr3 = (unsigned char)sp->ReadByte();
						  break;
					 }
					 
					  //XX->ByteNr0 =(unsigned char)sp->ReadByte();
					 //indata = sp->ReadExisting();
					 DatenZiel->Invoke(MyDelegate, XX);

					 										
				 }
			 };

  

			  
			 PortDataReceived^ SeriallSchnittStelle;
	

	
		         


			 //END*/




	private: System::Void button8_Click(System::Object^  sender, System::EventArgs^  e) {

			
		try
		{
			if ((comboBox1->Text == "") || (comboBox2->Text == ""))
			{
				//textBox2->Text = "Please select port setting";
				textBox3->Text = "Please select port setting";
			}
			else
			{
				
				
				serialPort1->PortName = comboBox1->Text;
				serialPort1->BaudRate = Convert::ToUInt32(comboBox2->Text); //String - Convert
				
				if (RTS->Checked)
					serialPort1->RtsEnable =true;
				else
					serialPort1->RtsEnable = false;

				serialPort1->Open();
				
				if (!EmpfangsDatenPuffer)
				{
					EmpfangsDatenPuffer = (char *)malloc(5001000);
					if (!EmpfangsDatenPuffer)
						textBox3->Text = "Memory no aloketed";
				}
				//EmpfangsDatenPuffer = 0;
				//StelleInEmpfangsDatenPuffer = 0;;
				SchrittBeimZeichenEmpfang = 0;
				


				myDelegate = gcnew UpdateUiTextDelegate(this, &Form1::AddDataMethod);
				//Invoke(myDelegate,12);
				//PortDataReceived^ SeriallSchnittStelle = gcnew PortDataReceived();
				SeriallSchnittStelle = gcnew PortDataReceived();
				//SeriallSchnittStelle->Main(serialPort1, textBox2);
				SeriallSchnittStelle->Main(1,serialPort1, textBox2, myDelegate);

				

				
				

				
				progressBar1->Value = 100;
				button3->Enabled = true;
				Recive->Enabled = true;
				textBox1->Enabled = true;
				button8->Enabled = false;
				button9->Enabled = true;

				/*
				     String^ MyString1 = "  ";
					 String ^ a = gcnew String("abc");
					 String ^ b = "def";   // same as gcnew form  
					 Object ^ c = gcnew String("ghi");
				
				
				
				*/

				

			}
		} 
		//catch (const UnauthorizedAccessException^ e)
		catch (System::UnauthorizedAccessException^)//(const UnauthorizedAccessException^)
		{
			textBox3->Text = "Unauthorized Access";
		}

	
}






private: System::Void button7_Click(System::Object^  sender, System::EventArgs^  e) {

	

}
private: System::Void button9_Click(System::Object^  sender, System::EventArgs^  e) {

	//serialPort1->PortName = comboBox1->Text;
	//serialPort1->BaudRate = Convert::ToUInt32(comboBox2->Text); //String - Convert
	serialPort1->Close();

	 
	SeriallSchnittStelle->Main(0,serialPort1, textBox2, myDelegate);
	delete SeriallSchnittStelle;
	delete myDelegate;

	progressBar1->Value = 0;
	button3->Enabled = false;
	Recive->Enabled = false;
	button9->Enabled = false;
	button8->Enabled = true;
	textBox1->Enabled = false;
	ReadActive = 0;
	Recive->Text = "Receive OFF";

}
private: System::Void textBox3_TextChanged(System::Object^  sender, System::EventArgs^  e) {
	//textBox3->Text = "";
}
private: System::Void button5_Click(System::Object^  sender, System::EventArgs^  e) {
	textBox3->Text = "";
}
private: System::Void textBox4_TextChanged(System::Object^  sender, System::EventArgs^  e) {
}
private: System::Void LF_CheckedChanged(System::Object^  sender, System::EventArgs^  e) {
}
private: System::Void Time_Click(System::Object^  sender, System::EventArgs^  e) {
}
private: System::Void button4_Click_1(System::Object^  sender, System::EventArgs^  e) {
	StelleInEmpfangsDatenPuffer = 0;;
	SchrittBeimZeichenEmpfang = 0;
	ZeitBeimEmpfang = 0;
	textBox2->Text = "";
	textBox3->Text = "";
	Clearing->Enabled = false;
	PufferSave->Enabled = false;
	PufferIstVoll = 0;
	//Falle = Convert::ToByte(textBox3->Text);
	FalleGeschnapt = 0;

}
private: System::Void TimerClr_Click(System::Object^  sender, System::EventArgs^  e) {

	SekDiv10 = 0;
	Second = 0;
	Minute = 0;
	
}
private: System::Void DisplayStopOn_Click(System::Object^  sender, System::EventArgs^  e) {

	if (DisplayStopOn->Text== "Display On")
			DisplayStopOn->Text = "Display Stop";
		else
		DisplayStopOn->Text = "Display On";

}
};

	
	}

	


