Help with appending to RichTextBox from an event

RichardHM

Member
Joined
Jul 22, 2005
Messages
10
Programming Experience
1-3
I an new to using VB.net and I am using the serialport class that Microsoft has kindly created for VB.net 2005.

I am trapping the received event and reading the byte in sucessfully, but when I try to access the main form richtextbox to append the data i get an

"A first chance exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll" exception.

If I call the function from a button on the same form, then this doesn't happen.

VB.NET:
 Public WithEvents MyEvent As System.IO.Ports.SerialPort 



Public Sub MyEvent_DoRx(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _

Handles SerialPort1.DataReceived

Dim MyByteIn As String

MyByteIn = SerialPort1.ReadExisting 

MsgBox("data received: " & MyByteIn)

DoAppendDebugText(MyByteIn)

End Sub

 



Public Sub DoAppendDebugText(ByVal MyDebStr As String)

MsgBox(" passed Value : " & MyDebStr)

Me.MyDebugText.AppendText(MyDebStr)

End Sub

Any help or guidance would be greatly appreciated.

Richard

 
i think it's because you're not calling the instance of the form, you're calling the form it'self

i'm assuming that this form is the startup object and what you need to do is add a module to the project and put "Friend MainForm As Form1" (with Form1 being the name of the form you're using)

then in the Load Event of that form put "MainForm = Me"

now where ever that DoAppendDebugText sub is you simply use MainForm.MyDebugText.AppendText()

and there you go
 
JuggaloBrotha -- are you looking at the same code I am? He's not referencing the form at all.... only as Me. Which is the current active instance of that form within that context. - never mind, I sit corrected, I see the "main form richtextbox" in his description.

I assume that the message boxes come up OK, right?

Having just now seen the "main form richtextbox" bit.... what do you mean by that? Is the code running somewhere else other than on the main form?

Tg
 
TechGnome,

RichardHM said:
but when I try to access the main form richtextbox to append the data
RichardHM said:
If I call the function from a button on the same form, then this doesn't happen


i'm assuming that it's in a module or another form than the one he's trying to access the richtextbox
 
[resolved]

Cheers for the advice and help.

The Module didn't solve my problem, but it did highlight a 'Cross-Threading' problem associated with the serial port event and wanting to access controls on the main form.

After much searching and understanding, I am now using a delegate subroutine and the invoke method to pass a string to update the text box.

Richard:)
 
RichardHM:


Can you post the code. I'm having problems with "ReadExisting" method not showing the stream read into a textbox.

I will appreciated if you could post the code.

thanks
 
It's been a while since I have written any.net stuff, but here is the code that I was refering to. I've stripped out some of the specifics to give you a basic framework. I have used the readline instead of readexisting, but this shouldn't make any difference.

VB.NET:
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]WithEvents[/COLOR][/SIZE][SIZE=2] MyEvent [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.IO.Ports.SerialPort
[/SIZE]
 
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] MyEvent_DoRx([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2]System.IO.Ports.SerialDataReceivedEventArgs) _
[/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] SerialPort1.DataReceived
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] MyStringIn [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' local variable
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].SerialPort1.NewLine = Chr(13)
MyStringIn = SerialPort1.ReadLine [/SIZE][SIZE=2][COLOR=#008000]' read data
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'MsgBox("data received: " & MyStringIn)
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].InvokeRequired [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' is main thread running?
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Params() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2] = {MyStringIn} [/SIZE][SIZE=2][COLOR=#008000]' arguments for 'invoke' command
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] NewUpdateDel [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] CommsHandlerDelegate _
([/SIZE][SIZE=2][COLOR=#0000ff]AddressOf[/COLOR][/SIZE][SIZE=2] CommsHandlerSubroutine)
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Invoke(NewUpdateDel, Params) [/SIZE][SIZE=2][COLOR=#008000]' invoke main thread through delegate
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'MsgBox("invoke finished")
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].CommsHandlerSubroutine(MyStringIn) [/SIZE][SIZE=2][COLOR=#008000]' don't use delegate
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][/COLOR][/SIZE] 
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Delegate[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] CommsHandlerDelegate([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] DelStr [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff][/COLOR][/SIZE] 
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] CommsHandlerSubroutine([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] DataString [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#008000]' DataString should contain entire comms string including (13)
[/COLOR][/SIZE][SIZE=2]' deal with comms string in here, any references to me.textbox should be accessable and visible in here[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]

Hope this helps, let me know how you get on.

Best regards

Richard
 
RichardHM:

Thank you very much for the code. I have made some advances; I’m able to read MyStringIn but only when I Open the Port, Close it and open it again.

For some reason I’m not getting the text as it is coming out of the port in my:

txtListATcommands.Text = DataString

Do you know by any change how can I modify this piece of code so I’m able to receive the string in real time into my txtListATcommands textbox?

Any help will be highly appreciated.

Thanks
VB.NET:
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] MyEvent_DoRx([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] SerialDataReceivedEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] myCommPort.DataReceived
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] MyStringIn [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#008000]' local variable
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].myCommPort.NewLine = Chr(13)
MyStringIn = myCommPort.ReadExisting [/SIZE][SIZE=2][COLOR=#008000]' read data
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'MsgBox("data received: " & MyStringIn)
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].InvokeRequired [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#008000]' is main thread running?
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Params() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2] = {MyStringIn} [/SIZE][SIZE=2][COLOR=#008000]' arguments for 'invoke' command
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] NewUpdateDel [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] CommsHandlerDelegate([/SIZE][SIZE=2][COLOR=#0000ff]AddressOf[/COLOR][/SIZE][SIZE=2] CommsHandlerSubroutine)
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Invoke(NewUpdateDel, Params) [/SIZE][SIZE=2][COLOR=#008000]' invoke main thread through delegate
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'MsgBox("invoke finished")
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].CommsHandlerSubroutine(MyStringIn) [/SIZE][SIZE=2][COLOR=#008000]' don't use delegate
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Delegate[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] CommsHandlerDelegate([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] DelStr [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] CommsHandlerSubroutine([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] DataString [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#008000]' DataString should contain entire comms string including (13)
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]' deal with comms string in here, any references to me.textbox should be accessable and visible in here
[/COLOR][/SIZE][SIZE=2]txtListATcommands.Text = DataString
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]
 
The only thing i can think of at the moment, is that you look at your serial port properties. Try setting RThreshold to 1. This will force the event to happen for each byte received.

Richard
 
Back
Top