iejun
April 29, 2017, 9:48am
1
hi all,
Am working with TouchDesigner and want to retrieve his string and convert that into an array
Like the value am sending in TD is 334, 555, 666, 777, 444 etc
now i can serial print a string with no problem but converting to an array is troubling me
void loop(){
if (Serial.available() > 0) {
char incomingPattern = {Serial.read()};
Serial.print(incomingPattern);
}
}
I tried this with no luck
int newpat[64]={Serial.read()};
The serial input basics post shows how to receive serial data into a character array (string) and parse the data.
char incomingPattern = {Serial.read()};
The curly brackets around Serial.read() are not necessary.
You can parse the data without storing its ascii-equivalent, if it only contains comma seperated ints.
bool lineDone;
bool negativ;
byte numbersGot;
int numbers[50];
void setup() {
Serial.begin(250000);
Serial.println(F("numbers seperated by ','"));
}
void loop() {
if (Serial.available()) {
char got = Serial.read();
if (isdigit(got)) {
numbers[numbersGot] *= 10;
numbers[numbersGot] += got - '0';
} else if (got == '-') {
negativ = true;
} else if (got == 10) {
if (negativ) {
numbers[numbersGot] = -numbers[numbersGot];
}
numbersGot++;
lineDone = true;
} else if (got == ',') {
if (negativ) {
numbers[numbersGot] = -numbers[numbersGot];
}
if (++numbersGot >= sizeof(numbers) / sizeof(numbers[0])) {
lineDone = true;
} else {
numbers[numbersGot] = 0;
negativ = false;
}
}
}
if (lineDone) {
Serial.print(numbersGot);
Serial.print(F(" number"));
if (numbersGot != 1) {
Serial.print(F("s"));
}
Serial.print(F(" parsed "));
for (byte i = 0; i < numbersGot; i++) {
Serial.print(numbers[i]);
if (i != numbersGot - 1) {
Serial.print(F(", "));
}
}
Serial.println();
lineDone = false;
numbersGot = 0;
numbers[0] = 0;
negativ = false;
}
}
This parses the integers on one line into an int array.
if you can afford the overhead, sscanf() sure makes things easier in that regard:
char* message = "334,555,666,-777,444";
int myArray[5];
void setup()
{
Serial.begin(9600);
Serial.println("\n");
sscanf(message, "%d,%d,%d,%d,%d", &myArray[0], &myArray[1], &myArray[2], &myArray[3], &myArray[4]);
for(int i = 0; i < sizeof(myArray)/sizeof(myArray[0]); i++)
{
Serial.println(myArray[i]);
}
}
void loop()
{
}
Or use "intVariable = Serial.parseInt();". It skips over anything that doesn't look like a number (like a comma) and then reads the next number until it reaches a non-number (comma, new line, etc) or times out.
iejun
April 30, 2017, 9:03am
6
Am missing the first value in the array and also some indexes have random values like -32999 or 26509 what am not sending
Whandall:
You can parse the data without storing its ascii-equivalent, if it only contains comma seperated ints.
bool lineDone;
bool negativ;
byte numbersGot;
int numbers[50];
void setup() {
Serial.begin(250000);
Serial.println(F("numbers seperated by ','"));
}
void loop() {
if (Serial.available()) {
char got = Serial.read();
if (isdigit(got)) {
numbers[numbersGot] *= 10;
numbers[numbersGot] += got - '0';
} else if (got == '-') {
negativ = true;
} else if (got == 10) {
if (negativ) {
numbers[numbersGot] = -numbers[numbersGot];
}
numbersGot++;
lineDone = true;
} else if (got == ',') {
if (negativ) {
numbers[numbersGot] = -numbers[numbersGot];
}
if (++numbersGot >= sizeof(numbers) / sizeof(numbers[0])) {
lineDone = true;
} else {
numbers[numbersGot] = 0;
negativ = false;
}
}
}
if (lineDone) {
Serial.print(numbersGot);
Serial.print(F(" number"));
if (numbersGot != 1) {
Serial.print(F("s"));
}
Serial.print(F(" parsed "));
for (byte i = 0; i < numbersGot; i++) {
Serial.print(numbers[i]);
if (i != numbersGot - 1) {
Serial.print(F(", "));
}
}
Serial.println();
lineDone = false;
numbersGot = 0;
numbers[0] = 0;
negativ = false;
}
}
This parses the integers on one line into an int array.
You only gave some lame examples "Like the value am sending in TD is 334, 555, 666, 777, 444 etc",
which will be handled perfectly by the given code.
I guess you are sending values outside the valid integer range.
If you could post the complete real input and describe the errors, that could be fixed.