Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
tickeParsing.c
//gcc 5.4.0 #include <stdio.h> #include <string.h> #define AUTO_SERVICIO 0 #define HORA_FECHA 1 #define DURACION 2 #define LITROS 3 #define KILOS 4 #define PRECIO_U 5 #define SUBTOTAL 6 #define IVA 7 #define TOTAL 8 #define TEXTO 9 char dataLine[120]; //Every line of text of the ticket int i; //Holds position in previous array to store valid received char int updating = 0; //It is true once the GRUPO tag has been received, false once the PLANTA tag has been received. int status = 0; char ticket[60]; char temp[200]; unsigned char command[5]; unsigned char commandSize=0; unsigned char previousChar; void makeStream(char *dataStream) { unsigned char line [50]; scanf("%s", line); while(strcmp(line, "xxx") != 0){ //printf("%s\n", line); strcat(dataStream, line); scanf("%s", line); } // printf("%s\n", dataStream); } //////////////////////////////////////////////////////////////////////////// int findSubString(char *line, char *data){ int result = 0; for (char *p = strtok(line," "); p != NULL; p = strtok(NULL, " ")) { if( strcmp(data, p) == 0 ) result++; } return result; } int checkStart(char *line){ //Look for "group" substring in the given line string return findSubString(line, "GRUPO"); } int checkEnd(char *line){ return findSubString(line, "PLANTA"); } int fillAutoServicio(char *line){ int result = 0; /* for (char *p = strtok(line," "); p != NULL; p = strtok(NULL, " ")){ result++; puts(p); }*/ char *p = strtok(line," "); if( strcmp(p, "#AUTO:") == 0 ){ p = strtok(NULL, " "); strcat(ticket, "\"AUTO\":"); strcat(ticket, p); strcat(ticket, ","); p = strtok(NULL, " "); p = strtok(NULL, " "); strcat(ticket, "\"SERVICIO\":"); strcat(ticket, p); strcat(ticket, ","); result = 1; } return result; } int fillHoraFecha(char *line){ int result = 0; /* for (char *p = strtok(line," :"); p != NULL; p = strtok(NULL, " :")) puts(p);*/ char *p = strtok(line," :"); if( strcmp(p, "HORA") == 0 ){ strcat(ticket, "\"HORA\":\""); //"HORA":" p = strtok(NULL, " :"); strcat(ticket, p); strcat(ticket, "/"); //HOURS/ p = strtok(NULL, " :"); strcat(ticket, p); strcat(ticket, "/"); //MIN/ p = strtok(NULL, " :"); strcat(ticket, p); strcat(ticket, "\","); //SEC", strcat(ticket, "\"FECHA\":\""); //"FECHA":" p = strtok(NULL, " :"); p = strtok(NULL, " :"); strcat(ticket, p); strcat(ticket, "\","); //MM/DD/YY", result = 1; } return result; } int fillDuracion(char *line){ int result = 0; char *p = strtok(line," :"); if( strcmp(p, "DURACION") == 0 ){ strcat(ticket, "\"DURACION\":\""); //"DURACION":" p = strtok(NULL, " :"); //SRV NOT USED p = strtok(NULL, " :"); strcat(ticket, p); strcat(ticket, "/"); //HOURS/ p = strtok(NULL, " :"); strcat(ticket, p); strcat(ticket, "/"); //MIN/ p = strtok(NULL, " :"); strcat(ticket, p); strcat(ticket, "\","); //SEC", result = 1; } return result; } int fillLitros(char *line){ int result = 0; char *p = strtok(line," :"); if( strcmp(p, "LITROS") == 0 ){ strcat(ticket, "\"LITROS\":"); //"LITROS": p = strtok(NULL, " :"); strcat(ticket, p); strcat(ticket, ","); //nnn, result = 1; } return result; } int fillKilos(char *line){ int result = 0; char *p = strtok(line," :"); if( strcmp(p, "KILOS") == 0 ){ strcat(ticket, "\"KILOS\":"); //"KILOS": p = strtok(NULL, " :"); strcat(ticket, p); strcat(ticket, ","); //nnn, result = 1; } return result; } int fillPrecioU(char *line){ int result = 0; char *p = strtok(line," :"); if( strcmp(p, "PRECIO") == 0 ){ strcat(ticket, "\"PRECIO_UNIT\":"); //"PRECIO_UNIT": p = strtok(NULL, " :"); //UNIT not used p = strtok(NULL, " :"); strcat(ticket, p); strcat(ticket, ","); //nnn.nn, result = 1; } return result; } int fillSubTotal(char *line){ int result = 0; char *p = strtok(line," $"); if( strcmp(p, "SUBTOTAL") == 0 ){ strcat(ticket, "\"SUBTOTAL\":"); //"SUBTOTAL": p = strtok(NULL, " $"); strcat(ticket, p); strcat(ticket, ","); //nnn.nn, result = 1; } return result; } int fillIVA(char *line){ int result = 0; char *p = strtok(line," $"); if( strcmp(p, "16.0%") == 0 ){ strcat(ticket, "\"IVA\":"); //"IVA": p = strtok(NULL, " $"); p = strtok(NULL, " $"); strcat(ticket, p); strcat(ticket, ","); //nnn.nn, result = 1; } return result; } int fillTotal(char *line){ int result = 0; char *p = strtok(line," :$"); if( strcmp(p, "TOTAL") == 0 ){ strcat(ticket, "\"TOTAL\":"); //"TOTAL": p = strtok(NULL, " :$"); strcat(ticket, p); strcat(ticket, ","); //nnn.nn, result = 1; } return result; } int fillTicketString(char *line, int status){ int result = 0; switch(status){ case AUTO_SERVICIO: result = fillAutoServicio(line); break; case HORA_FECHA: result = fillHoraFecha(line); break; case DURACION: result = fillDuracion(line); break; case LITROS: result = fillLitros(line); break; case KILOS: result = fillKilos(line); break; case PRECIO_U: result = fillPrecioU(line); break; case SUBTOTAL: result = fillSubTotal(line); break; case IVA: result = fillIVA(line); break; case TOTAL: //result = fillTotal(line); break; case TEXTO: //NOT USED default: break; } return result; } void receiveChar(unsigned char dataByte){ //if we are receiving just command bytes if( commandSize > 0 ){ if( command[0] == 0x1b ){ if( dataByte = 0x21 ){ commandSize ++; command[0]=0; } } commandSize --; } else{ if( dataByte == 0x1b ){ commandSize = 1; command[0] = 0x1b; } else{ if( dataByte == 0x1d){ commandSize = 2; } else{ if( dataByte >=20 && dataByte < 127 ){ //printf("%c", dataByte); dataLine[i] = dataByte; i++; } else { if( dataByte == 5){ //EOT: End of Transmission //printf("EOT"); dataLine[i] = '\0'; strcpy(temp, dataLine); i=0; if( updating == 0){ if( checkStart(temp) != 0 ){ updating = 1; strcpy(ticket, "{"); status = AUTO_SERVICIO; } } else{ if( checkEnd(temp) == 0 ){ //Ticket printing has not finished //Fill JSON ticket string strcpy(temp, dataLine); if( fillTicketString(temp, status) == 1){ status++; } } else{ //Ticket is ready updating = 0; //wait for next ticket start //Send ticket through BlueTooth channel //strcat(ticket, "}"); puts(ticket); //Send over BT } } } } //previousChar = dataByte; } } } } unsigned char atohex(unsigned char asciiByte){ if( (asciiByte >= '0') && ( asciiByte <= '9') ) asciiByte -= 0x30; else{ if( (asciiByte >= 'A') && ( asciiByte <= 'F') ) asciiByte -= 0x37; } return asciiByte; } void sendDataStream(unsigned char *rawData){ unsigned char highByte, lowByte, dataByte; while( *rawData != '\0' ){ highByte = *rawData; rawData++; lowByte = *rawData; dataByte = (atohex(highByte) << 4) | (atohex(lowByte)); receiveChar(dataByte); rawData++; } } int main(void) { //unsigned char *dataFile; unsigned char dataStream[14000]; //printf("Hello, world!\n"); makeStream(dataStream); i=0; sendDataStream(dataStream); return 0; }
run
|
edit
|
history
|
help
0
5 trabajo semanal 4
main.c
Majeur ou mineur
Lab 7 blackjack v0.1
Timestamp microsecond for C
Array Struct, Call-By-Reference, Malloc, Free, Init, Destroy ... Example
TeleBook
ChangeStructVal
Memory leak
guia 4 terminada