Click here to Skip to main content
15,877,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all,

Happy new year!

I am currently working on programming a small serial port software in VC2008.

The aim of this software is to receive the GPS data from serial port and display the data (or text) in the TextBox in windows form interface. I had setting up the serial port and all is right. the problem is when I started to receive the data text, it can only display one line in TextBox.I pasted my key part of code here,

C++
port->Open();
 
// read a line of data from serial port
 
StreamWriter^ sw = gcnew StreamWriter("GPSdata.txt");
 
String^ txt = port->ReadLine();
 
TextBox1->Text = txt;
 
while (( txt = port->ReadLine())!= nullptr)
 
{
 
//sw->WriteLine(txt);
 
TextBox1->Text = txt;
 
sw->Flush();
 
}


I found that if I changed the StreamWriter to write the txt into a file but no need to display in TextBox, the program is right and data can be input in file, but if i need the the data saving in file and displaying on TextBox at the same time, it will break down...

Any helps could be very appreciated.

Many thanks.
Posted
Updated 8-Feb-11 2:13am
v2

1 solution

You need to append to the text box, not change the text.

port->Open();
// read a line of data from serial port
StreamWriter^ sw = gcnew StreamWriter("GPSdata.txt");
String^ txt = port->ReadLine();
TextBox1->Text = txt;
while (( txt = port->ReadLine())!= nullptr) {
	//sw->WriteLine(txt);
	TextBox1->Text += txt;
	//You may also need a + "\r\n" if txt doesn't have a new line sequence
	//TextBox1->Text += txt + "\r\n";
	sw->Flush();
}


Also make sure the text box is multiline.
 
Share this answer
 
Comments
Nish Nishant 8-Feb-11 9:11am    
Voted 5, proposed as answer.
HAOYE850815 8-Feb-11 9:30am    
Hello Andrew, thanks for your reply. I changed the code as what you said. But the situation is the program is stopped without any reaction. I am not sure, it still no data on the textBox?
// open the serial port
port->Open();
// create a file to write
String^ txt = port->ReadLine(); StreamWriter^ sw = gcnew StreamWriter("GPSdata.txt");
//TextBox1->Text += txt + "\r\n";
while (( txt = port->ReadLine())!= nullptr)
{
//String^ txt = port->ReadLine();
//sw->WriteLine(txt);
TextBox1->Text += txt + "\r\n";
sw->WriteLine(txt);
sw->Flush();
}
//sw->Close();
Andrew Brock 8-Feb-11 9:37am    
Some things to look for:
1. Make sure code flow is making it over the line "String^ txt = port->ReadLine();"
2. Make sure the text is getting read correctly from the port.

You can find both of these by inserting a breakpoint on the lines "String^ txt = port->ReadLine();" and "TextBox1->Text = txt;" to make sure the code is getting executed, and that port->ReadLine() is not blocking execution waiting for data.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900