Serial Port, String to Char Array. Help Please

Hi everyone,
I have a serial port program, which is working. Basically a micro sends serial data every 3 seconds and the program receives it and displays it in a rich text box. The data is in a String, but I want to manipulate it or remove parts for calculation. The string resembles something like "T4323H2345P4398C6212S7623*"
So the string splits up into T4323 , H2345, P4398 , C6212 , S7623 and *(The * just means this is the end of data).
I'm looking to use the letters as an INDEX (eg between T and H = 4323 or 43 and 23) to get the data into variables so I can do calculations with them.
Alternatively, get the data to a Char[] first and then to a string. The only purpose for the string is to show it in the rich text box.
Any help is appreciated.
Thank you.
Regards Mark.

 
  
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
#pragma once

namespace CppWinForm7 {

	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;
	using namespace System::Threading;
	using namespace System::Windows;
	/// <summary>
	/// Summary for MyForm
	/// </summary>
	public ref class MyForm : public System::Windows::Forms::Form
	{
	public:
		MyForm(void)
		{
			InitializeComponent();
			findPorts();

			
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~MyForm()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::IO::Ports::SerialPort^  serialPort1;


	protected:



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

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		//Global Var///////////////////////////
		String^ Data = "*";
		String^ data1;
		String^ data2;
		
	private: System::Windows::Forms::Button^  btnOpen;
	private: System::Windows::Forms::Button^  btnClose;
	private: System::Windows::Forms::ComboBox^  comboBox1;
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::TextBox^  txtTransfer;
	private: System::Windows::Forms::Button^  btnTransfer;
	private: System::Windows::Forms::RichTextBox^  SerialDataBox;

			 
			
#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->serialPort1 = (gcnew System::IO::Ports::SerialPort(this->components));
				 this->btnOpen = (gcnew System::Windows::Forms::Button());
				 this->btnClose = (gcnew System::Windows::Forms::Button());
				 this->comboBox1 = (gcnew System::Windows::Forms::ComboBox());
				 this->label1 = (gcnew System::Windows::Forms::Label());
				 this->txtTransfer = (gcnew System::Windows::Forms::TextBox());
				 this->btnTransfer = (gcnew System::Windows::Forms::Button());
				 this->SerialDataBox = (gcnew System::Windows::Forms::RichTextBox());
				 this->SuspendLayout();
				 // 
				 // serialPort1
				 // 
				 this->serialPort1->DataReceived += gcnew System::IO::Ports::SerialDataReceivedEventHandler(this, &MyForm::serialPort1_DataReceived);
				 // 
				 // btnOpen
				 // 
				 this->btnOpen->Location = System::Drawing::Point(705, 72);
				 this->btnOpen->Name = L"btnOpen";
				 this->btnOpen->Size = System::Drawing::Size(75, 23);
				 this->btnOpen->TabIndex = 2;
				 this->btnOpen->Text = L"Open";
				 this->btnOpen->UseVisualStyleBackColor = true;
				 this->btnOpen->Click += gcnew System::EventHandler(this, &MyForm::btnOpen_Click);
				 // 
				 // btnClose
				 // 
				 this->btnClose->Location = System::Drawing::Point(810, 72);
				 this->btnClose->Name = L"btnClose";
				 this->btnClose->Size = System::Drawing::Size(75, 23);
				 this->btnClose->TabIndex = 3;
				 this->btnClose->Text = L"Close";
				 this->btnClose->UseVisualStyleBackColor = true;
				 this->btnClose->Click += gcnew System::EventHandler(this, &MyForm::btnClose_Click);
				 // 
				 // comboBox1
				 // 
				 this->comboBox1->BackColor = System::Drawing::SystemColors::WindowFrame;
				 this->comboBox1->DropDownStyle = System::Windows::Forms::ComboBoxStyle::DropDownList;
				 this->comboBox1->FormattingEnabled = true;
				 this->comboBox1->Location = System::Drawing::Point(731, 35);
				 this->comboBox1->Name = L"comboBox1";
				 this->comboBox1->Size = System::Drawing::Size(121, 21);
				 this->comboBox1->TabIndex = 4;
				 // 
				 // label1
				 // 
				 this->label1->AutoSize = true;
				 this->label1->Location = System::Drawing::Point(728, 18);
				 this->label1->Name = L"label1";
				 this->label1->Size = System::Drawing::Size(26, 13);
				 this->label1->TabIndex = 5;
				 this->label1->Text = L"Port";
				 // 
				 // txtTransfer
				 // 
				 this->txtTransfer->Location = System::Drawing::Point(684, 155);
				 this->txtTransfer->Name = L"txtTransfer";
				 this->txtTransfer->Size = System::Drawing::Size(225, 20);
				 this->txtTransfer->TabIndex = 6;
				 // 
				 // btnTransfer
				 // 
				 this->btnTransfer->Location = System::Drawing::Point(758, 117);
				 this->btnTransfer->Name = L"btnTransfer";
				 this->btnTransfer->Size = System::Drawing::Size(75, 23);
				 this->btnTransfer->TabIndex = 7;
				 this->btnTransfer->Text = L"Transfer";
				 this->btnTransfer->UseVisualStyleBackColor = true;
				 this->btnTransfer->Click += gcnew System::EventHandler(this, &MyForm::btnTransfer_Click);
				 // 
				 // SerialDataBox
				 // 
				 this->SerialDataBox->Location = System::Drawing::Point(12, 18);
				 this->SerialDataBox->Name = L"SerialDataBox";
				 this->SerialDataBox->Size = System::Drawing::Size(617, 334);
				 this->SerialDataBox->TabIndex = 8;
				 this->SerialDataBox->Text = L"";
				 this->SerialDataBox->TextChanged += gcnew System::EventHandler(this, &MyForm::SerialDataBox_TextChanged);
				 // 
				 // MyForm
				 // 
				 this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
				 this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
				 this->ClientSize = System::Drawing::Size(1020, 402);
				 this->Controls->Add(this->SerialDataBox);
				 this->Controls->Add(this->btnTransfer);
				 this->Controls->Add(this->txtTransfer);
				 this->Controls->Add(this->label1);
				 this->Controls->Add(this->comboBox1);
				 this->Controls->Add(this->btnClose);
				 this->Controls->Add(this->btnOpen);
				 this->Name = L"MyForm";
				 this->Text = L"MyForm";
				 this->Load += gcnew System::EventHandler(this, &MyForm::MyForm_Load);
				 this->ResumeLayout(false);
				 this->PerformLayout();

			 }
#pragma endregion
	
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
private: void findPorts(void)
	{
		// get port names
		array<Object^>^ objectArray = SerialPort::GetPortNames();
		// add string array to combobox
		this->comboBox1->Items->AddRange(objectArray);
	}

			
	private: System::Void SetTextCallback(System::Object^ sender, System::EventArgs^ e)
	{	
		this->SerialDataBox->AppendText(data1);
		this->SerialDataBox->AppendText("ffff \n"); // just a marker to show what data we got/are we getting all the data? Debug
		
		
	}

	private: System::Void serialPort1_DataReceived(System::Object^  sender, System::IO::Ports::SerialDataReceivedEventArgs^  e) {

		System::Threading::Thread::Sleep(50);	// There is data so Wait 50ms for the buffer to fill! or we will miss some.
		data1 = serialPort1->ReadExisting();
		
		this->Invoke(gcnew EventHandler(this, &MyForm::SetTextCallback));
	}

	private: System::Void MyForm_Load(System::Object^  sender, System::EventArgs^  e) {
	}
	private: System::Void btnOpen_Click(System::Object^  sender, System::EventArgs^  e) {
		if (this->comboBox1->Text == String::Empty)
			this->SerialDataBox->Text = "Please Select Port Settings";
		else {
			try {
				// make sure port isn’t open
				if (!this->serialPort1->IsOpen) {
					this->serialPort1->PortName = this->comboBox1->Text;
					
					//open serial port
					this->serialPort1->Open();
				}
				else
					this->txtTransfer->Text = "Port isn't openned";
			}
			catch (UnauthorizedAccessException^) {
				this->txtTransfer->Text = "UnauthorizedAccess";
			}
		}
	}

private: System::Void btnTransfer_Click(System::Object^  sender, System::EventArgs^  e) {
	this->serialPort1->Write("THE INVOKING STRING GOES HERE");
}

private: System::Void btnClose_Click(System::Object^  sender, System::EventArgs^  e) {
	this->serialPort1->Close();
}

private: System::Void SerialDataBox_TextChanged(System::Object^  sender, System::EventArgs^  e) {
	
	String^ dateString = DateTime::Now.ToString("d");
	String^ timeString = DateTime::Now.ToString("T");
	String^ Stamp = ".";

	if (SerialDataBox->Text->EndsWith(Data))
	{	
		this->SerialDataBox->AppendText("    " + timeString + "        " + dateString  );
		
	}


//Scroll to End if text fills box.
	this->SerialDataBox->SelectionStart = SerialDataBox->TextLength;
	this->SerialDataBox->ScrollToCaret();



}
		 ////////////////////////////////////////////End
};
}
Take a look at this:

