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