The DataReceived event is not firing for me with a SerialConnection located in a Module. In the same Module a Timer using the PerformAction Method successfully sends a string (SerialConnection3.Write(“finished”)) to the RS232 connected instrument using the SerialConnection that other testing has shown successfully returns data through the serial port.
Do I need to somehow Instantiate the SerialConnection enabling the DataReceived event, i.e. “Var SerialConnection3 As New SerialConnection” somewhere?
If I understand correctly, I have already applied the second option to create the DataReceived event handler for the SerialConnection. See the attached ScreenShot.
But maybe I don’t understand what is meant by “… after you instantiate the serial connection.” Does this mean after I use the GUI to drag the SerialConnection control out of the library and drop it onto the Module? Or does “instantiate” refer to the code “Var SerialConnection3 As New SerialConnection” that I’m using in the Timer in the Module before writing to the SerialConnection like this: “SerialConnection3.Write(“start” + " " + MethodNumber + " " + PositionNumber + chr(13))”
A module is just a container. Dragging a control inside a module creates a subclass of the control, to which you can add your own methods, properties, etc, but does not create an instance the way it would if you dragged it to a window.
What you’ll need to do (because SerialConnection fires its events asynchronously) is to create a property somewhere (I’d start with the module that you’re working with) whose type is SerialConnection3 (although you might want to rename that to something more useful):
MySerial as SerialConnection3
And then somewhere, let’s say in the method that does the first write, you can write at the top.
If MySerial = Nil then
MySerial = New SerialConnection3
End If
Having said that… you have a lot of code in the DataAvailable event. This is generally a bad idea because events like this are reentrant. That is, they will fire whenever new data comes in, even if your code from the previous DataAvailable event is still running.
The general practice is to just use the DataAvailable event to copy data to a buffer and then another method started by a timer to process the data. That method should check to see if processing is already running and just return if it is, and at the end, should check to see if there’s more data in the buffer that needs to be processed before it exits.
You have done the first option. You created a subclass named SerialConnection3. Now you need a property to hold an instance of it so you can use it. Note that
Var SerialConnection3 As New SerialConnection
creates a local variable that just happens to have the same name as your subclass, but is not related to it in any way.
Add a property such as MySerialConnection As SerialConnection3 to your module and instantiate it as Greg reccomended
If MySerialConnection = Nil Then
MySerialConnection = New SerialConnection3
End If