By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,246 Members | 1,451 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,246 IT Pros & Developers. It's quick & easy.

rs232 Data Received

P: n/a
Lou
I have a class that uses the serial port class

Private SerialPort as New SerialPort

When I receive the asyncronous serial port response it appears that data is
on a different thread than my UI.
When I try to raise an event in my class my frmMain doesn't get the
callback. i think this is a threading issue.
Question is how do I get the serial port response from my vb class to my
main form.
Also when I send data the event in my send never gets raised back to my main
form.
Again this appears to be a threading issue and I have no solution. I have
been at this for days. Arghh.

Here is my send code. Notice the "RaiseEvent", well it nevrs gets received
by my main form that has this class defind withEvents.

Public Function SendData(ByVal sData As String) As Boolean '

Dim Bytes() As Byte = System.Text.Encoding.Default.GetBytes(sData)

If mProtocol = eProtocolType.Serial Then

Try

If SerialPort.IsOpen = False Then OpenSerialPort(mComPort)

' Write a line to the serial port

SerialPort.Write(Bytes, 0, (Bytes.Length - 1))

RaiseEvent Rs232DataSent(sData) 'I GET HERE BUT THE DATA GOES INTO LA LA
LAND!!!!

Catch ex As System.Exception

MessageBox.Show(ex.Message)

End Try

End If

End Sub


Jun 9 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a

"Lou" <lo********@comcast.netwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
>I have a class that uses the serial port class

Private SerialPort as New SerialPort

When I receive the asyncronous serial port response it appears that data
is on a different thread than my UI.
When I try to raise an event in my class my frmMain doesn't get the
callback. i think this is a threading issue.
Question is how do I get the serial port response from my vb class to my
main form.
Also when I send data the event in my send never gets raised back to my
main form.
Again this appears to be a threading issue and I have no solution. I have
been at this for days. Arghh.

Here is my send code. Notice the "RaiseEvent", well it nevrs gets received
by my main form that has this class defind withEvents.

Public Function SendData(ByVal sData As String) As Boolean '

Dim Bytes() As Byte = System.Text.Encoding.Default.GetBytes(sData)

If mProtocol = eProtocolType.Serial Then

Try

If SerialPort.IsOpen = False Then OpenSerialPort(mComPort)

' Write a line to the serial port

SerialPort.Write(Bytes, 0, (Bytes.Length - 1))

RaiseEvent Rs232DataSent(sData) 'I GET HERE BUT THE DATA GOES INTO LA LA
LAND!!!!

Catch ex As System.Exception

MessageBox.Show(ex.Message)

End Try

End If

End Sub

Have you subscribed to that event in your main form to receive those events?

Jun 9 '07 #2

P: n/a
Hi,
>>
When I receive the asyncronous serial port response it appears that data is
on a different thread than my UI.
<<

Yes, the DataReceived event executes in the thread context of the SerialPort
receive background thread. You have to marshal data from the backgound
thread using a Delegate or Delegate event (Invoke or BeginInvoke).

You can download example code from my website. I have more detailed
information in my book (see below), with many other examples.

Dick

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
Jun 9 '07 #3

P: n/a
Lou
Yes..
"Ray Cassick" <rc******@enterprocity.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
>
"Lou" <lo********@comcast.netwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
>>I have a class that uses the serial port class

Private SerialPort as New SerialPort

When I receive the asyncronous serial port response it appears that data
is on a different thread than my UI.
When I try to raise an event in my class my frmMain doesn't get the
callback. i think this is a threading issue.
Question is how do I get the serial port response from my vb class to my
main form.
Also when I send data the event in my send never gets raised back to my
main form.
Again this appears to be a threading issue and I have no solution. I have
been at this for days. Arghh.

Here is my send code. Notice the "RaiseEvent", well it nevrs gets
received by my main form that has this class defind withEvents.

Public Function SendData(ByVal sData As String) As Boolean '

