/*
 * This ESP32 code is created by esp32io.com
 *
 * This ESP32 code is released in the public domain
 *
 * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-rs232
 */


//########################
char hexValues[] = "1B74";

// Länge des Hexadezimal-Arrays berechnen
int lengthHexArray = sizeof(hexValues) - 1;

//es würde auch funktionieren, wenn man als Länge 2 einfügt, dann printed er in der Konsole nur ein N
//char asciiVal[lengthHexArray / 2 + 1] = {(char) 0x1B, (char) 0x70}; //70 ist Print - 74 ist tare
char asciiVal_read[3] = {(char) 0x1B, (char) 0x70}; //70 ist Print
char asciiVal_tare[3] = {(char) 0x1B, (char) 0x74}; //74 ist tare

void setup() {
  // start communication with baud rate 9600
  Serial.begin(9600);   // Serial Monitor
  Serial2.begin(9600);  // RS232

  // wait a moment to allow serial ports to initialize
  delay(100);


  //
   // Beispiel-Hexadezimalwerte



 
}

void loop() {

  //Serial.println(asciiVal);

  //char pcData = Serial.read();
  String pcData = Serial.readStringUntil('\n');

  // abhängig davon, was vom PC empfangen wird, wird der entsprechende Befehl zur Waage geschickt
  if(pcData == "tare"){
    Serial2.write(asciiVal_tare);
  } else if(pcData == "getWeight"){
    Serial2.write(asciiVal_read);   
  }
  pcData == "";
      

  //Serial2.write(asciiVal_read);

  // Check if there's data available on Serial
  if (Serial2.available()) {
    //char data = Serial2.read();  // read the received character
    String data = Serial2.readStringUntil('\n');  // read the received character
    data = data + "\n";
    Serial.write(data.c_str());
  }



}