dgibum
September 7, 2012, 12:40pm
1
I am hawing troubles with serial communication between two APC220 RF modules...
(controller: is a PC with PlayStation DS3 controller)
Arduino: While Serial is empty, send "R" to the controller.
Controler: Send to Arduino "A;1;1;255;B;2;90;X".
Arduino: Read chars from Serial and use them in switch function to start appropriate method.
So this sketch works great if i use 100ms delay (or larger) between commands to controller (Serial1.println("R"); delay(100);). But this is way to slow.
What is with this delay? Why cant Arduino read data properly from Serial if there is no delay?
void loop() {
ReadyToReceiveData();
ReadSerialData();
}
//-------------------------------------------
void ReadyToReceiveData() {
while(Serial1.available() <= 0) {
Serial1.println("R");
delay(100);
}
}
void ReadSerialData() {
if(Serial1.read() == 'A') {
while(true) {
temp = Serial1.read();
if(temp == _delimiter) {
if(tempVar[0] != '\0') {
variable[argNumCount] = atoi(tempVar);
argNumCount++;
charCountVar = 0;
ClearArray(tempVar,_maxVarLength);
}
}else if(temp == _method || temp == _end) {
RunMethod();
argNumCount = 0;
ClearArray(variable,_maxMethodVarNum);
}else {
//store char to tempvar
tempVar[charCountVar] = temp;
charCountVar++;
}
if(temp == _end) {
break;
}
}
} else {
//Wrong start Header -> discard all received data
ClearSerialBuffer();
}
}
If i send one "R" command to the controller and then wait for it to response it still doesn't work without the delay
void ReadyToReceiveData() {
Serial1.println("R");
while(Serial1.available() <= 0) { ; }
}
This code will be used for RC car so this much of a delay is not an option.
pylon
September 7, 2012, 1:11pm
2
What's the result without the delay? What's the result when waiting for the Controller?
I'd guess you fill up the serial buffer without the delay. When waiting for the Controller the transmission of the "R" may have failed so the Controller doesn't answer and you're waiting forever. Are these guesses correct?
Show us the whole code not just excepts.
dgibum
September 7, 2012, 1:32pm
3
//Command sample: "A;1;1;255;B;2;90;X"
const char _start = 'A'; //Start header
const char _delimiter = ';'; //Delimiter for arguments
const char _method = 'B'; //Delimiter for methods
const char _end = 'X'; //End header
const byte _maxVarLength = 3; //Max digits for one argument
const byte _maxMethodVarNum = 5; //Max number of arguments for a method
const int ForwardBackward = 40;
const int Motor = 2;
const int ServoMotor = 9;
const int LED = 13;
//-------------------------------------------
//#include <LiquidCrystal.h>
#include <Servo.h>
//LiquidCrystal LCD(12, 11, 34, 35, 36, 37);
Servo ServoDirection;
//-------------------------------------------
char temp; //stores curently read character from serial ('2')
char tempVar[_maxVarLength]; //stores charaters for one variable ('2','5','5')
int variable[_maxMethodVarNum]; //stores multiple variables for one method (1,255)
byte charCountVar = 0; //current read char index in tempVar
byte argNumCount = 0; //current argument index
void setup() {
Serial1.begin(19200);
//LCD.begin(16, 2);
pinMode(ForwardBackward, OUTPUT);
pinMode(Motor, OUTPUT);
ServoDirection.attach(ServoMotor);
pinMode(LED, OUTPUT);
}
void loop() {
ReadyToReceiveData();
ReadSerialData();
}
//-------------------------------------------
void ReadyToReceiveData() {
Serial1.println("R");
delay(100);
while(Serial1.available() <= 0) { delay(1); }
}
void ReadSerialData() {
if(Serial1.read() == 'A') {
while(true) {
temp = Serial1.read();
if(temp == _delimiter) {
if(tempVar[0] != '\0') {
variable[argNumCount] = atoi(tempVar);
argNumCount++;
charCountVar = 0;
ClearArray(tempVar,_maxVarLength);
}
}else if(temp == _method || temp == _end) {
RunMethod();
argNumCount = 0;
ClearArray(variable,_maxMethodVarNum);
}else {
//store char to tempvar
tempVar[charCountVar] = temp;
charCountVar++;
}
if(temp == _end) {
break;
}
}
} else {
//Wrong start Header -> discard all received data
ClearSerialBuffer();
}
}
void RunMethod() {
//debug()
switch (variable[0]) {
case 1:
Speed();
break;
case 2:
Direction();
break;
case 3:
Light();
break;
}
}
//-------------------------------------------
void ClearSerialBuffer() {
while (Serial1.read() > 0)
delay(1);
}
void ClearArray(int Array[], byte length) {
for(int i=0; i<length; i++) {
Array[i] = 0;
}
}
void ClearArray(char Array[], byte length) {
for(int i=0; i<length; i++) {
Array[i] = '\0';
}
}
/*
void debug() {
LCD.setCursor(0, 1);
for(int i=0; i<_maxMethodVarNum; i++) {
LCD.print(variable[i]);
LCD.print(" ");
}
}*/
void Speed() {
if(variable[1] == 1) {
digitalWrite(ForwardBackward, HIGH);
} else if (variable[1] == 0) {
digitalWrite(ForwardBackward, LOW);
}
analogWrite(Motor, variable[2]);
}
void Direction() {
ServoDirection.write(variable[1]);
}
void Light() {
if(variable[1] == 1)
digitalWrite(LED, HIGH);
else if (variable[1] == 0)
digitalWrite(LED, LOW);
}
I will try to use main Serial for debuging... Will report back
Cant see how could Serial buffer be full if there is nothing to be read? That means it is empty wright?
The transmission of "R" and reception is almost 100%. (Controller counts every character received that is not "R").
Could the length (process time) of other methods bee the cause for the delay?
Is there any other way to eficiently send data from one arduino (Controller) to another (RC car). It has to bee fast.
pylon
September 7, 2012, 3:02pm
4
Cant see how could Serial buffer be full if there is nothing to be read? That means it is empty wright?
There is a sending and a receiving buffer.
The transmission of "R" and reception is almost 100%. (Controller counts every character received that is not "R").
If that works, what's the problem then without the delay()? You haven't answered that question yet.
Could the length (process time) of other methods bee the cause for the delay?
Which delay?
Is there any other way to eficiently send data from one arduino (Controller) to another (RC car). It has to bee fast.
Wirelessly? Define "fast"! How far away are they?
dgibum
September 7, 2012, 5:55pm
5
The controller is sending "A;1;1;0;B;2;138;X"
I get this:
START
R send...
Data Received
First Char: A
ÿ
ÿ
ÿ
ÿ
ÿ
;
ÿ
ÿ
ÿ
ÿ
ÿ
ÿ
1
;
0
;
2
5
5
;
B
Var.: -1;0;0;255;0;
;
2
;
1
3
8
;
X
Var.: 2;138;0;0;0;
END
The second list of parametrs is correct, while the first one is corupted. This char "ÿ" ???
pylon
September 7, 2012, 6:19pm
6
This 'ÿ' character is the latin character for code 255 or -1 if in integer mode. You're reading from the Serial interface faster than the bits are coming in. Put a
while (!Serial.available());
before the Serial.read() and that character will go away.