/*******************************************************************************************
 *******************************************************************************************
 **                                www.bastelgarage.ch                                    															**
 ** Der Onlineshop mit Videoanleitungen und kompletten Bausätzen für Anfänger und Profis! 										**
 *******************************************************************************************
 *******************************************************************************************
 ** Waveshare 1D/2D Barcode Scanner Reader Modul - Demoprogramm             											         **
 ** Autor: Alf Müller                                                               																		 **
 ** Datum: 02.05 2019                                                                   																 **
 ** Version: 1.0                                                                          																 **
 *******************************************************************************************
*/

#include <SoftwareSerial.h>

SoftwareSerial mySerial(5, 4); // RX, TX  (Wemos D1 mini D1, D2) Software Serial
long interval = 10000;  // Schaltet alle 10 Sekunden den Leser ein
long previousMillis = 0;
String Daten;

//UART COMMAND Seite 57 im Manual.
char Head1, Head2, Types, Lens, Adress1, Adress2, CRC1, CRC2, Dummy;


void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  
  Serial.println(".....wird gestartet");
  mySerial.begin(9600);
}

void loop() { 
  if (mySerial.available()) {

    Head1 = mySerial.read();
    Serial.print("Head1:");
    Serial.println(Head1, HEX);

    Head2 = mySerial.read();
    Serial.print("Head2:");
    Serial.println(Head2, HEX);

    Types = mySerial.read();
    Serial.print("Types:");
    Serial.println(Types, HEX);

    Lens = mySerial.read();
    Serial.print("Lens:");
    Serial.println(Lens, HEX);

    Adress1 = mySerial.read();
    Serial.print("Adress1:");
    Serial.println(Adress1, HEX);

    Adress2 = mySerial.read();
    Serial.print("Adress2:");
    Serial.println(Adress2, HEX);

    Dummy = mySerial.read();
    Serial.print("Dummy:");
    Serial.println(Dummy, HEX);

    Daten = mySerial.readString();

    CRC1 = mySerial.read();
    Serial.print("CRC1:");
    Serial.println(CRC1, HEX);

    CRC2 = mySerial.read();
    Serial.print("CRC2:");
    Serial.println(CRC2, HEX);

    Serial.println(Daten);

    delay(500);


  }


  if (Serial.available()) {
    mySerial.write(Serial.read());
  }
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis > interval) {

    previousMillis = currentMillis;
    Serial.println("Sende einschalten");
    byte message[] = {0x7E, 0x00, 0x08, 0x01, 0x00, 0x02, 0x01, 0xAB, 0xCD };   // Sende Einschalten
    mySerial.write(message, sizeof(message));
  }
  delay(500);
}
