Hi Folks - I realize this is a very fundamental question about class management in Xojo, but I missed the boat somewhere, and the boat has changed over the years.
I want to instantiate SerialConnection in the App class. This seems to be a sensible thing to do since the SerialConnection is going to be used by several windows, but only one window at a time. If I have multiple copies, then signal routing becomes convoluted.
So, the first thing I tried is this simple statement in the opening event of App:
var USBSerial as new SerialConnection
But then the compiler says Type “App” has no member named “USBSerial” .
So, I add a property USBSerial as SerialConnecton to App. The compiler no longer complains but when running code reaches a line App.USBSerial.Device = SerialDevice.At(i), it throws a “nil object exception” (SerialDevice.At(i) appears to be valid, not nil),
I have read through the tutorial about classes and I have no clue what to do. Help would be appreciated.
As Thomas mentioned, you need to instantiate the SerialConnection when the app opens to avoid the NilObjectException.
You might want to consider making the SerialConnection a property of a module instead of App so that your code can just say
USBserial.whatever
instead of
App.USBserial.whatever
In either case, after instantiating the SerialConnection, you’ll need to add handlers for the SerialConnection’s events using AddHandler. If it was dragged onto a window in the IDE the events would be directly available in the IDE as they are for other controls, but since you’re instantiating it in code you need to create your own handler for any events you want to respond to, e.g. DataAvailable.
Folks, I am drawing a blank, here. Maybe back up a bit. I am looking for a way to have a single SerialConnection that is accessible from various windows as they come and go. Among other things, it seems wasteful to reconfigure per-window SerialConnections each time I close one window and open another, when the serial ports are identical. So, IF this is a good idea, then how to implement it? I thought associating with app would work, but I keep having problems having it persist after the opening event is complete. I can “new” an instance of SerialConnection, there, and even set some properties. But, once that event is over, that instance is nil to the rest of the program.
So, is there a different strategy that would be more useful? Is there a way to create a persistent instance? Yes, in the fall-back, I CAN do one per window but … are there other options?
Julia - sorry, I started this reply just before you posted. I have tried to use modules a few times but keep getting scared away by what seems to be a lack of “how-to” examples or by a simple lack of understanding. I’ll try again.
Many thanks
Jim Wagner
Oregon Research Electronics
Follow-on: I added a module to the IDE and named it SerialModule. I added SerialConnection to the module and named it USBSerial. Removed all references in app. But when I do USBSerial.device = … in a window, I get USBSerial unknown. I seem to be missing a crucial step but I don’t know what to do.
I get “SerialModule.USBSerial.device (with “USBSerial.device” highlighted) this item does not exist”. I have checked spelling of the name of the SerialConnection class added to the SerialModule. It has an event handler for DataReceived.
Public Sub InitSerial()
USBserial = New SerialConnection
USBserial.Device = SerialDevice.At(0) // You need to select the port elsewhere, it may not be 0
USBserial.Baud = SerialConnection.Baud115200
AddHandler USBserial.DataReceived, AddressOf HandleDataReceived
End Sub
Private Sub HandleDataReceived(Sender As SerialConnection)
RXBuffer = RXBuffer+USBserial.ReadAll
End Sub
Because USBserial is a global property, I can refer to it in any window, you can see here that autocomplete is working:
Thank you. I think I can see what you are doing. I really do appreciate your effort on this!
I am doing somewhat similar with a thread, adding a class to the IDE, newing it and running it from opening event of app, and that seems to be all it takes. I do understand that thread behaves differently with respect to persistence and access, but trying to use that as a template was not useful.
Reading through this thread - there’s a disconnect. I don’t think I actually understand what you’re experiencing. Can you post a small sample project?
Julie - I had tried to add the SerialConnection class directly to the module, and not as a property. I was thinking of module as some sort of a “super directory”. Trying your example out right now.
Eric - I think the problem was that I was creating a property that was local to the app opening event. It behaved well in that event handler but trying to access it from another window (after ending the event handler) failed. I expect Julie’s example to work.
Have a problem - Using 2025R1 or 20223R3.1, the IDE does not seem to want to allow me to set SerialConnection as a property; it only allows class. BTW MacOS Sonoma 14.7.1 with 6-core intel core i5.
When I try to compile, 2025R1 tells me this about line 1 of the init function:
This is a type name, not a variable; values can’t be assigned to it USBSerial = new Serial Connection
Modules can contain Class definitions, which is what you have here. You need to add a Property to the module of type USBSerial to accomplish what you’re looking for.
Thank you. I guess Julie’s code did that but without having a class added. When I saw the property USBSerial as SerialConnection, I thought it got there by dragging from the library icon list and what I observed was that it would not work. Every time I dragged, it ended up as a class, not a property. As a hand-entered property IT DOES WORK.
So, I removed that class from the module and typed in the name and super, and all is OK. It is so easy to get into problems like this when you really do not know the fundamentals. Is there a tutorial on the “nuts and bolts” of Xojo classes? I’ve looked but not found anything.