Using Events from outside controls (1 Viewer)

keiths

Registered User.
Local time
Yesterday, 23:43
Joined
Feb 10, 2005
Messages
28
I am trying to use an "external" control in my access database. Very briefly, here is the scenario.

I have a unique measuring instrument that uses hex codes to communicate with it via a comm port. To initialize the instrument, I send it 0xD5 in Hex. It responds with 200 in Ascii.
I was using some third party software to do this, and it works okay to a point. However I never did like the third party software running in the background idea, for stability reasons. So I am working on a solution using an active control (referenced library/activex control).
So far, I have worked out the code so I can send the hex code to the instrument and it initializes....That is great. Now I want to read data coming back from the instrument. Here is where I am having problems....
The control uses event processing (samples are in vb, by the way) to do most things. When there is data, an event is fired called "RecvData". The documentation states that "When the ReadData method is called this event is fired whenever data is read from the serial port buffer based on the ReceiveInterval value."
The documentation also states the syntax is as follows:
Private Sub Object_RecvData(ByVal Data as Variant)
The Data value is the text data read from the port.
I am a bit confused on trying to use events from external dll's or activex controls... Listed is my code so far.

Option Compare Database

Private Sub Command0_Click()

Dim X As String
'create a object reference to the component
Dim cp As COMPORTLib.ComPortX
'create an instance of the object
Set cp = New COMPORTLib.ComPortX

' Set parameters for ComPort
cp.ComPort = 1
cp.Baud = 9600
cp.DataBits = 8
cp.Parity = 0
cp.StopBits = 0
cp.RecvBufferSize = 1024
cp.RecvTimeout = 500
cp.ReceiveInterval = 1000
cp.RecvDataType = 0
cp.DiscardDataOnTimeoutWithNoEOS = False
cp.EndOfStringCharacters = ""
cp.EOSExtraCharacters = 0
' Initialize COM1 for use with device
cp.Initialize

If (cp.Initialize = False) Then
MsgBox ("Comm Init Failed")
Exit Sub
End If

MsgBox ("Comm Init Succeeded")

cp.SendData Chr(213), 1
' Device should initialize and send the number 200 in ascii
cp.ReadData
' Read data from device and display in message box

' Something needs to be in here to work with RecvData Event
' and place data sent from machine into the value X

MsgBox "Transmitted Initialization and got " + NL + X + NL

End Sub

I am not sure what to do with the RecvData event, so I can put the data coming from the instrument into the value of X.
If anyone can steer me in the right direction, I would be verry happy!
 

Bat17

Registered User.
Local time
Today, 06:43
Joined
Sep 24, 2004
Messages
1,687
Just guessing as I have not used it but,

Private Sub Object_RecvData(ByVal Data as Variant)
MsgBox "Transmitted Initialization and got " + NL + Data + NL
end sub

Will probably fire off the message box every time some data is received, so you need to build filters in the event to tell it what to do with what it receives.

HTH

Peter
 

Users who are viewing this thread

Top Bottom