Industrial App: Can I raise an event for something that doesn't have an event?

fj1200

Member
Joined
Sep 1, 2010
Messages
13
Programming Experience
1-3
I want to raise an event for an opto-input state change but not sure if I can. Basically I have 4 opto-inputs on a PCI control card - one of which changes pretty rapidly - that I currently poll with a timer. I'm looking at getting a proper hardware counter for that but the other 3 are production status indicators. I'd prefer it if they alerted the app to a state change though.

Depending on the state of one of the inputs I have to fire a relay which simulates a button push. Having been playing with serial ports and getting a very useful app built for logging production data into a SQL Server DB I want to re-visit a proof-of-concept system I built a few months ago.

Current code - which works fine - but I'd like to change is like this:
VB.NET:
 Private Sub tmrOptoInput_0_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrOptoInput_0.Tick
  
 'Read OptoInput_1 to Monitor Track State...

        Dim aType As Byte                          ' replace Byte with any other type
        Dim TypeCode As Integer                    ' an integer to hold the type code
        TypeCode = Type.GetTypeCode(aType.GetType)

        dwResult = PciRELAY8_OptoInputRead(RELAY8_DEVICE, 0, aType)
        TypeCode = Type.GetTypeCode(aType.GetType)

        If aType = 0 Then ..... <lots of other stuff>

Having 4 inputs so I need 4 timers, plus another one that polls the database and checks a status flag which triggers the app to start logging. I'd like it to be event driven rather than timer driven if at all possible but I don't know how and there's nothing in the control card docs. Also I can't seem to find anything relevant on the web. Can anyone help?
 
well if the action doesn't have an event coupled to it, you aren't going to just immediately pull an event out of nowhere.

But you can convert that polling to an event so that the rest of the application doesn't realize that you're actually polling it.

Of course if you find a better method (other then the timer, you brought up hardware counter) to perform your polling, you can use whatever to fire the event (an event is just the calling of a delegate).

VB.NET:
Public Event OptoStateChange As EventHandler

Private Sub tmrOptoInput_0_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrOptoInput_0.Tick
  
 'Read OptoInput_1 to Monitor Track State...

        Dim aType As Byte                          ' replace Byte with any other type
        Dim TypeCode As Integer                    ' an integer to hold the type code
        TypeCode = Type.GetTypeCode(aType.GetType)

        dwResult = PciRELAY8_OptoInputRead(RELAY8_DEVICE, 0, aType)
        TypeCode = Type.GetTypeCode(aType.GetType)

	If aType = 0 Then
		RaiseEvent OptoStateChange(Me, EventArgs.Empty)
	End If
End Sub
 
Yeah - that's what I though. Oh well... we had the card already - just trying to save a bit of cash. I've since found a rail-mounted opto-input unit with inputs that will generate events and also virtual COM port drivers so that'll make things easier.

Thanks anyway.
 
Back
Top