14 private SerialPort currentPort;
21 public bool PortFound {
get;
private set; }
28 public string PortString {
get;
private set; }
36 if (String.IsNullOrEmpty(portString))
40 PortString = portString;
54 string[] ports = SerialPort.GetPortNames();
55 foreach (
string port
in ports)
62 return PortString = port;
80 SendCommand(@on ? ArduinoConfiguration.EnableLaserCommand : ArduinoConfiguration.DisableLaserCommand);
90 SendCommand(ArduinoConfiguration.EnableLightCommand, hue);
92 SendCommand(ArduinoConfiguration.DisableLightCommand);
101 SendCommand(ArduinoConfiguration.RotateTurnTableCwCommand, steps);
110 SendCommand(ArduinoConfiguration.RotateTurnTableCcwCommand, steps);
119 SendCommand(ArduinoConfiguration.RotateLaserCwCommand, steps);
128 SendCommand(ArduinoConfiguration.RotateLaserCcwCommand, steps);
136 SendCommand(ArduinoConfiguration.EnableMicroSteppingCommand);
144 SendCommand(ArduinoConfiguration.DisableMicroSteppingCommand);
146 private bool DetectArduino()
151 byte[] buffer =
new byte[1];
152 buffer[0] = Convert.ToByte(ArduinoConfiguration.Ping);
154 currentPort.Write(buffer, 0, 1);
157 int intReturnAscii = currentPort.ReadByte();
158 return intReturnAscii == ArduinoConfiguration.ACK;
160 catch (TimeoutException)
172 if(currentPort.IsOpen)
179 private bool SendCommand(
int command,
int parameter = -1)
183 byte[] buffer = parameter == -1 ?
new byte[1] :
new byte[2];
185 buffer[0] = Convert.ToByte(command);
187 buffer[1] = Convert.ToByte(parameter);
189 currentPort.Write(buffer, 0, parameter == -1 ? 1 : 2);
191 return currentPort.ReadByte() == ArduinoConfiguration.ACK;
193 catch (TimeoutException)
205 if (currentPort.IsOpen)
void ActivateLight(bool on, int hue)
Activates the light.
void ActivateLaser(bool on=true)
Activates the laser.
void RotateLaserCcw(int steps)
Rotates the laser counter clockwise.
void RotateLaserCw(int steps)
Rotates the laser clockwise.
ArduinoConnection(string portString="")
Initializes a new instance of the ArduinoConnection class.
Class for creating a singleton for a generic class
void RotateTurnTableCcw(int steps)
Rotates the turn table counter clockwise.
void RotateTurnTableCw(int steps)
Rotates the turn table clockwise.
static T Instance
Gets the instance.
string FindComPort()
Finds the COM port used by arduino
int BaudRate
Gets or sets the baud rate.
void DisableMicroStepping()
Disables the micro stepping.
void EnableMicroStepping()
Enables the micro stepping.