在TextBOX中显示来自串行端口的文本 [英] Display text from serial port in TextBOX

查看:80
本文介绍了在TextBOX中显示来自串行端口的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

新年快乐!

我目前正在使用VC2008编程小型串行端口软件.

该软件的目的是从串口接收GPS数据,并在Windows窗体界面的TextBox中显示数据(或文本).我已经设置了串行端口,一切正常.问题是当我开始接收数据文本时,它只能在TextBox中显示一行.我在这里粘贴了代码的关键部分,

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,

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();
 
}



我发现如果我更改了StreamWriter以将txt写入文件中但无需在TextBox中显示,则该程序是正确的,并且可以在文件中输入数据,但是如果我需要将数据保存在文件中并显示在TextBox上,同时会分解...

任何帮助都将不胜感激.

非常感谢.



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.

推荐答案

您需要追加到文本框中,而不要更改文本.

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.


这篇关于在TextBOX中显示来自串行端口的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