Reputation: 1
I am an embedded system software developer for safety critical systems, so I am fairly new with C# but proficient in C-based languages.
To provide a little background, I have developed a Windows Form that interprets serial data packets sent from my embedded software through the serial port into meaningful debugging information.
What I want to do is display each byte of each packet in a TextBox Control. The textbox control that displays the packet information is actually a second form opened by the first form. Here's the code for the event handler that opens the second form from the first:
private void ShowRawSerialData(object sender, EventArgs e)
{
SendSerialDataToForm = true;
if (SerialStreamDataForm == null)
SerialStreamDataForm = new RawSerialDataForm();
SerialStreamDataForm.Instance.Show();
}
In the above code, the .Instance.Show() directive is a means by which I may open a new form if the form is closed, but not show a new form if the form is already open. Then, in my serial data received event handler, I do 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)
{
SerialStreamDataForm.UpdateSerialDataStream(MsgByteArray);
}
Where MsgByteArray is the byte array of the serial packet received. And here is the code for 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();
}
The end result is that the value of RawSerialStreamTextBox.Text is correctly updated with the string I intend on adding to the text box! For example, if I pass the byte array {0x01, 0x7F, 0x7E}, then, through the debugger, I can see that the value of RawSerialStreamTextBox.Text = "0x01 0x7F 0x7E".
The problem is that the text box control itself does not show the newly added text. So even though I can confirm through the debugger that RawSerialStreamTextBox.Text = "0x01 0x7F 0x7E" the text box in Windows does not show "0x01 0x7F 0x7E" but rather, remains blank.
Any ideas for what might be happening here?
Upvotes: 0
Views: 2787
Reputation: 17178
I would guess that you are setting the Text property on an instance other than the one that is actually being displayed. A sanity check would be something like
RawSerialStreamTextBox.Visible = false;
Does it disappear?
Upvotes: 1
Reputation: 8190
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.
Upvotes: 0