来自串行端口的文本出现在富文本框中,但是没有作为字符串提取? [英] Text from a serial port appearing in a rich text box but not being picked up as string?
问题描述
大家好,
我正在读取一个串行端口并将输出放入字符串中.
但是字符串是空白的,但是,如果我在其中插入一个消息框,则会造成足够的间隙以允许文本出现在字符串中.我使用的是基于预期回复时间的延迟.
出现的代码行是
Hi All,
I am reading a serial port and putting output to a string.
But the string is blank, however if I stick a message box in, it causes enough of a gap to allow the text to appear in the string. I am using a delay based on the length of the expected reply.
The line of code that appears is
reply = rtbIncoming.text
rtbIncoming.Text中包含来自设备的字符串.是否由于线程分开而导致文本框中的答复出现在一个线程上,所以从comms线程到UI线程的延迟没有足够的延迟吗?由于我有以下建议
rtbIncoming.Text has a string in it from the device. Is it because of seperate threads the reply in the text box appears on one thread, there is not enough of a delay for it to get from the comms thread to the UI thread? As I have had the following advice
So instead of a line like this:
rtbIncoming.Text = reply;
我会尝试这样的代码:
I''d try code like this:
this.Invoke(new MethodInvoker(delegate()
{
rtbIncoming.Text = reply;
}));
这似乎使用了与我正在使用数据已接收事件"的代理类似的代理.
This would appear to use a delegate similar to the one I am using the Data Recieved Event.
private void port_DataReceived_1(object sender,SerialDataReceivedEventArgs e)
{
// MessageBox.Show("Here!!");
int valueWait = WaitForData(12, 10);
InputData = myComPort.ReadExisting();
if (InputData != String.Empty)
{
this.BeginInvoke(new SetTextCallback(SetText), new object[]
{InputData});
}
/*
this.Invoke(new MethodInvoker(delegate()
{
ReplyData= rtbIncoming.Text;
}));
*/
}
我认为我已经看过的上一个答案是这样的,因为我现在有两个委托到同一个线程,但没有成功!帮助!
Glenn
I think the previous answer I had over looked this as I now have two delegates to the same thread with no success! HELP!!
Glenn
推荐答案
已解决! (我认为)我对您提出的问题有点含糊,但是我不得不思考并在昨天发送来解决该问题后安排该代表.今天早上,我的电脑变得更糟了,于是Windows重新安装了,杂乱无章,杂乱无章.当我得到一个来源,然后找到另一个不矛盾但足够接近以形成问号的来源时,有人能推荐一个可靠的信息重新线程和委托的好来源吗?
格伦(Glenn)
Solved! (I think) the question was a little a vague I grant you but it got me to thinking and placing the delegate after the send appeared to cure the problem yesterday. This morning my PC took a turn for the worse so thats a Windows reinstall, mutter, mutter. Can some one recomend a good source of reliable info regaurding threading and delegates as I get one source and then find another that does not contradict but comes near enough for question marks to form.
Glenn
这篇关于来自串行端口的文本出现在富文本框中,但是没有作为字符串提取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!