Olá pessoal,
estou tentando enviar uns dados do C# para o arduino, no caso são cinco números.
envio uma string pelo SerialPort.Write(); no seguinte formato:
numero1 + "," + numero2 "+" numero3 + "," + numero4 "+" numero5 + ","
no arduino tô tentando ler da seguinte forma:
//********************GLOBAL***********************
int loopCount;
char p;
String numero1, numero2,..., numero5;
//***************NO MÉTODO LOOP***************
if(Serial.available() > 0){
p = (char)Serial.read();
if(loopCount == 0){
if(p == ',')loopCount++;
else numero1 += p;
//restante do algorítimo...
}
// PARA OS NÚMEROS 2, 3 E 4 É A MESMA LÓGICA...
// AÍ A ÚLTIMA EXECUÇÃO (FORMANDO O ÚLTIMO NÚMERO) É DA SEGUINTE FORMA:
if(loopCount == 4){
if(p == ','){
loopCount = 0;
Serial.flush();
}
else numero5 += p;
}
// Antes de retornar converto as strings de cada número para int com .toInt();
A questão é que quando coloco para retornar os valores, eles veem diferente dos originais.
Tags:
String inString = ""; // string to hold input
String temp="";
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// send an intro:
Serial.println("\n\nString toInt():");
Serial.println();
}
void loop() {
// Read serial input:
while (Serial.available() > 0) {
int inChar = Serial.read();
inString += (char)inChar;
if(inChar==','){
Serial.print("Value:");
Serial.println(inString.toInt());
Serial.print("String: ");
Serial.println(inString);
// clear the string for new input:
inString = "";
}
}
}
Bem-vindo a
Laboratorio de Garagem (arduino, eletrônica, robotica, hacking)
© 2024 Criado por Marcelo Rodrigues. Ativado por