I can't send a command I have a mks tiny bee device with esp32.
by sending data via rx tx to imu Vectornac vn-100
What I want is to pass command from mks to imu command via Serialmonitor.
//mks tiny bee
#define tx 28
#define rx 27
// imu
#define RXD2 5
#define TXD2 6
//$VNASY,1*5179 // command start
//$VNAST,0*4158 // command stop
String Data;
String dat1, datYaw, datPitch, datRoll ,datMagX ,datMagY ,datMagZ ,datAccX , //Retrieve various types of information sent by imu.
datAccY ,datAccZ ,datGyroX ,datGyroY ,datGyroZ, Time;
char command ;
void setup()
{
Serial.begin(115200);
Serial2.begin(115200);
}
void loop()
{
//send command through serial monitor
if (Serial.available()) {
char command = Serial.read();
Serial.print(command);
Serial2.write(command);
}
//Get the value sent from imu
if (Serial2.available() > 0) {
Serial.println();
//Read the value sent from the imu.
Data = Serial2.readStringUntil('\n'); //อ่านค่ามาเก็บที่ data
dat1 = getValue(Data, ',', 0);
datYaw = getValue(Data, ',', 1);
datPitch = getValue(Data, ',', 2);
datRoll = getValue(Data, ',', 3);
datMagX = getValue(Data, ',', 4);
datMagY = getValue(Data, ',', 5);
datMagZ = getValue(Data, ',', 6);
datAccX = getValue(Data, ',', 7);
datAccY = getValue(Data, ',', 8);
datAccZ = getValue(Data, ',', 9);
datGyroX = getValue(Data, ',', 10);
datGyroY = getValue(Data, ',', 11);
datGyroZ = getValue(Data, ',', 12);
Time = getValue(Data, ',', 13);
// Serial.print(""); Serial.print(" ");
Serial.print("Yaw = ") ;Serial.print(datYaw);Serial.print(" ");
Serial.print("Pitch = ") ;Serial.print(datPitch);Serial.print(" ");
Serial.print("Pitch = ") ;Serial.print(datRoll);Serial.print(" ");
//Serial.print("MagX = ") ;Serial.print(datMagX);Serial.print(" ");
//Serial.print("MagY = ") ;Serial.print(datMagY);Serial.print(" ");
//Serial.print("MagZ = ") ;Serial.print(datMagZ);Serial.print(" ");
//Serial.print("AccX = ") ;Serial.print(datAccX);Serial.print(" ");
//Serial.print("AccY = ") ;Serial.print(datAccY);Serial.print(" ");
//Serial.print("AccZ = ") ;Serial.print(datAccZ);Serial.print(" ");
//Serial.print("GyroX = ") ;Serial.print(datGyroX);Serial.print(" ");
//Serial.print("GyroY = ") ;Serial.print(datGyroY);Serial.print(" ");
//Serial.print("GyroZ = ") ;Serial.print(datGyroZ);Serial.print(" ");
Serial.print("time = ") ;Serial.print(Time);Serial.print(" ");
Serial.println();
}
}
//conversion function
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]) : "";
}