Simply3DScan
ArduinoConnection.cs
Go to the documentation of this file.
1 using System;
2 using System.IO.Ports;
3 using Configuration;
4 using Logging;
5 using SharedObjects;
6 
7 namespace ArduinoConnection
8 {
12  public class ArduinoConnection
13  {
14  private SerialPort currentPort;
21  public bool PortFound { get; private set; }
28  public string PortString { get; private set; }
29 
34  public ArduinoConnection(string portString = "")
35  {
36  if (String.IsNullOrEmpty(portString))
37  FindComPort();
38  else
39  {
40  PortString = portString;
41  currentPort = new SerialPort(PortString, Configuration.Configuration.Information.Arduino.BaudRate) { ReadTimeout = 1000 };
42  }
43  }
44 
50  public string FindComPort()
51  {
52  try
53  {
54  string[] ports = SerialPort.GetPortNames();
55  foreach (string port in ports)
56  {
57  currentPort = new SerialPort(port, Configuration.Configuration.Information.Arduino.BaudRate);
58  if (DetectArduino())
59  {
60 
61  PortFound = true;
62  return PortString = port;
63  }
64  PortFound = false;
65  }
66  }
67  catch (Exception ex)
68  {
69  Singleton<Logger>.Instance.LogException(ex);
70  }
71  return string.Empty;
72  }
73 
78  public void ActivateLaser(bool on = true)
79  {
80  SendCommand(@on ? ArduinoConfiguration.EnableLaserCommand : ArduinoConfiguration.DisableLaserCommand);
81  }
87  public void ActivateLight(bool on, int hue)
88  {
89  if(on)
90  SendCommand(ArduinoConfiguration.EnableLightCommand, hue);
91  else
92  SendCommand(ArduinoConfiguration.DisableLightCommand);
93  }
94 
99  public void RotateTurnTableCw(int steps)
100  {
101  SendCommand(ArduinoConfiguration.RotateTurnTableCwCommand, steps);
102  }
103 
108  public void RotateTurnTableCcw(int steps)
109  {
110  SendCommand(ArduinoConfiguration.RotateTurnTableCcwCommand, steps);
111  }
112 
117  public void RotateLaserCw(int steps)
118  {
119  SendCommand(ArduinoConfiguration.RotateLaserCwCommand, steps);
120  }
121 
126  public void RotateLaserCcw(int steps)
127  {
128  SendCommand(ArduinoConfiguration.RotateLaserCcwCommand, steps);
129  }
130 
134  public void EnableMicroStepping()
135  {
136  SendCommand(ArduinoConfiguration.EnableMicroSteppingCommand);
137  }
138 
142  public void DisableMicroStepping()
143  {
144  SendCommand(ArduinoConfiguration.DisableMicroSteppingCommand);
145  }
146  private bool DetectArduino()
147  {
148  try
149  {
150  //The below setting are for the Hello handshake
151  byte[] buffer = new byte[1];
152  buffer[0] = Convert.ToByte(ArduinoConfiguration.Ping);
153  currentPort.Open();
154  currentPort.Write(buffer, 0, 1);
155  try
156  {
157  int intReturnAscii = currentPort.ReadByte();
158  return intReturnAscii == ArduinoConfiguration.ACK;
159  }
160  catch (TimeoutException) // occurs if no response is there after 1000 ms.
161  {
162  Singleton<Logger>.Instance.LogInfo(String.Format("Not detected on port {0}", currentPort));
163  return false;
164  }
165  }
166  catch (Exception ex)
167  {
168  Singleton<Logger>.Instance.LogException(ex);
169  }
170  finally
171  {
172  if(currentPort.IsOpen)
173  currentPort.Close();
174  }
175 
176  return false;
177  }
178 
179  private bool SendCommand(int command, int parameter = -1)
180  {
181  try
182  {
183  byte[] buffer = parameter == -1 ? new byte[1] : new byte[2];
184 
185  buffer[0] = Convert.ToByte(command);
186  if (parameter != -1)
187  buffer[1] = Convert.ToByte(parameter);
188  currentPort.Open();
189  currentPort.Write(buffer, 0, parameter == -1 ? 1 : 2);
190 
191  return currentPort.ReadByte() == ArduinoConfiguration.ACK;
192  }
193  catch (TimeoutException)
194  {
195  Singleton<Logger>.Instance.LogInfo(String.Format("Command took longer then expected: {0}", command));
196  return false;
197  }
198  catch (Exception ex)
199  {
200  Singleton<Logger>.Instance.LogException(ex);
201  return false;
202  }
203  finally
204  {
205  if (currentPort.IsOpen)
206  currentPort.Close();
207  }
208  }
209  }
210 }
Class holding the configuration information
Definition: Information.cs:8
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
Definition: Singleton.cs:9
void RotateTurnTableCcw(int steps)
Rotates the turn table counter clockwise.
Definition: Logger.cs:5
void RotateTurnTableCw(int steps)
Rotates the turn table clockwise.
static T Instance
Gets the instance.
Definition: Singleton.cs:27
string FindComPort()
Finds the COM port used by arduino
int BaudRate
Gets or sets the baud rate.
Definition: Arduino.cs:23
void DisableMicroStepping()
Disables the micro stepping.
Arduino Arduino
Gets or sets the arduino.
Definition: Information.cs:45
void EnableMicroStepping()
Enables the micro stepping.