No connection to Arduino from Win 10 64 1803 C# program

Hello,

I have written a script for the Arduino to run a bipolar stepper motor.
If I test this script via the Seral Monitor, it work very fine.
But I cannot manage to send a command to the Arduino from my C# program

The essential C# code is:

try
{
doMove = Task.Run(() => MoveOneFrameArduino());
// Create the task to do the transport
}
catch (Exception ex)
{
Console.WriteLine("FG 679 extype = " + ex.GetType().ToString());
}

.....

public void MoveOneFrameArduino()
{
try
{
exceptionType = " ";

Console.WriteLine("FG 885 About to send command");
result = CineFrameGrabber.theSerialCOMPorts.WriteCommandReadResponse(CineFrameGrabber.actInstance.moveCommand);
CineFrameGrabber.theLogFile.WriteLogRecord("Command = "+ CineFrameGrabber.actInstance.moveCommand + ", Result value triggered by sent command =" + result);
Console.WriteLine("FG 887 After send command result = " + result);

if (result == false)
{
CineFrameGrabber.actInstance.SetText("ARDUINO: " + languages.GetM(051)); // catching error
}
else
{
Thread.Sleep(CineFrameGrabber.transDelayI);
}
}
catch (Exception ex)
{
Console.WriteLine("FG 679 extype = " + ex.GetType().ToString());
}
}

// important part of WriteCommandReadResponse
.........

if (_isOpen == true)
{
dataAvailable = false;
// activePort.Write(commandBytes, 0, cmdLength);
activePort.WriteLine(moveCmd); // move command is "FF000200002151"
activePort.DiscardInBuffer();
Console.WriteLine("SCP 662 movecommand no of bytes written = " + moveCmd.Length);
actInData = "";
try
{
doRead = Task.Run(() => Read()); // Function see below !
// Create the task to do the read of the arduino response
// this is to use a seperate kernel of the processor to do the communication with the ARDUINO
}
catch (Exception ex)
{
CineFrameGrabber.theLogFile.WriteLogRecord("Read Exception: ex = " + ex);
Console.WriteLine("SCP 680 extype = " + ex.GetType().ToString());
}
Task.WaitAll(new Task[] { doRead });
Console.WriteLine("SCP 683 transport attempt endend actInData="+ actInData);

}

........
public static void Read()
{
Console.WriteLine("SCP SendCommand 883 - try to read a response");
_continue = true;
string message = "";
while (_continue)
{
try
{
message = activePort.ReadLine();
if (message.Length == 0)
{
_continue = true;
}
else
{
Console.WriteLine("SCP SendCommand 897 after ReadLine() . message=" + message + ".");
if (Program.doLogging) { CineFrameGrabber.theLogFile.WriteLogRecord("Read 898" + message); }
if (message.Contains("1111") == true)
{
actInData = message.Trim();
_continue = false;
}
else
{
_continue = true;
}
}

}
catch (Exception ex)
{
Console.WriteLine("SCP 914 ex="+ex);
if (Program.doLogging) { CineFrameGrabber.theLogFile.WriteLogRecord("Read 914 message exception = "+ex.GetType()+", msg="+message+"."); }
actInData = "";
_continue = false;
}
}
}

Whatever I do ( I have tried byte oriented sending an character oriented sendig ) I always get a Timeout and nothing is receive on the Arduino. And also nothing is returned.

// This is read routine in the script:

...
...
int endPos = readline(Serial.read(), buffer, 14);
.....

int readline(int readch, char *buffer, int len)
{ // Readline Hilfsfunktion
static int pos = 0;
int rpos;

if (readch > 0)
{
switch (readch)
{
case '\n': // Ignore new-lines / neue Zeile ignorieren
case '\r': // Return on CR / Zeilenrücklauf ignorieren
rpos = pos;
pos = 0; // Reset position index ready for next time
// Positionsindex auf null für nächsten Durchlauf
return rpos;
break;
default:
if (pos < len - 1)
{
buffer[pos++] = readch;
buffer[pos] = 0;
}
break;
}
}
// no line end found ( /r ), therefore return minus one
return -1;
}

I have also tried the test program provided by Solid Soils .NET Library.
If I invoke the App to determine the existing conncetions, I get the message:
No connections availabel although the Arduino is correctly attachend to COM12(via USB9
and teh FTDI drivers are installed.

What can be wrong.

I am totally lost.

Wolfgnag

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn't show the posting toolbar then you can just manually add the code tags:
[code]``[color=blue]// your code is here[/color]``[/code]
Using code tags and other important information is explained in the How to use this forum post. Please read it.

When a serial connection is made to your Arduino via the USB port it resets the Arduino. After that there is a delay of for the bootloader to run before your code starts running. So if your C# code opens a serial connection and then immediately tries to start communication it won't work. That could match with the timeout you're having.

The software workaround would be to wait some time after opening the serial connection (how long depends on which Arduino board you're using because they use different bootloaders).

The hardware solution would be to disable the auto-reset behavior. You can do this temporarily by putting a 10 uF capacitor between the reset and ground pins on the Arduino board. Another way to do it is by cutting the RESET-EN solder jumper on your Arduino board, which can be undone later by putting a bead of solder across the cut. Be aware that the reason for the auto-reset circuit is to allow uploading sketches to your Arduino board. If you disable auto-reset you will need to manually reset the board at just the right time as the upload starts (after compilation finishes).

Another workaround would be to connect a separate USB to TTL serial adapter to the serial pins on your Arduino. You can open a serial connection via that adapter without resetting the board.

Where is that error message coming from?

I don't see that string in your code, so some other piece of code that you're calling is failing at something and printing the message shown in the screenshot. (I search your post for the misspelled word "shure" and it is not there - being a misspelling, it's probably a good term to search for to find the code in question) Find the code where that error message is, and determine what test fails that causes it to print that.

Is it looking at VID/PID? (would fail for clones) What windows calls the port? A certain COM number? (you can configure this) Does that error come up before or after it opens the port? If after, under what conditions? (if it's not even getting to where it opens the port, it cannot be what pert suggests, whereas if it is after the port is opened, pert's theory is far more likely).

The information that we need in order to debug this problem is absent from the post.