Created
January 27, 2013 16:31
-
-
Save fulminator/4649120 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Windows; | |
using System.Windows.Controls; | |
using System.Windows.Data; | |
using System.Windows.Documents; | |
using System.Windows.Input; | |
using System.Windows.Media; | |
using System.Windows.Media.Imaging; | |
using System.Windows.Navigation; | |
using System.Windows.Shapes; | |
using System.IO; | |
using System.IO.Ports; | |
using System.Threading; | |
namespace serialTest | |
{ | |
/// <summary> | |
/// Interaction logic for MainWindow.xaml | |
/// </summary> | |
public partial class MainWindow : Window | |
{ | |
public MainWindow() | |
{ | |
InitializeComponent(); | |
} | |
private void conectare_bt_Click(object sender, RoutedEventArgs e) | |
{ | |
ArduinoControllerMain controler = new ArduinoControllerMain(); | |
try | |
{ | |
controler.SetComPort(); | |
if (controler.portFound == true) | |
{ | |
info_sb.Text = "Conectat la portul Arduino " + controler.currentPort.PortName.ToString() + "."; | |
} | |
} | |
catch (Exception exc) | |
{ | |
MessageBox.Show(exc.ToString()); | |
} | |
} | |
} | |
// clasa principala pentru controlul arduino | |
public class ArduinoControllerMain | |
{ | |
private string tempPortName; | |
public SerialPort currentPort; | |
public bool portFound; | |
// metoda pentru setarea portului | |
public void SetComPort() | |
{ | |
try | |
{ | |
string[] ports = SerialPort.GetPortNames(); | |
foreach (string port in ports) | |
{ | |
currentPort = new SerialPort(port, 9600); | |
if (DetectArduino()) | |
{ | |
portFound = true; | |
tempPortName = port; | |
break; | |
} | |
else | |
{ | |
portFound = false; | |
} | |
currentPort.PortName = port; | |
} | |
} | |
catch (Exception e) | |
{ | |
MessageBox.Show(e.ToString()); | |
} | |
} // end of SetComPort() | |
// metoda pentru detectarea arduino prin handshake | |
private bool DetectArduino() | |
{ | |
try | |
{ | |
// handshake-ul este format dintr-un mesaj de 5 bytes: 16, 128, 0, 0, 4 | |
byte[] buffer = new byte[5]; | |
buffer[0] = Convert.ToByte(16); | |
buffer[1] = Convert.ToByte(128); | |
buffer[2] = Convert.ToByte(0); | |
buffer[3] = Convert.ToByte(0); | |
buffer[4] = Convert.ToByte(4); | |
int intReturnASCII = 0; | |
char charReturnValue = (Char)intReturnASCII; | |
currentPort.Open(); | |
currentPort.Write(buffer, 0, 5); | |
Thread.Sleep(1000); | |
int count = currentPort.BytesToRead; | |
string returnMessage = ""; | |
while (count > 0) | |
{ | |
intReturnASCII = currentPort.ReadByte(); | |
returnMessage = returnMessage + Convert.ToChar(intReturnASCII); | |
count--; | |
} | |
//ComPort.name = returnMessage; // oficial line. gives error. perhaps currentPort instead of ComPort | |
currentPort.Close(); | |
if (returnMessage.Contains("HELLO FROM ARDUINO")) | |
{ | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
catch (Exception e) | |
{ | |
MessageBox.Show(e.ToString()); | |
return false; | |
} | |
} // end of DetectArduino() | |
} // end of class ArduinoControllerMain | |
} // end of namespace |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment