From 866944fa26cb242710068066e38c38719b006876 Mon Sep 17 00:00:00 2001 From: Lukas Loidold Date: Tue, 21 Apr 2020 18:38:38 +0200 Subject: [PATCH] main changed for arduino nano debug --- main.cpp | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/main.cpp b/main.cpp index 211302b..4a2ce87 100644 --- a/main.cpp +++ b/main.cpp @@ -16,6 +16,7 @@ #define RING_BUFFER_SIZE 100 #define MAX_PACKET_LENGTH 100 +#define SERIAL_RX_BUFFER_SIZE 256 static const uint8_t COMMAND_TRANSFER_SPI = 1; @@ -60,8 +61,11 @@ void handlePacket(uint8_t *packet, size_t packetLen) { // 8 bit command | 8 bit address | 16bit length | byte data | 16 bit crc + + uint16_t crc = Calculate_CRC(packet, packetLen); + if (crc != 0) { Serial.println("invalid Checksum"); return; @@ -77,11 +81,18 @@ void handlePacket(uint8_t *packet, size_t packetLen) { uint8_t command = packet[0]; uint8_t address = packet[1]; + + switch (command) { case COMMAND_TRANSFER_SPI: transferSPI(address, packet + 4, payloadLen); //echo the data back, no need to change the header fields, they are the same //checksum will be written by sendData() + //check reply: + Serial.println("Data response check: "); + for(size_t i =0; i< packetLen; i++){ + Serial.print("packet nr ");Serial.print(i);Serial.print(" ");Serial.println(packet[i]); + } sendData(packet, packetLen); break; default: @@ -102,11 +113,13 @@ void handleNewData() { size_t firstSTXinRawData = 0; while ((firstSTXinRawData < rawDataSize) && (rawData[firstSTXinRawData] != DleEncoder::STX)) { + Serial.println(rawData[firstSTXinRawData]); firstSTXinRawData++; } if (rawData[firstSTXinRawData] != DleEncoder::STX) { //there is no STX in our data, throw it away... + Serial.println("NO STX"); ringBuffer.deleteData(rawDataSize); return; } @@ -136,7 +149,9 @@ void handleNewData() { //TODO check if this is thread safe by arduino void serialEvent() { //Serial.println(ringBuffer.availableWriteSpace()); - while (Serial.available()) { + uint8_t i = 0; + + while (Serial.available()>0) { uint8_t byte = Serial.read(); ringBuffer.writeData(&byte, 1); } @@ -151,6 +166,7 @@ void setup() { } void loop() { +; handleNewData(); - delay(100); + delay(1000); }