Arduino seri portuna gelen bir cümleyi belirli bir karakteri referans alarak parçalayabilirmiyim?
Örnek:
“Ali Mehmet Ahmet”
diye bir cümlemiz var bunu boşluklar referans alınarak parçalayarak bir dizi olarak alma imkanımız var mıdır?
serial - How do I split an incoming string? - Arduino Stack Exchange
strchr(): search for a character in a C string (i.e. char *)
strtok(): splits a C string into substrings, based on a separator character
atoi(): converts a C string to an int
// Calculate based on max input size expected for one command
#define INPUT_SIZE 30
...
// Get next command from Serial (add 1 for final 0)
char input[INPUT_SIZE + 1];
byte size = Serial.readBytes(input, INPUT_SIZE);
// Add the final 0 to end the C string
input[size] = 0;
// Read each command pair
char* command = strtok(input, "&");
while (command != 0)
{
// Split the command in two values
char* separator = strchr(command, ':');
if (separator != 0)
{
// Actually split the string in 2: replace ':' with 0
*separator = 0;
int servoId = atoi(command);
++separator;
int position = atoi(separator);
// Do something with servoId and position
}
// Find the next command in input string
command = strtok(0, "&");
}
How to split a string using a specific delimiter in Arduino? - Stack Overflow
// https://stackoverflow.com/questions/9072320/split-string-into-string-array
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = {0, -1};
int maxIndex = data.length()-1;
for(int i=0; i<=maxIndex && found<=index; i++){
if(data.charAt(i)==separator || i==maxIndex){
found++;
strIndex[0] = strIndex[1]+1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}
You can use this function as follows (with ";" as separator):
String part01 = getValue(application_command,';',0);
String part02 = getValue(application_command,';',1);
String part03 = getValue(application_command,';',2);
Splitting strings by a delimiter for Arduino · GitHub
//
// This is tested and works!
//
String input = "123,456";
int firstVal, secondVal;
for (int i = 0; i < input.length(); i++) {
if (input.substring(i, i+1) == ",") {
firstVal = input.substring(0, i).toInt();
secondVal = input.substring(i+1).toInt();
break;
}
}
//
// I tested a similar version in JS and it worked
// No guarantees!!
//
// Define number of pieces
const int numberOfPieces = 4;
String pieces[numberOfPieces];
// This will be the buffered string from Serial.read()
// up until you hit a \n
// Should look something like "123,456,789,0"
String input = "";
// Keep track of current position in array
int counter = 0;
// Keep track of the last comma so we know where to start the substring
int lastIndex = 0;
void setup(){
Serial.begin(9600);
}
void loop() {
// Check for data coming in from serial
if (Serial.available() > 0) {
// Read the first byte and store it as a char
char ch = Serial.read();
// Do all the processing here since this is the end of a line
if (ch == '\n') {
for (int i = 0; i < input.length(); i++) {
// Loop through each character and check if it's a comma
if (input.substring(i, i+1) == ",") {
// Grab the piece from the last index up to the current position and store it
pieces[counter] = input.substring(lastIndex, i);
// Update the last position and add 1, so it starts from the next character
lastIndex = i + 1;
// Increase the position in the array that we store into
counter++;
}
// If we're at the end of the string (no more commas to stop us)
if (i == input.length() - 1) {
// Grab the last part of the string from the lastIndex to the end
pieces[counter] = input.substring(lastIndex, i);
}
}
// Clear out string and counters to get ready for the next incoming string
input = "";
counter = 0;
lastIndex = 0;
}
else {
//if we havent reached a newline character yet, add the current character to the string
input += ch;
}
}
// Data is now available in pieces array
// pieces[0] is first item
// pieces[1] is second item, and so on
// You can call toInt() on the data to convert it to an int
// ex. int value = pieces[0].toInt();
}
Aslında Arduino değil C sorusu. Tabi arduino için de çalışır. Kodlara, C string kütüphanelerini eklemek gerekebilir.
Buradaki örneklerden burada kendimiz farklı şekilde de yazabiliriz.
1 Beğeni