rs232 Data Received

L

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
 
R

Ray Cassick

Lou said:
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?
 
D

Dick Grier

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.
 
L

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
 
L

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
 
D

Dick Grier

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.
 
L

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top