O
okrus
Guest
Hi,
I made a similar post couple weeks ago. I've come a lillte farther, but the problem is nor solved yet.
My C# app controls two stepper with two different Arduinoes, each with its own COM port. I want the app to connect to the correct COM port automatically.
1. C# must first find all available COM ports.
2. Send a string "s" to all available COM ports and only use the two COM ports that respond with "Elevation" and "Azimuth".
For example .... If C# sends a string "s" to COM3 and and COM3 responds with "Azimuth" then AzimuthPort.PortName = "COM3";.
My problem is that I can find all available COM ports, but I can't send them a command because the same COM port cannot be accessed from multiple locations.
The code I'm using:
private void detectCOMPort()
{
detectPort = new SerialPort();
// Get a list of available COM ports
string[] ports = SerialPort.GetPortNames();
// Display each port name
foreach (string port in ports)
{
detectPort.PortName = port;
try
{
detectPort.Open();
detectPort.DiscardInBuffer();
detectPort.Write("s");
//
// Allow time for Arduino to respond
//
Thread.Sleep(200);
//
// Read response.
//
String compare = detectPort.ReadExisting();
if (compare == "Elevation\r\n")
{
myPort.PortName = port;
detectPort.Close();
}
if (compare == "Azimuth\r\n")
{
myPort2.PortName = port;
detectPort.Close();
}
if (detectPort.IsOpen)
{
detectPort.Close();
}
//COM (Elevation)
myPort = new SerialPort();
myPort.BaudRate = 9600;
//myPort.PortName = "";
myPort.Parity = Parity.None;
myPort.StopBits = StopBits.One;
myPort.DataBits = 8;
myPort.Handshake = Handshake.None;
myPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
myPort.Open();
myPort.DiscardOutBuffer();
myPort.DiscardInBuffer();
//COM (Azimuth)
myPort2 = new SerialPort();
myPort2.BaudRate = 9600;
//myPort2.PortName = "";
myPort2.Parity = Parity.None;
myPort2.StopBits = StopBits.One;
myPort2.DataBits = 8;
myPort2.Handshake = Handshake.None;
myPort2.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler2);
myPort2.Open();
myPort2.DiscardOutBuffer();
myPort2.DiscardInBuffer();
}
catch
{
//
// Port already open in another app ?
//
}
}
}
I get this error message even though I try to close "detectPort" after the first com port is set. The first COM port is set to the correct Arduino, but when I set the next COM port, I get the error message shown below. What am I doing wrong? Is there a better way to solve the problem?
Any help would be strongly appreciated.
Continue reading...
I made a similar post couple weeks ago. I've come a lillte farther, but the problem is nor solved yet.
My C# app controls two stepper with two different Arduinoes, each with its own COM port. I want the app to connect to the correct COM port automatically.
1. C# must first find all available COM ports.
2. Send a string "s" to all available COM ports and only use the two COM ports that respond with "Elevation" and "Azimuth".
For example .... If C# sends a string "s" to COM3 and and COM3 responds with "Azimuth" then AzimuthPort.PortName = "COM3";.
My problem is that I can find all available COM ports, but I can't send them a command because the same COM port cannot be accessed from multiple locations.
The code I'm using:
private void detectCOMPort()
{
detectPort = new SerialPort();
// Get a list of available COM ports
string[] ports = SerialPort.GetPortNames();
// Display each port name
foreach (string port in ports)
{
detectPort.PortName = port;
try
{
detectPort.Open();
detectPort.DiscardInBuffer();
detectPort.Write("s");
//
// Allow time for Arduino to respond
//
Thread.Sleep(200);
//
// Read response.
//
String compare = detectPort.ReadExisting();
if (compare == "Elevation\r\n")
{
myPort.PortName = port;
detectPort.Close();
}
if (compare == "Azimuth\r\n")
{
myPort2.PortName = port;
detectPort.Close();
}
if (detectPort.IsOpen)
{
detectPort.Close();
}
//COM (Elevation)
myPort = new SerialPort();
myPort.BaudRate = 9600;
//myPort.PortName = "";
myPort.Parity = Parity.None;
myPort.StopBits = StopBits.One;
myPort.DataBits = 8;
myPort.Handshake = Handshake.None;
myPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
myPort.Open();
myPort.DiscardOutBuffer();
myPort.DiscardInBuffer();
//COM (Azimuth)
myPort2 = new SerialPort();
myPort2.BaudRate = 9600;
//myPort2.PortName = "";
myPort2.Parity = Parity.None;
myPort2.StopBits = StopBits.One;
myPort2.DataBits = 8;
myPort2.Handshake = Handshake.None;
myPort2.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler2);
myPort2.Open();
myPort2.DiscardOutBuffer();
myPort2.DiscardInBuffer();
}
catch
{
//
// Port already open in another app ?
//
}
}
}
I get this error message even though I try to close "detectPort" after the first com port is set. The first COM port is set to the correct Arduino, but when I set the next COM port, I get the error message shown below. What am I doing wrong? Is there a better way to solve the problem?
Any help would be strongly appreciated.
Continue reading...