|
|
|
Forum Member
      
Group: Forum Members
Last Login: Monday, December 01, 2008 8:40 AM
Posts: 40,
Visits: 190
|
|
Hi Jason (or anyone else who knows!),
I'm trying to declare my Serializer object in a C# application in such a way that I can add sensors and drive motor objects by way of their own classes. In other words, in my main Windows Forms application, I want to be able to do something like this:
mySerializer = new Serializer();
mySensors = new Sensors();
myDriveMotors = new DriveMotors();
where, for example, the Sensors class looks something like:
public class Sensors
{
public Ping sPing;
public Sensors()
{
sPing = new Ping(Serializer);
sPing.Pin = GpioPinId.Pin0;
}
}
and similarly for the Controller and DriveMotors classes. Then, in the rest of my application, I can refer to the Ping sensor as mySensors.sPing etc.
The Serializer class of course creates a new Serializer object and then runs StartCommunication(). My problem is that I can't figure out what needs to be static (or not) so that I don't get "null object references" when I create the Sensors and DriveMotors classes (which depend on the Serializer object). Similarly, I'm not sure if I call StartCommunication() before or after I instantiate the Sensors and DriveMotors classes.
I know if I put all the sensor and motor definitions in the same class file as the Serializer, then I can get things to work, but I'm trying to make my code a little more modular.
Does this make any sense???
--patrick
|
|
|
|
|
Supreme Being
      
Group: Administrators
Last Login: Monday, December 01, 2008 6:34 PM
Posts: 299,
Visits: 426
|
|
| Patrick, You can pass in a ref to your Serializer instance to the constructor for your custom classes. Something like: public class MySensors { private Ping p; private GP2D12 ir; public MySensors(ref Serializer s) { p = new Ping(s); ir = new GP2D12(s); // Configure the p and ir properties } public ReadMySensors() { // Do whatever you want Console.WriteLine("Ping: {0}, GP2D12: {1}", p.Distance, ir.Distance); } } Then, in main(), you can do: ... private Serializer s; private MySensors ms; public void main() { s = new Serializer(); s.PortName = "COM10"; s.BaudRate = 19200; ... ms = new MySensors(ref s); // Sign up for comm start event & start serializer communications while (true) { s.PumpEvents(); ms.ReadMySensors(); } } This is just psuedocode, but I'll bet you'll get the idea... You would do something similar for your custom motor class, etc. Best Regards,
Jason Summerour President, Summerour Robotics Corporation Microsoft MVP www.roboticsconnection.com
|
|
|
|
|
Forum Member
      
Group: Forum Members
Last Login: Monday, December 01, 2008 8:40 AM
Posts: 40,
Visits: 190
|
|
Thanks Jason--that helps immensely!
--patrick
|
|
|
|