https://msdn.microsoft.com/en-us/library/system.string(v=vs.110).aspx

String^ has a rich interface to manipulate data like IndexOf(...) or Split(...)
Thanks coder777
Came up with this Function.

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
// Strings.cpp : main project file.

#include "stdafx.h"

using namespace System;

int findPosition(String^ daString, String^ indexMarker) //Find Start Position Of data
{
	String^ str = daString;
	String^ toFind = indexMarker;
	int index = str->IndexOf(toFind);
	Console::WriteLine("DEBUG: Found '{0}' in the string '{1}' at position '{2}'", toFind, str, index); //Debug
	return index;
}

int main()
{
    Console::WriteLine(L"Hello World");
	String^ Data = "T4643H4375P3434C2323S2544*";//The Data String

	String^ FindMarker = "P"; //Find Start Position of required data marked by letter.
	int position = findPosition(Data, FindMarker);

	Console::WriteLine("Function \"findPostion()\" reports the start position is '{0}'", position);//Test

	Console::WriteLine("Press a key to continue");
	Console::ReadLine(); // PAUSE! All I got off the top of my head.

    return 0;
}


Can someone please now help with how to get the data into chars/array please.
Thanks again coder, I just need a bit of direction.
Regards Mark.
Last edited on
I think I've got it. bit messy but something like,
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
// StringManipulation.cpp : main project file.

