私のバックグラウンドはCではなく(Real Studioにあります-VBに似ています)、低レベルの文字列処理に慣れていないため、コンマ区切りの文字列を分割するのに本当に苦労しています。
シリアル経由でArduinoに文字列を送信しています。これらの文字列は、特定の形式のコマンドです。例えば:
@20,2000,5!
@10,423,0!
「@」は新しいコマンドを示すヘッダーであり、「!」コマンドの終了を示す終了フッターです。'@'の後の最初の整数はコマンドIDであり、残りの整数はデータです(データとして渡される整数の数は、0〜10の整数のどこでもかまいません)。
コマンド(「@」と「!」を取り除いたもの)を取得し、処理するコマンドがあるときに呼び出される関数を呼び出すスケッチを作成しましたhandleCommand()
。問題は、このコマンドを分割して処理する方法が本当にわからないことです。
スケッチコードは次のとおりです。
String command; // a string to hold the incoming command
boolean commandReceived = false; // whether the command has been received in full
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// main loop
handleCommand();
}
void serialEvent(){
while (Serial.available()) {
// all we do is construct the incoming command to be handled in the main loop
// get the incoming byte from the serial stream
char incomingByte = (char)Serial.read();
if (incomingByte == '!')
{
// marks the end of a command
commandReceived = true;
return;
}
else if (incomingByte == '@')
{
// marks the start of a new command
command = "";
commandReceived = false;
return;
}
else
{
command += incomingByte;
return;
}
}
}
void handleCommand() {
if (!commandReceived) return; // no command to handle
// variables to hold the command id and the command data
int id;
int data[9];
// NOT SURE WHAT TO DO HERE!!
// flag that we've handled the command
commandReceived = false;
}
私のPCがArduinoに文字列「@20,2000,5!」を送信するとします。command
私のスケッチは「20,2000,5」を含むString変数(と呼ばれる)で終わり、commandRecieved
ブール変数はTrueに設定されているため、handleCommand()
関数が呼び出されます。
(現在は役に立たない)関数でやりたいことhandleCommand()
は、20をと呼ばれる変数に割り当てid
、2000と5をと呼ばれる整数の配列に割り当てることdata
です。data[0] = 2000
data[1] = 5
私は読んだことがstrtok()
ありますatoi()
が、率直に言って、それらとポインターの概念について頭を悩ませることはできません。私のArduinoスケッチも最適化できると確信しています。