Correct use of "static" Serializer object in C#?
Navigates to RoboticsConnection.com Home RoboticsConnection.com HomePage
RoboticsConnection User Forum
Home       Members    Calendar    Who's On
Welcome Guest ( Login | Register )
        



Correct use of "static" Serializer object in... Expand / Collapse
Author
Message
Posted Wednesday, April 23, 2008 3:31 PM
Forum Member

Forum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum 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
Post #432
Posted Thursday, April 24, 2008 10:36 AM


Supreme Being

Supreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme 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

Post #441
Posted Thursday, April 24, 2008 3:20 PM
Forum Member

Forum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum Member

Group: Forum Members
Last Login: Monday, December 01, 2008 8:40 AM
Posts: 40, Visits: 190
Thanks Jason--that helps immensely!

--patrick

Post #448
« Prev Topic | Next Topic »


Reading This Topic Expand / Collapse
Active Users: 0 (0 guests, 0 members, 0 anonymous members)
No members currently viewing this topic.
Forum Moderators: jsummerour

Permissions Expand / Collapse

All times are GMT -8:00, Time now is 2:48pm

Powered By InstantForum.NET v4.1.4 © 2008
Execution: 0.250. 13 queries. Compression Disabled.