Ich habe versucht, ein Array von 5 Werten (0 bis 4) über die UART TX/RX Schnittstelle zu senden und empfangen. Irgendie wills nicht so recht. Kann mir hier jemand helfen?
Ich möchte das Array wie folgt senden: Adresse, Befehlszeichen, Wert, CRC Prüfsumme.
Empfangen wird es wie folgt:
- Erstes Zeichen einlesen und abgleichen, ob es mit der Adresse übereinstimmt. Falls ja, die Adresse ins Array 0 schreiben
- Wenn es mit der Adresse übereinstimmt, dann die vier nächsten Werte ins Array schreiben (1 bis 4)
- Sobald die Daten alle gelesen wurden, mit den Array 0 bis 2 die Prüfsumme rechnen und mit dem Empfangenen Array 3 und 4 vergleichen.
- Wenn die Prüfsummen übereinstimmen, dann die Werte von Array 1 per switch ins dazugehörige case das Array 2 schreiben.
Hier mal den Programmcode wie er bis jetzt aussieht. Senden tut er was, per Serielle Ausgabe erkennt er auch die Adresse, aber er speichert keine Werte ab.
Sendecode:
char data[5];
char incomingData[5];
char crcData[2];
uint16_t crc = 0;
boolean newData = false;
const byte address = 0x82;
const byte addressRX = 0x80;
byte BUT10 = 0;
byte BUT11 = 0;
byte BUT12 = 0;
byte BUT13 = 0;
byte BUT14 = 0;
void setup() {
Serial.begin(38400);
}
void loop() {
readCommand(Serial);
command('E',184, address, Serial);
command('H',253, address, Serial);
command('#',98, address, Serial);
}
//Function for driving the feet and dome motors over serial
void command(const char command, const byte value, const byte address, Stream& stream){
data[0] = address; //write address in data array
data[1] = command; //write command for forward/reverse/left/right in data array
data[2] = value; //write movement speed in data array
for(byte index = 0; index < 3; index++){ //create the checksum for the array data[i] with 0 <= i <= 3
crc16(data[index]); //call the function crc16
}
//crc = getCRC();
data[3] = crc >> 8; //store checksum in data array
data[4] = crc;
stream.write(data, sizeof(data)); //send the data array by serial
crc = 0; //clear the checksum
}
//Calculates CRC16 of nBytes of data[index] in byte array
void crc16(uint8_t data) {
int i;
crc = crc ^ ((uint16_t)data << 8);
for (i=0; i<8; i++){
if (crc & 0x8000){
crc = (crc << 1) ^ 0x1021;
}
else{
crc <<= 1;
}
}
}
void readCommand(Stream& stream){
readData(stream);
if(newData == true){
for(byte index = 0; index < 3; index++){ //create the checksum for the array data[i] with 0 <= i <= 3
crc16(incomingData[index]); //call the function crc16
}
crcData[0] = crc >> 8;
crcData[1] = crc;
if(crcData[0] == incomingData[3] && crcData[1] == incomingData[4]){
switch(incomingData[1]){
case 'A': BUT10 = incomingData[2];
break;
case 'B': BUT11 = incomingData[2];
break;
case 'C': BUT12 = incomingData[2];
break;
case 'D': BUT13 = incomingData[2];
break;
case '@': BUT14 = incomingData[2];
break;
}
}
Serial.print("BUT10 = ");
Serial.println(BUT10);
Serial.print("BUT11 = ");
Serial.println(BUT11);
Serial.print("BUT12 = ");
Serial.println(BUT12);
Serial.print("BUT13 = ");
Serial.println(BUT13);
Serial.print("BUT14 = ");
Serial.println(BUT14);
newData = false;
for(byte index = 0; index < 5; index++){
incomingData[index] = '\0';
}
crc = 0;
}
}
void readData(Stream& stream){
static boolean inProgress = false;
static byte index = 0;
char var = 0;
while(stream.available() > 0){
var = stream.read();
if(inProgress == true){
if(index < 4){
incomingData[index++] = var;
}
else{
inProgress = false;
index = 0;
newData = true;
}
}
else if(var == addressRX && inProgress == false){
inProgress = true;
incomingData[0] = addressRX;
}
}
}
Empfängercode:
char data[5];
char incomingData[5];
char crcData[2];
uint16_t crc = 0;
boolean newData = false;
const byte address = 0x80;
const byte addressRX = 0x82;
byte BUT1 = 0;
byte BUT2 = 0;
byte BUT3 = 0;
byte BUT4 = 0;
byte BUT5 = 0;
void setup() {
Serial.begin(38400);
}
void loop() {
readCommand(Serial);
command('A',155, address, Serial);
command('D',247, address, Serial);
command('B',29, address, Serial);
}
//Function for driving the feet and dome motors over serial
void command(const char command, const byte value, const byte address, Stream& stream){
data[0] = address; //write address in data array
data[1] = command; //write command for forward/reverse/left/right in data array
data[2] = value; //write movement speed in data array
for(byte index = 0; index < 3; index++){ //create the checksum for the array data[i] with 0 <= i <= 3
crc16(data[index]); //call the function crc16
}
//crc = getCRC();
data[3] = crc >> 8; //store checksum in data array
data[4] = crc;
stream.write(data, sizeof(data)); //send the data array by serial
crc = 0; //clear the checksum
}
//Calculates CRC16 of nBytes of data[index] in byte array
void crc16(uint8_t data) {
int i;
crc = crc ^ ((uint16_t)data << 8);
for (i=0; i<8; i++){
if (crc & 0x8000){
crc = (crc << 1) ^ 0x1021;
}
else{
crc <<= 1;
}
}
}
void readCommand(Stream& stream){
readData(stream);
if(newData == true){
for(byte index = 0; index < 3; index++){ //create the checksum for the array data[i] with 0 <= i <= 3
crc16(incomingData[index]); //call the function crc16
}
crcData[0] = crc >> 8;
crcData[1] = crc;
if(crcData[0] == incomingData[3] && crcData[1] == incomingData[4]){
switch(incomingData[1]){
case 'E': BUT1 = incomingData[2];
break;
case 'F': BUT2 = incomingData[2];
break;
case 'G': BUT3 = incomingData[2];
break;
case 'H': BUT4 = incomingData[2];
break;
case '#': BUT5 = incomingData[2];
break;
}
}
Serial.print("BUT1 = ");
Serial.println(BUT1);
Serial.print("BUT2 = ");
Serial.println(BUT2);
Serial.print("BUT3 = ");
Serial.println(BUT3);
Serial.print("BUT4 = ");
Serial.println(BUT4);
Serial.print("BUT5 = ");
Serial.println(BUT5);
newData = false;
for(byte index = 0; index < 5; index++){
incomingData[index] = '\0';
}
crc = 0;
}
}
void readData(Stream& stream){
static boolean inProgress = false;
static byte index = 0;
char var = 0;
while(stream.available() > 0){
var = stream.read();
if(inProgress == true){
if(index < 4){
incomingData[index++] = var;
}
else{
inProgress = false;
index = 0;
newData = true;
Serial.println(incomingData[0]);
Serial.println(incomingData[1]);
Serial.println(incomingData[2]);
Serial.println(incomingData[3]);
Serial.println(incomingData[4]);
}
}
else if(var == addressRX && inProgress == false){
inProgress = true;
incomingData[0] = addressRX;
Serial.println("inProgress = true!");
}
}
}