Dim Bytes() As Byte = System.Text.Encoding.Default.GetBytes(sData)

If mProtocol = eProtocolType.Serial Then

Try

If SerialPort.IsOpen = False Then OpenSerialPort(mComPort)

' Write a line to the serial port

SerialPort.Write(Bytes, 0, (Bytes.Length - 1))

RaiseEvent Rs232DataSent(sData) 'I GET HERE BUT THE DATA GOES INTO LA LA
LAND!!!!

Catch ex As System.Exception

MessageBox.Show(ex.Message)

End Try

End If

End Sub


Have you subscribed to that event in your main form to receive those
events?

Jun 11 '07 #4

P: n/a
Lou
Buying the book NOW.
You have been enormously helpful in the past. I have 2 versions of your book
already.
I am just stupid for not buying the .NET version, I could have saved MUCH
time and MONEY!

-Louie

"Dick Grier" <dick_grierNOSPAM@.msn.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Hi,
>>>
When I receive the asyncronous serial port response it appears that data
is
on a different thread than my UI.
<<

Yes, the DataReceived event executes in the thread context of the
SerialPort receive background thread. You have to marshal data from the
backgound thread using a Delegate or Delegate event (Invoke or
BeginInvoke).

You can download example code from my website. I have more detailed
information in my book (see below), with many other examples.

Dick

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.

Jun 11 '07 #5

P: n/a
Lou
How do I do that from with a VB class.
I have a Device class that creates a serial class.
The serial class raises an event back to the device class. I get to the line
of code where it raises the event but the device class never receives the
event. very weird.
-Lou

"Dick Grier" <dick_grierNOSPAM@.msn.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Hi,
>>>
When I receive the asyncronous serial port response it appears that data
is
on a different thread than my UI.
<<

Yes, the DataReceived event executes in the thread context of the
SerialPort receive background thread. You have to marshal data from the
backgound thread using a Delegate or Delegate event (Invoke or
BeginInvoke).

You can download example code from my website. I have more detailed
information in my book (see below), with many other examples.

Dick

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.

Jun 11 '07 #6

P: n/a
Hi,

This should be easy. You just:

Private WithEvents SomeObject as New SomeClass

VB then creates stubs for each event in SomeClass, and your code that
handles these events "should" get called when you call RaiseEvent. For
example, if you do this:

Private WithEvents SerialPort As New System.IO.Ports.SerialPort

The DataReceived event will be called whenever RThreshold (or more) bytes
are received after the port has been opened.

Dick

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
Jun 11 '07 #7

P: n/a
Lou
Dick, I got all that and the serial control event gets called. So at that
point I raise another event back to the Devices class, that works then
another event back to the UI so I can display it , it's that event that
never gets back to the UI.

I have a Device class and a Serial class:
The Device class creates a Serial class. The serial class has this.
Private WithEvents SerialPort As New System.IO.Ports.SerialPort

I then have a collection (Generics) of Devices.
Now when i run the app.
The serial class serial port receives the data and calls back to the device
class. That works.
The device class then rasises an event back to the frmMain UI for display,
that never happens and thats the problem. I put a break point in my Device
class where I raise the event, it gets executed but the function receiveing
the event in frmMain never gets called. Wierd, its probably a threading
issue??? Do I need a delegate in the device class, if so how is that done in
a class?

-Lou Garvin

"Dick Grier" <dick_grierNOSPAM@.msn.comwrote in message
news:ua**************@TK2MSFTNGP03.phx.gbl...
Hi,

This should be easy. You just:

Private WithEvents SomeObject as New SomeClass

VB then creates stubs for each event in SomeClass, and your code that
handles these events "should" get called when you call RaiseEvent. For
example, if you do this:

Private WithEvents SerialPort As New System.IO.Ports.SerialPort

The DataReceived event will be called whenever RThreshold (or more) bytes
are received after the port has been opened.

Dick

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.

Jun 12 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.