Hi All
I’m having difficulty getting the Arduino serial output to update based on the data sent to the com port.
When I send the first instruction the Arduino responds as expected.
I then send a new instruction with different data and the Arduino returns the information related to the first instruction, not the new data.
I am sending the the instruction from a winforms app (C#) and reading the data back into the same app.
This is the received string from the app
#A800010001500004000000000051\n
These are the values returned by the Arduino based on the above
Dir: A
Max Speed: 8000
Speed: 1000
Accel: 1500
Step: 00400
Angle: 000
Rotations: 000005
Abort: 1
Rotations as int: 5
If i change my command to
#B800010001500004000000000051\n
The returned data does not reflect the change to the first char following the #
#include <AccelStepper.h>
AccelStepper motor(1, 2, 12); //1 = Driver mode, 2 = Pulse pin, 12 = direction pin
//-----For Serial Read-----
const byte buffSize = 30;
char serialCommand[buffSize];
const char endMarker = '\n';
byte bytesRecvd = 0;
bool readBuffer;
bool writeOutValues;
char cDir[2];
char cMaxSpeed[5];
char cSpeed[5];
char cAngle[4];
char cAccel[5];
char cStepsPerRev[6];
char cRotations[7];
char cAbortFlag[2];
void setup()
{
writeOutValues = false;
Serial.begin(115200);
Serial.println("Starting Motor Driver V1.0");
}
void loop()
{
readSerial();
serialData();
}
void readSerial()
{
if (Serial.available() == 0) {
return;
}
char myChar = Serial.read();
if (myChar == '#')
{
readBuffer = true;
serialCommand[0] = '\0';
}
//Set a start char and check if present
if (readBuffer == true)
{
if (myChar != endMarker) {
serialCommand[bytesRecvd] = myChar;
bytesRecvd ++;
if (bytesRecvd == buffSize) {
bytesRecvd = buffSize - 1;
}
}
else { // myChar is the endMarker
serialCommand[bytesRecvd] = 0;
}
}
if (myChar == endMarker)
{
readBuffer = false;
writeOutValues = true;
}
}
void serialData()
{
if (writeOutValues == true)
{
cDir[0]= '\0';
cMaxSpeed[0]= '\0';
cSpeed[0]= '\0';
cAccel[0]= '\0';
cStepsPerRev[0]= '\0';
cAngle[0]= '\0';
cRotations[0]= '\0';
cAbortFlag[0]= '\0';
Serial.println("Serial data function has been executed");
Serial.println();
Serial.print("Command: ");
Serial.println(serialCommand);
Serial.println();
cDir[0] = serialCommand[1];
Serial.print("Dir: ");
Serial.println(cDir);
cMaxSpeed[0] = serialCommand[2];
cMaxSpeed[1] = serialCommand[3];
cMaxSpeed[2] = serialCommand[4];
cMaxSpeed[3] = serialCommand[5];
Serial.print("Max Speed: ");
Serial.println(cMaxSpeed);
cSpeed[0] = serialCommand[6];
cSpeed[1] = serialCommand[7];
cSpeed[2] = serialCommand[8];
cSpeed[3] = serialCommand[9];
Serial.print("Speed: ");
Serial.println(cSpeed);
cAccel[0] = serialCommand[10];
cAccel[1] = serialCommand[11];
cAccel[2] = serialCommand[12];
cAccel[3] = serialCommand[13];
Serial.print("Accel: ");
Serial.println(cAccel);
cStepsPerRev[0] = serialCommand[14];
cStepsPerRev[1] = serialCommand[15];
cStepsPerRev[2] = serialCommand[16];
cStepsPerRev[3] = serialCommand[17];
cStepsPerRev[4] = serialCommand[18];
Serial.print("Step: ");
Serial.println(cStepsPerRev);
cAngle[0] = serialCommand[19];
cAngle[1] = serialCommand[20];
cAngle[2] = serialCommand[21];
Serial.print("Angle: ");
Serial.println(cAngle);
cRotations[0] = serialCommand[22];
cRotations[1] = serialCommand[23];
cRotations[2] = serialCommand[24];
cRotations[3] = serialCommand[25];
cRotations[4] = serialCommand[26];
cRotations[5] = serialCommand[27];
int n;
n = atoi(cRotations);
Serial.print("Rotations: ");
Serial.println(cRotations);
cAbortFlag[0] = serialCommand[28];
Serial.print("Abort: ");
Serial.println(cAbortFlag);
Serial.println();
Serial.print("Rotations as int: ");
Serial.println(n);
writeOutValues = false;
}
}
Any help that you can give would be much appreciated.