I have a winform tool that interacts with hardware using a serial port.
The serial port is used to send commands to hardware that will acknowledge commands and sometimes return data.
To complete the task, several commands must be sent to the hardware in a specific order.
I have successfully implemented the tool using the switch statement to control the sending of commands. However, although this works, I cannot help but think that there is a better, easier way to do this - is there?
The current implementation is below:
Each case is a team that requires sending to the equipment. The Send method is passed the calling method and the identifier of the next step - which will be returned if the command was sent successfully.
- The tool sends a session start command to the hardware.
- Hardware confirms the command.
- The tool sends a command to set the direction to the hardware.
- Hardware will confirm the command.
and etc.
MessageID nextStep = MessageID.IMS;
while (nextStep != MessageID.Stop)
{
switch (nextStep)
{
case MessageID.ISS:
nextStep = Send( new ISS_StartSession(), MessageID.IE386);
RaiseProgressEvent(10);
break;
case MessageID.IE386:
nextStep = Send( new IE386_SetDirection(Direction.BOTH), MessageID.IE378);
RaiseProgressEvent(20);
break;
case MessageID.Error:
HandleError();
break;
}
}
Is there a better way to do this ??
Is there a well-known design that I should look at?
source
share