#include "stdafx.h"
#include <iostream>

using namespace System;
using std::cout;

int findPosition(String^ daString, String^ indexMarker) //Find Position Of data
{
	String^ str = daString;
	String^ toFind = indexMarker;
	int index = str->IndexOf(toFind);
	Console::WriteLine("DEBUG: Found '{0}' in the string '{1}' at position '{2}'", toFind, str, index); //Debug
	return index;
}

int main()
{
	Console::WriteLine(L"Hello World");
	String^ Data = "T4643H4375P3434C2323S2544*";//The Data String

	String^ FindStartMarker = "T";
	String^ FindEndMarker = "H";
	int startPosition = findPosition(Data, FindStartMarker);
	int endPosition = findPosition(Data, FindEndMarker);
	Byte Arrayy[8] ;

	int i = (startPosition);
	//int SP = startPosition;
	int EP =( endPosition );
	int J = 0;
	for (i = i + 1; i <= EP; i++)
	{
		Arrayy[J] = Data[i];
		J++;
		if (Data[i] == endPosition)
		{
			break;
		}
	}
	Console::WriteLine("Ascii");
	for (i = 0; i < (EP-1); i++)
	{
		Console::WriteLine(Arrayy[i]);
	}

	
	Console::WriteLine("Start Marker =: '{0}'  End Marker =: '{1}'", FindStartMarker, FindEndMarker);
	Console::WriteLine("Start Position  = : '{0}' End Position = : '{1}'",startPosition,endPosition);//Test

	Console::WriteLine("Press a key to continue");
	Console::ReadLine(); // PAUSE! All I got off the top of my head.

	return 0;
}


Topic archived. No new replies allowed.