我正在尝试将通过处理生成的字符串传递给Arduino串行端口,但似乎不起作用。
Arduino代码:
String readString; //main captured String
String MotorChoice;
String AngleRange;
String FrequencyIN;
int ind1;
int ind2;
int ind3;
int MC;
int AR;
float FIN;
void setup() {
Serial.begin(9600);
Serial.println("Input command in the form of Motor Choice,Angle Range,Frequency*");
}
void loop()
{
if (Serial.available()) {
char c = Serial.read();
if (c == '*') {
Serial.println();
Serial.print("captured String is : ");
Serial.println(readString);
ind1 = readString.indexOf(','); //finds location of first ,
MotorChoice = readString.substring(0, ind1); //captures first data String
ind2 = readString.indexOf(',', ind1+1 ); //finds location of second ,
AngleRange = readString.substring(ind1+1, ind2); //captures second data String
ind3 = readString.indexOf(',', ind2+1 );
FrequencyIN = readString.substring(ind2+1);
//convert sring to int
MC = MotorChoice.toInt();
AR = AngleRange.toInt();
FIN = FrequencyIN.toFloat();
Serial.print("Motor Selected = ");
Serial.println(MC);
Serial.print("Angle Range = ");
Serial.println(AR);
Serial.print("Frequency Required = ");
Serial.println(FIN);
Serial.println();
Serial.println();
readString=""; //clears variable for new input
MotorChoice="";
AngleRange="";
FrequencyIN="";
}
else
{
readString += c; //makes the string readString
}
}
}
处理代码:
import processing.serial.*;
Serial myPort;
println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
int M =1;
int A =90;
float F =2.5;
String sM = str(M);
String sA = str(A);
String sF = str(F);
String sb;
String s1;
sb = sM +","+ sA+"," + sF+"*";
s1 = sb;
println(s1);
myPort.write(s1);
上面的Arduino代码以前已经尝试过使用串行监视器以电机,角度,频率*的形式输入输入的地方
任何帮助将不胜感激。
答案 0 :(得分:0)
如果我正确解释了您的代码,则说明您使用*字符表示列表结尾。但是在您的Arduino代码中,您正在检查*,然后打印输出字符串。因此,到那时为止,数组的其余部分早已一去不复返了。另外,您在开头设置了一个名为readString
的变量,但从未将其设置为任何值。这就是为什么您看不到任何输出的原因。
首先,我首先将变量名从readString
更改为其他名称(为清楚起见),然后使用Serial.readString()
从串行端口获取该字符串。然后检查该字符串的最后一个字符是否为*字符。
if (Serial.available()) {
String str = Serial.readString(); // get string
char c = str[strlen(str)-1]; // find *
if (c == '*') {
Serial.print("Output: ");
Serial.println(str);
//The rest of your code
}