This is basically my first post on the Arduino forum so hello everyone. I am currently working on my first Arduino project and have come to a point where my head is a bit spinning, too many ways to do something. So I am looking for some suggestions on the most efficient way to send and receive byte arrays to and from the Nano and PC.
Background:
Project is a coil winder using 2 stepper motors (winder & carriage), 2 x TB6600's, USB port, C# PC program for the grunt work, Arduino Nano (old bootloader), AccelStepper library.
There are 2 arrays going to the Nano and 1 coming back to the PC. The 3 arrays are Settings, Commands, and Indicators. I want to keep everything or as much as I can as simply as possible for others to use my project.
What I've done so far:
I've read all the basic serial stuff, read the more advanced stuff, researched on here and the internet and have come up with a mish-mash of ideas. I know that C# and the Nano transmit the ASCII byte values as integers and each converts them into characters for representation.
Main problem: Parsing the bytes.
My basic program flow; do all the heavy calculations on the PC, send those calculations(Settings to the Nano), jog the carriage into position and set the home location, start the coil build and have the Nano report on it's status/progress as the Indicator array. Speed of communications is NOT a major factor because the Nano will be handling the actual coil build. The arrays will be like this below:
Start of string, either - setting (S) or command (C) or indicator (I) char, data, end of string. All coma delimited.
Settings:
string StartS = "<";
string Sts = "S";
string strPPR_WindingM;
strPPR_WindingM = Convert.ToString(PPR_WindingM);
string EndS = ">";
String unicodeString = StartS + "," + Sts + "," + strPPR_WindingM + "," + dblPPR + "," + PPWind + "," + WPLayer + "," + STEP_RATIO + "," + PPLayer + "," + Speed + "," + Accel + "," + radBtnDir + "," + EndS;
For a total of 38 bytes.
Commands:
string StartS = "<";
string CMD = "C";
string strCarLEFT;
strCarLEFT = "Lft";
string EndS = ">";
//prep byte string
String unicodeString = StartS + "," + CMD + "," + strCarLEFT + "," + EndS;
Total of 9 bytes per separate command which is sent one command at a time as needed.
Indicators to PC:
LED Left, LED Right, LED Paused, LED Home Set, WINDER PULSE, CARRIAGE PULSE, WORKING ON WINDING #, WORKING ON LAYER #
-mixed binary like string.
eg. I,100111,######,###
So what I mainly need to do is to determine is the incoming data is a Setting or a Command. If a setting then store these values including the home position (command). Commands are mostly do until (Jogging). I have looked into and tried the strtok method a couple of different uses anyway. I was thinking of using a 'if' statement for the 'S'/'C' part and a switch statement for the 'C' commands part.
This code seems promising:
char* subStr (char* input_string, char *separator, int segment_number)
{
char *act, *sub, *ptr;
static char copy[100];
int i;
strcpy(copy, input_string);
for (i = 1, act = copy; i <= segment_number; i++, act = NULL)
{
sub = strtok_r(act, separator, &ptr);
if (sub == NULL) break;
}
return sub;
}
All suggestions welcome on making an efficient byte array parse. Thank you in advance. I hope I gave enough info.
Ray
P.S. I have found that setting the COM in C# to Xon/Xoff the fastest and no programming of wait states. This is really weird.
