| CodeGuru Home | VC++ / MFC / C++ | .NET / C# | Visual Basic | Newsletters | VB Forums | Developer.com |
|
|||||||
| C-Sharp Programming Post questions, answers, and comments about C#. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
|
#2
|
|||
|
|||
|
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
}
}
|
|
#3
|
|||
|
|||
|
Re: Event handling and delegates with SerialPort
Quote:
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);
}
}
}
|
|
#4
|
|||
|
|||
|
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
}
}
|
![]() |
| Bookmarks |
| Tags |
| serialport event delegate |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|