CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Deploying Windows Server 2008 with System Center
  • Remote Desktop Protocol Performance Improvements in Windows Server 2008 R2 and Windows 7
  • The Microsoft Dynamics CRM Security Model
  • SQL Server Modeling Services with Microsoft Visual Studio 2010 Beta 2

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > .NET Programming > C-Sharp Programming
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    C-Sharp Programming Post questions, answers, and comments about C#.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old October 2nd, 2009, 12:32 PM
    funkathustra funkathustra is offline
    Junior Member
     
    Join Date: Oct 2009
    Posts: 8
    funkathustra is an unknown quantity at this point (<10)
    Event handling and delegates with SerialPort

    I'm working on writing a C#/WPF application to control a hexapod robot over a serial port. Obviously, I need one instance of a SerialPort that I can issue commands to. I want to create a Leg class to abstract the movement of each leg. i.e., leg1.stepForward, leg1.stepBackward, etc.

    Those methods would write bytes to the serial port, preferably using a wrapper function (say, move(int servoNumber, int servoPosition)), which would translate the input into the actual bytecode that needs to be sent down the SerialPort object.

    So, essentially, I need to create a SerialPort object when the program launches, and pass commands to it through instantiations of the Leg class. I guess, sort of like a global function that the Leg classes can call -- but for performance issues, I don't want to have to open() and close() the port every time a call is issued.

    It seems like I would need to use an event handler/delegate for the class interaction, however, I need to keep the SerialPort object persistent, and from what I've read, I'd need to initialize a new event handler/delegate for each leg, so I couldn't put my SerialPort code in the handler (otherwise, I would initialize a new SerialPort object for every leg).

    Can anyone point me in the right direction? If someone could help me out with a skeleton structure of the class interaction, I'd really appreciate it.
    Reply With Quote
      #2    
    Old October 2nd, 2009, 01:26 PM
    BigEd781 BigEd781 is offline
    Senior Member
     
    Join Date: Jun 2008
    Posts: 1,194
    BigEd781 is a glorious beacon of light (400+) BigEd781 is a glorious beacon of light (400+) BigEd781 is a glorious beacon of light (400+) BigEd781 is a glorious beacon of light (400+) BigEd781 is a glorious beacon of light (400+) BigEd781 is a glorious beacon of light (400+)
    Re: Event handling and delegates with SerialPort

    So you just have a single serial port connection, right? I would probably start out by creating a high level class for my robot:

    Code:
    using System.IO.Ports;
    
    class Robot
    {
        private readonly SerialPort m_port;
     
        public Robot( string comPort )
        {
            // initialize m_port and keep it around
        }
    }
    Your Robot class would own instances of Legs, Arms, etc, and would be responsible for handling all communications. It could expose methods like Move( x, y ) to the outside world, but it would be responsible for doing all of the coordination between the parts. Of course, my designs always change once I actually start writing code, so it's kind of hard to say. Is that the kind of advice you were looking for?
    Reply With Quote
      #3    
    Old October 2nd, 2009, 02:08 PM
    funkathustra funkathustra is offline
    Junior Member
     
    Join Date: Oct 2009
    Posts: 8
    funkathustra is an unknown quantity at this point (<10)
    Re: Event handling and delegates with SerialPort

    Quote:
    Originally Posted by BigEd781 View Post
    So you just have a single serial port connection, right? I would probably start out by creating a high level class for my robot:

    Code:
    using System.IO.Ports;
    
    class Robot
    {
        private readonly SerialPort m_port;
     
        public Robot( string comPort )
        {
            // initialize m_port and keep it around
        }
    }
    Your Robot class would own instances of Legs, Arms, etc, and would be responsible for handling all communications. It could expose methods like Move( x, y ) to the outside world, but it would be responsible for doing all of the coordination between the parts. Of course, my designs always change once I actually start writing code, so it's kind of hard to say. Is that the kind of advice you were looking for?
    Yeah, that makes sense -- but how do I expose the function? I'm afraid I'll run into a circular dependency. My "move" function lives in the Robot class (because that's where the serial port is -- if I move it into my Leg class, I won't be able to access the SerialPort object, right?), but I obviously can't call it from my Leg class.

    Code:
    class Leg
    {
         int horizontalServo;
         int verticalServo;
    
         public Leg(int legNumber) {
             horizontalServo = legNumber * 2    //Every leg has two servos, so the horizontal servos are 0, 2, 4, 6, 8, 10
             verticalServo = (legNumber * 2) + 1 // The vertical servos are 1, 3, 5, 7, 9, 11
             move(verticalServo, 0);         // move the servos of the leg to their initial position. Where does this "move" function live, though?
             move(horizontalServo, 0);   
        }
    }
    
    class Robot
        {
            private readonly SerialPort m_port = new SerialPort("COM1", 9200, Parity.None, 8, StopBits.One);
    
            public void move(int servo, int position)
            {
                byte[] b = { 255, (byte)servo, (byte)position };
                m_port.Write(b, 0, 3);
            }
    
            public Robot()
            {
                m_port.Open();
                Leg leg1 = new Leg(1);
                Leg leg2 = new Leg(2);
                Leg leg3 = new Leg(3);
                Leg leg4 = new Leg(4);
                Leg leg5 = new Leg(5);
                Leg leg6 = new Leg(6);
            }
        }
    }
    I know this is remedial, so thanks for holding my hand through all this -- I'm a Electrical Engineering student possessing very little background in object-oriented design.
    Reply With Quote
      #4    
    Old October 2nd, 2009, 04:59 PM
    BigEd781 BigEd781 is offline
    Senior Member
     
    Join Date: Jun 2008
    Posts: 1,194
    BigEd781 is a glorious beacon of light (400+) BigEd781 is a glorious beacon of light (400+) BigEd781 is a glorious beacon of light (400+) BigEd781 is a glorious beacon of light (400+) BigEd781 is a glorious beacon of light (400+) BigEd781 is a glorious beacon of light (400+)
    Re: Event handling and delegates with SerialPort

    Well, one approach would be to pass a serial port into your move functions.

    Code:
    class Leg
    {
        public void IssueMove( SerialPort comPort, int degrees ) { ... }
    }
    
    class Robot
    {
        private readonly SerialPort m_comPort;
        private Leg m_rightLeg;
        // other private objects
    
        ... //constructor and whatnot
    
        public void Move( int x, int y )
        {
            m_rightLeg.IssueMove( m_comPort, ... );  
            // move other parts here    
        }
    }
    It's great to have a design to work to, but you will inevitably change things, so I would just start with the most simple design first and work from there.
    Reply With Quote
    Reply

    Bookmarks

    Tags
    serialport event delegate
    Go Back   CodeGuru Forums > .NET Programming > C-Sharp Programming


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 03:15 PM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.