0

我是安全关键系统的嵌入式系统软件开发人员,所以我对 C# 相当陌生,但精通基于 C 的语言。

为了提供一些背景知识,我开发了一个 Windows 窗体,它将通过串行端口从我的嵌入式软件发送的串行数据包解释为有意义的调试信息。

我想要做的是在 TextBox 控件中显示每个数据包的每个字节。显示数据包信息的文本框控件实际上是第一个窗体打开的第二个窗体。这是从第一个表单打开第二个表单的事件处理程序的代码:

private void ShowRawSerialData(object sender, EventArgs e)
{
    SendSerialDataToForm = true;
    if (SerialStreamDataForm == null)
        SerialStreamDataForm = new RawSerialDataForm();
    SerialStreamDataForm.Instance.Show();
}

在上面的代码中, .Instance.Show() 指令是一种方法,如果表单关闭,我可以打开一个新表单,但如果表单已经打开,则不显示新表单。然后,在我的串行数据接收事件处理程序中,我这样做:

// Get bytes from the serial stream
bytesToRead = IFDSerialPort.BytesToRead;
MsgByteArray = new Byte[bytesToRead];
bytesRead = IFDSerialPort.Read(MsgByteArray, 0, bytesToRead);
// Now MsgByteArray has the bytes read from the serial stream,
// send to raw serial form
if (SendSerialDataToForm == true && SerialStreamDataForm != null)
{
    SerialStreamDataForm.UpdateSerialDataStream(MsgByteArray);
}

其中 MsgByteArray 是接收到的串行数据包的字节数组。这是 UpdateSerialDataStream 的代码:

public void UpdateSerialDataStream(Byte[] byteArray)
{
    String currentByteString = null;
    currentByteString = BitConverter.ToString(byteArray);
    currentByteString = "0x" + currentByteString.Replace("-", " 0x") + " ";
    if (RawSerialStreamTextBox.InvokeRequired)
    {
        RawSerialStreamTextBox.Invoke(new SerialTextBoxDelegate(this.UpdateSerialDataStream), new object[] { byteArray });
    }
    else
    {
        RawSerialStreamTextBox.Text += currentByteString;
    }
    RawSerialStreamTextBox.Update();
}

最终结果是 RawSerialStreamTextBox.Text 的值已正确更新为我打算添加到文本框中的字符串!例如,如果我传递字节数组 {0x01, 0x7F, 0x7E},那么,通过调试器,我可以看到 RawSerialStreamTextBox.Text = "0x01 0x7F 0x7E" 的值。

问题是文本框控件本身不显示新添加的文本。因此,即使我可以通过调试器确认 RawSerialStreamTextBox.Text = "0x01 0x7F 0x7E",Windows 中的文本框也不会显示 "0x01 0x7F 0x7E",而是保持空白。

对这里可能发生的事情有什么想法吗?

4

2 回答 2

1

我猜你是在一个实例上设置 Text 属性,而不是实际显示的实例。健全性检查将类似于

RawSerialStreamTextBox.Visible = false;

它会消失吗?

于 2011-05-11T19:59:48.593 回答
0

To simplify a little, I would have UpdateSerialDataStream return a string (or pass a string to an out parameter) so that your Event Handler would, instead, look like this:

    // Get bytes from the serial stream
    bytesToRead = IFDSerialPort.BytesToRead;
    MsgByteArray = new Byte[bytesToRead];
    bytesRead = IFDSerialPort.Read(MsgByteArray, 0, bytesToRead);
    // Now MsgByteArray has the bytes read from the serial stream,
    // send to raw serial form
    if (SendSerialDataToForm == true && SerialStreamDataForm != null)
    {
        RawSerialStreamTextBox.Text = UpdateSerialDataStream(MsgByteArray);
    }

And UpdateSerialDataStream would look something like this:

public string UpdateSerialDataStream(Byte[] byteArray)
{
    String currentByteString = null;
    currentByteString = BitConverter.ToString(byteArray);
    currentByteString = "0x" + currentByteString.Replace("-", " 0x") + " ";
    return currentByteString;
}

You'd have to move your code handling the display of the form around a little, but this would allow the form already containing the TextBox to handle the update on its own.

于 2011-05-11T19:18:09.320 回答