Software Instance of SerialConnection?

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.

Thanks
Jim, Oregon Research Electronics

That just gets you a local variable, which evaporates when the Opening event exits.

I guess you need something like this to create the instance in app.opening

USBSerial = new SerialConnection

1 Like

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.

Thanks
Jim

You would need to refer to it as SerialModule.USBSerial (or whatever your module is named).

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.

Still stumped!

Jim

This all compiles and runs for me, but I don’t have a serial device handy to test it with:

In App.Open, I call InitSerial

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:

1 Like

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.

Jim

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.

Jim

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

This is just the first error - several others.

Jim

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.

Thank you, both, for your help.
Jim

1 Like

Pardon me Julia, for mis-writing your name.

Jim

1 Like

Others might excoriate me for suggesting such an “out of date” reference, but imo the best reference for Xojo fundamentals is still Matt Neuburg’s REALBasic: TDG: The Definitive Guide, 2nd Edition (Definitive Guides): Neuburg, Matt: 9780596001773: Amazon.com: Books

1 Like

This YouTube video may also be worth checking out:

Understanding Object-Oriented Programming with Xojo

Hate to be nit-picky, but wouln’t that be slightly better as

Private Sub HandleDataReceived(Sender As SerialConnection)
  RXBuffer = RXBuffer+Sender.ReadAll
End Sub

It makes it more generic and that’s what the parameter is there for.

1 Like

Julia - Think I may have a copy of that book, though likely first edition.

Arthur - Thanks for the suggestion.

Tim - Point taken. Thanks

Jim