我是安全关键系统的嵌入式系统软件开发人员,所以我对 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",而是保持空白。
对这里可能发生的事情有什么想法吗?