Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I do this?

Status
Not open for further replies.

woogoo

Programmer
Feb 14, 2004
247
GB
Hi,

I have a class that handles input from a Serial Port, I have then created an ArrayList of these classes to deal with the supported number of Ports.

I would like to add some public shared events to the class that will only fire on the currently active port.

For example if I have 4 instances of my SerialPort class in my ArrayList and the 2 instance is the active one only it will raise the events.

I then need a way of catching these events as the normal way doesn't work since using WithEvents is pointless on what is deemed an ArrayList.

I'd appreciate your thoughts on this one thanks.


 
First of all, title your threads a little better than this next time, like "Custom Serial Port Events" or something of the like. You'll get more responses that way.

Here is an example of a custom event I found at



Code:
Public Class RegistrationWatch
   Public Event NewRegistrations(ByVal pStudents As DataSet)

   ' Other methods and properties
   Public Sub Look()
    Dim dsStuds As DataSet
    Dim flNew As Boolean

    ' Method that fires on a timer to look for new registrations
    ' since the last invocation of the method
    ' If one is found then create a DataSet with the new students
    ' and raise the event
    flNew = True
    dsStuds = New DataSet()

    If flNew Then
      RaiseEvent NewRegistrations(dsStuds)
    End If
   End Sub
End Class

I'm pretty sure you at least have a dll loaded to read from the Serial Port(s).

- First, make a class to handle the serial port(s).
- Add your existing code to it.
- Add an event to it, I'll use SerialPortEnabled
- Make a timer that tracks "something in that dll" that changes when you need the event to fire. When it detects the "event" you have in the timer, use the RaiseEvent command like in the above example.
- In another class/module/etc., you'll have to make an object to hold the class that can trigger the event, and a method to handle the Event.

Code:
'Serial Port Class
Public Class SerialPortClass

     'Make sure you add this... 
     'This is the event declaration
     Public Event SerialPortEnabled(ByVal someNeededParam As Object)
    'Make a timer
    Private tmrSerialEnabledCheck as Timer = New Timer
       

     Public Sub New()
       'Set Interval to 1/10th of a second(100 milliseconds)
       tmrSerialEnabledCheck.Interval = 100
       'Turn on the timer
       tmrSerialEnabledCheck.Enabled = True 
       'Add any other code needed...
     End Sub

     'Put what you need in here to do whatever with the
     'serial port(s)

     


     'Handle the Timer's Tick Event
     Private Sub CatchSerialEnabledEvent() Handles tmrSerialEnabledCheck.Tick
       'Here you check your "something in the dll"
       'If the "something" matches what should call the      
       'event then
       RaiseEvent SerialPortEnabled(<add a parameter if you need too>)
     End Sub

End Class

Public Class MainClass

     'Declare a New SerialPortClass
     'MAKE SURE YOU PUT THE WithEvents IN THERE
     Public WithEvents oSerialPort As SerialPortClass = _
        New SerialPortClass

     'Make a method to handle the Serial Port Event
     Public Sub oSerialPort_SerialPortEnabled(<any needed params>) Handles oSerialPort.SerialPortEnabled
       'Do what you gotta do here...
     End Sub


End Class

If you need more specific events search the web for VB.NET Delegates.

I haven't tried any of the code I just wrote here, so it's quite possible there is something wrong with it, but I really have to get back to work, so hopefully this gives you a start.

Hope this helps you out.

-Merk

There will either be World Peace or The World in Pieces. Everything we do plays a part for one of the outcomes.
 
First of all, title your threads a little better than this next time, like "Custom Serial Port Events" or something of the like. You'll get more responses that way."

Fair comment and duly noted.

Thanks for the other input I'll go and have a read of the chapter you references see what I can glean from it.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top