I am just a beginner in c#. I am now trying to interface arduino with a GUI application. And i need a small function to automatically detect the port which I have connected Arduino. I tried using nested "try and catch" blocks but it failed. can anyone suggest a good way to automatic select the port in which arduino is connected and open that port such that we can move directly to coding other switches that do different functions in that arduino.
Asked
Active
Viewed 983 times
0
-
You should add a code sample to show what you've got so far. – Simon W Jan 20 '15 at 05:52
3 Answers
1
Recently i had the same situation and i wrote this method to check for our device, all you need to set your device to send specific Pattern on Specific input. In this example if you send 0x33 then your device have to send 0x8A to identify itself.
public enum SerialSignal
{
SendSync = 0x33,
ReceiveSync = 0x8A,
}
private static SerialPort _arduinoSerialPort ;
/// <summary>
/// Polls all serial port and check for our device connected or not
/// </summary>
/// <returns>True: if our device is connected</returns>
public static bool Initialize()
{
var serialPortNames = SerialPort.GetPortNames();
foreach (var serialPortName in serialPortNames)
{
try
{
_arduinoSerialPort = new SerialPort(serialPortName) { BaudRate = 9600 };
_arduinoSerialPort.Open();
_arduinoSerialPort.DiscardInBuffer();
_arduinoSerialPort.Write(new byte[] { (int)SerialSignal.SendSync }, 0, 1);
var readBuffer = new byte[1];
Thread.Sleep(500);
_arduinoSerialPort.ReadTimeout = 5000;
_arduinoSerialPort.WriteTimeout = 5000;
_arduinoSerialPort.Read(readBuffer, 0, 1);
// Check if it is our device or Not;
if (readBuffer[0] == (byte)SerialSignal.ReceiveSync){
return true;
}
}
catch (Exception ex)
{
Debug.WriteLine("Exception at Serial Port:" + serialPortName + Environment.NewLine +
"Additional Message: " + ex.Message);
}
// if the send Sync repply not received just release resourceses
if (_arduinoSerialPort != null) _arduinoSerialPort.Dispose();
}
return false;
}

Mujahid Daud Khan
- 1,983
- 1
- 14
- 23
-
I'd prefer a parallel check instead of a sequential one. Because... On some PCs i have 30+ serial ports and, if I have to wait 5 seconds for each, it can take a veeeeery long time (considering it is just an initialization). You can wrap the communication code inside a backgroundworker and launch several BGW in parallel.. – frarugi87 Jan 20 '15 at 12:14
-
-
@frarugi87 Yes, you are right, its a nice idea to make use of TPL. – Mujahid Daud Khan Jan 22 '15 at 05:27
-
@ANOOPM BGW=Backgroud Workers, you can find more information in this [question](http://stackoverflow.com/questions/12251874/how-use-parallel-foreach-loop-instead-of-regular-foreach-info-about-parallel-f) – Mujahid Daud Khan Jan 22 '15 at 05:28
0
public partial class Form1 : Form { SerialPort serial = new SerialPort(); static SerialPort cport; public Form1() { InitializeComponent(); button1.Enabled = true; button2.Enabled = false; button3.Enabled = false;
}
private void button1_Click(object sender, EventArgs e)
{
int i;
try
{
string[] ports = SerialPort.GetPortNames();
foreach(string newport in ports)
{
cport = new SerialPort(newport, 9600);
cport.Open();
cport.WriteLine("A");
int intReturnASCII = serial.ReadByte();
char returnMessage = Convert.ToChar(intReturnASCII);
if (returnMessage == 'B')
{
button2.Enabled = true;
break;
}
else
{
cport.Close();
}
}
}
catch (Exception )
{
Console.WriteLine("No COM ports found");
}
}

ANOOP M
- 1
0
I undertand that I'm a bit late, But I have created a simple and free C# NuGet library that allows for interaction between the host PC and an Arduino board!
Examples in the ReadMe.txt file.

Nikolay Kostov
- 16,433
- 23
- 85
- 123

Bogdan0804
- 27
- 1
- 5