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))
110 this.currentPort =
new SerialPort(testPort, this.baudRate) {ReadTimeout = 2000};
221 private bool DetectArduino(SerialPort serialPort)
226 byte[] buffer =
new byte[1];
233 serialPort.Write(buffer, 0, 1);
237 int intReturnAscii = serialPort.ReadByte();
241 catch (TimeoutException)
255 if(serialPort.IsOpen)
273 private bool SendCommand(
int command,
int parameter = -1,
int retry = 5)
277 byte[] buffer = parameter == -1 ?
new byte[1] :
new byte[2];
279 buffer[0] = Convert.ToByte(command);
281 buffer[1] = Convert.ToByte(parameter);
282 this.currentPort.Open();
283 this.currentPort.Write(buffer, 0, parameter == -1 ? 1 : 2);
285 return this.currentPort.ReadByte() == CommandRepresentations.ACK;
287 catch (TimeoutException)
292 catch (UnauthorizedAccessException uax)
302 return this.SendCommand(command, parameter, retry - 1);
312 if (this.currentPort.IsOpen)
313 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