#include <ArduinoJson.h>
#define MaxCarLin 32
#define CaracMaxCmdTx 32
const char CMD_Desconocido[] = "UNK";
String strG_JsonRx;
DynamicJsonDocument doc(2048); //Para Recibir los Datos Json
struct TipoImpuestos{
char Descripcion[MaxCarLin];
char DesBI[MaxCarLin];
char DesIVA[MaxCarLin];
};
enum State{
state01,
state02,
state03,
state04,
state05
};
State stateActual;
enum Input{
input01,
input02,
input03,
input04,
input05
};
#define ErrorYes 0
#define ErrorNoF 1
Input inputActual;
#define CantMaxCaracRx 1000
uint16_t ui16L_Cont; //Contador de {} del Json Rx
uint16_t ui16L_Index; //Contador de Caracteres Rx
//char chL_CarRx; //Se Almacena el ultimo Caracter Rx
char chG_CmdRx[20];
const int timeOutUART =20000;
bool imprimeRx;
struct MediosDePago
{
uint64_t ui64Medios[8] ;
};
struct Impuestos
{
uint64_t ui64Impuestos[8] ;
};
struct Contadores
{
uint8_t uchFactor ;
uint32_t ui32UltFact ;
uint32_t ui32Fecha ;
uint32_t ui32UltNCre ;
uint32_t ui32UltNDeb ;
uint32_t ui32UltDNF ;
uint32_t ulFechaDNF ;
uint32_t ui32UlrTR ;
uint32_t ui32FechaTR ;
};
struct XyZ
{
struct Impuestos stRecargos ;
struct Impuestos stDescuentos ;
struct Impuestos stAnulaciones ;
struct Impuestos stCorrecciones ;
struct Impuestos stVentas ;
struct Impuestos stNotaDebito ;
struct Impuestos stNotaCredito ;
struct Contadores stContadires ;
struct MediosDePago stMedios ;
} ;
XyZ stXyZ;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32-S3!");
Serial.printf("El tamano de la estructura XyZ: %d\n", sizeof(stXyZ));
Serial.printf("El tamano de un byte: %d\n", sizeof(byte));
Serial.printf("El tamano de un int: %d\n", sizeof(int));
Serial.printf("El tamano de un long: %d\n", sizeof(long));
Serial.printf("El tamano de un long long : %d\n", sizeof(long long));
//Serial.printf("El tamano de un byte: %d", sizeof(byte));
char haystack[50] = "Se Busca la Palabra T o t a l en este texto";
char needle[10] = "T o t a l";
char *ret;
//ret = strstr(haystack, needle);
ret = replace_str(haystack, needle, "*****");
printf("The substring is: %s\n", ret);
String str = "hola t o t a l estas";
String str1;
//Serial.printf("La Frase: %s\n",str);
Serial.print("La frase: "); Serial.println(str);
str.replace("total", "***");
//Serial.printf("Se remmeplaza por: %s\n", str);
Serial.print("Por La frase: "); Serial.println(str);
}
void loop() {
// put your main code here, to run repeatedly:
//delay(10); // this speeds up the simulation
if(rxCMD() == ErrorYes){
if(imprimeRx){
Serial.println("Error Rx UART");
}
}
else{
if(imprimeRx){
Serial.println("Se Rx por UART:");
Serial.println(strG_JsonRx);
//veJson();
}
}
}
//Se inicializa el Contador de {}
bool rxCMD(void){
imprimeRx = 0;
char chL_JsonRx[CantMaxCaracRx]; //Se Almacena El JSON Rx
memset(chL_JsonRx, 0, CantMaxCaracRx); //Se inicializa el Arreglo de Rx
ui16L_Index = 0; //Se inicializa el Contador de Carac Rx
ui16L_Cont = 0;
char chL_CarRx; //Se Almacena el ultimo Caracter Rx
bool fErrorTimeO = 0;
long timeOutIni;
if (Serial.available()) {
Serial.println("Se Rx");
do { //Ciclo para Rx la Trama JSON
if (Serial.available()) { // Se Inicia con el caracter '{'
chL_CarRx = Serial.read();
//Serial.println(char(chL_CarRx));
if (ui16L_Index == 0) { //Si es el Primer Caracter se inicializ el contador Time Out
timeOutIni = millis();
}
if (chL_CarRx == '{') { //El contador sCont se Incrementa cada vez que se Rx '{'
ui16L_Cont++;
}
if (ui16L_Cont > 0) {
chL_JsonRx[ui16L_Index++] = chL_CarRx;
if (chL_CarRx == '}') { //Si se Rx '}' se decrementa sCont
ui16L_Cont--; //Al final del JSON sCont=0
}
} //Si se Empezo a Rx el Json se Almacena en el Arreglo
}
if (millis() - timeOutIni > timeOutUART) { //Se verifica si hay Time OUT
fErrorTimeO = 1;
imprimeRx = 0;
break;
}
} while (ui16L_Cont > 0); //El ciclo se Repite mientras no se a finalizado de Rx el JSON
if (fErrorTimeO) { //Se verifica si se salio de ciclo por Time Out
strncpy(chG_CmdRx, CMD_Desconocido, CaracMaxCmdTx);
//byG_CodeTx = ErrorTimeO;
//stcG_FlagFact.flagTxRespuesta = 1;
imprimeRx = 1;
Serial.flush();
return ErrorYes;
}
//Se verifica si no se empezo a Rx la Trama Json
if (ui16L_Index == 0) {
//stcG_FlagFact.flagTxRespuesta = 0; //Como no se Rx Datos no se Tx Respuesta la Host
imprimeRx = 1;
Serial.flush();
return ErrorYes;
}
//Si se termino de Rx la Trama JSON se almacena en la variable strJsonRx y se retorna sin errores
strG_JsonRx = String(chL_JsonRx);
imprimeRx = 1;
Serial.flush();
return ErrorNoF;
}
imprimeRx = 0;
return ErrorYes;
}
bool verJson(void){
DeserializationError error = deserializeJson(doc, strG_JsonRx);
if (error){
Serial.println("Errorn en el JSON");
return ErrorYes;
}
if (!doc["cmd"]) { //Se verifica si se Rx el comando "cmd"
//chG_CmdRx = "UNK";
strncpy(chG_CmdRx, CMD_Desconocido, CaracMaxCmdTx);
//byG_CodeTx = ErrorCmd;
//flagTxRespuesta = 1; //Se Tx la Respuesta al HOST
return ErrorYes;
}
}
// char *replace_str(char *str, char *orig, char *rep, int start)
// {
// static char temp[4096];
// static char buffer[4096];
// char *p;
// strcpy(temp, str + start);
// if(!(p = strstr(temp, orig))) // Is 'orig' even in 'temp'?
// return temp;
// strncpy(buffer, temp, p-temp); // Copy characters from 'temp' start to 'orig' str
// buffer[p-temp] = '\0';
// sprintf(buffer + (p - temp), "%s%s", rep, p + strlen(orig));
// sprintf(str + start, "%s", buffer);
// return str;
// }
char *replace_str(char *str, char *orig, char *rep)
{
static char buffer[4096];
char *p;
if(!(p = strstr(str, orig))) // Is 'orig' even in 'str'?
return str;
strncpy(buffer, str, p-str); // Copy characters from 'str' start to 'orig' st$
buffer[p-str] = '\0';
sprintf(buffer+(p-str), "%s%s", rep, p+strlen(orig));
return buffer;
}