4 using System.Threading.Tasks;
16 private readonly
int baudRate;
17 private SerialPort currentPort;
46 this.baudRate = baudRate;
47 if (String.IsNullOrEmpty(portString))
52 this.currentPort =
new SerialPort(this.
PortString, baudRate) {ReadTimeout = 2000};
55 if (null != this.currentPort)
56 this.currentPort.ErrorReceived += this.CurrentPortOnErrorReceived;
62 private void CurrentPortOnErrorReceived(
object sender, SerialErrorReceivedEventArgs serialErrorReceivedEventArgs)
64 if (null != this.currentPort)
66 this.currentPort.ErrorReceived -= this.CurrentPortOnErrorReceived;
67 this.currentPort.Dispose();
68 this.currentPort = null;
71 this.currentPort =
new SerialPort(this.
PortString, this.baudRate) { ReadTimeout = 2000 };
73 if (null != this.currentPort)
74 this.currentPort.ErrorReceived += this.CurrentPortOnErrorReceived;
87 string[] ports = SerialPort.GetPortNames();
88 foreach (
string port
in ports)
90 string testPort = port;
91 Task<string> detectPort = Task<string>.Factory.StartNew(() =>
94 SerialPort serialPort =
new SerialPort(testPort, this.baudRate) {ReadTimeout = 2000};
95 return this.DetectArduino(serialPort) ? testPort : String.Empty;
98 if (detectPort.Wait(5000))
101 string detectedPort = detectPort.Result;
103 if (String.IsNullOrEmpty(detectedPort))
111 this.currentPort =
new SerialPort(testPort, this.baudRate) {ReadTimeout = 2000};
222 private bool DetectArduino(SerialPort serialPort)
227 byte[] buffer =
new byte[1];
234 serialPort.Write(buffer, 0, 1);
238 int intReturnAscii = serialPort.ReadByte();
242 catch (TimeoutException)
256 if(serialPort.IsOpen)
274 private bool SendCommand(
int command,
int parameter = -1,
int retry = 5)
278 byte[] buffer = parameter == -1 ?
new byte[1] :
new byte[2];
280 buffer[0] = Convert.ToByte(command);
282 buffer[1] = Convert.ToByte(parameter);
283 this.currentPort.Open();
284 this.currentPort.Write(buffer, 0, parameter == -1 ? 1 : 2);
286 return this.currentPort.ReadByte() == CommandRepresentations.ACK;
288 catch (TimeoutException)
293 catch (UnauthorizedAccessException uax)
303 return this.SendCommand(command, parameter, retry - 1);
313 if (this.currentPort.IsOpen)
314 this.currentPort.Close();
const int RotateTurnTableCcwCommand
The rotate turn table counter clockwise command
void ActivateLight(bool on, int hue)
Activates the light.
const int DisableLightCommand
The disable light command
Class for detecting an arduino and sending commands to it
void RotateLaserCw(int steps)
Rotates the laser clockwise.
const int RotateLaserCwCommand
The rotate laser clockwise command
const int DisableMicroSteppingCommand
The disable micro stepping command
void DisableMicroStepping()
Disables the micro stepping.
const int RotateTurnTableCwCommand
The rotate turn table clockwise command
Class for creating a singleton for a generic class
const int Ping
Ping constant
const int EnableMicroSteppingCommand
The enable micro stepping command
void EnableMicroStepping()
Enables the micro stepping.
const int RotateLaserCcwCommand
The rotate laser counterclockwise command
Representations of commands and states to control arduino sketch
const int DisableLaserCommand
The disable laser command
const int EnableLaserCommand
The enable laser command
void RotateTurnTableCcw(int steps)
Rotates the turn table counter clockwise.
void RotateTurnTableCw(int steps)
Rotates the turn table clockwise.
string PortString
Gets the port string.
static T Instance
Gets the instance.
void ActivateLaser(bool on=true)
Activates the laser.
ArduinoCommands(int baudRate, string portString="")
Initializes a new instance of the ArduinoCommands class.
void RotateLaserCcw(int steps)
Rotates the laser counter clockwise.
const int EnableLightCommand
The enable light command
string FindComPort()
Finds the COM port used by arduino
bool PortFound
Gets a value indicating whether [port found].
const int ACK
ACK constant