diff --git a/README.md b/README.md index 11a96c1..581b07c 100644 --- a/README.md +++ b/README.md @@ -24,12 +24,12 @@ Implemented and tested on: * CLIENT - `gcc client.c -Wall -Werror -Wextra -o client` * SERVER - - `gcc server.c rs232.c -Wall -Werroe -Wextra -o client` + - `gcc server.c rs232.c -Wall -Werror -Wextra -o server` #### WINDOWS * CLIENT - `gcc client.c -Wall -Wextra -lwsock32 -o client` * SERVER - - `gcc server.c rs232.c -lwsock32 -Wall -Werror -Wextra -o client` + - `gcc server.c rs232.c -lwsock32 -Wall -Werror -Wextra -o server` ## Launch ### Server diff --git a/client.c b/client.c index 0ff3134..20c6eed 100644 --- a/client.c +++ b/client.c @@ -142,7 +142,7 @@ int main(int argc , char *argv[]){ return 1; } - // Check server's mode + // Check server's mode and set control variable int automatic = 0; if(strcmp(server_reply, "0") == 0){ printf("Connected - manual mode.\n"); @@ -157,12 +157,13 @@ int main(int argc , char *argv[]){ char *description[CMD_BUF_SIZE]; int i = 0; + // Manual mode if(automatic == 0){ // Load user commands FILE *fptr; if ((fptr = fopen("commands.txt", "r")) == NULL) { - printf("Cannot open/create log file!\n"); + printf("Cannot open command file!\n"); cleanUP(sock); return 1; } diff --git a/server.c b/server.c index 2761e7f..31ac240 100644 --- a/server.c +++ b/server.c @@ -52,6 +52,14 @@ first number represents databits, letter defines parity and last number is for s It's better to use higher values, it's helpful in case of hardware/RS-232 overload */ #define COM_BUFFER 4096 +/* Amount of seconds used to wait before checking and reading data from RS-232 line +Waiting time should be approx. 0,1 seconds (from RS-232 library), but the lowest +measure (response) time od distance sensor is 0,3 seconds, the greatest value is 4-5 seconds. +Value 1 equals 1 second, which is used in loop checking max response time. +This value fit perfectly for this software and should not be changed. +*/ +#define WAIT_TIME 1 + /* Definition of struct used in main() */ struct var { char *name; @@ -81,7 +89,7 @@ Returns lasermeter device ID from return value of command "dg" char getID(int cport_nr){ char dg[] = "dg\r\n"; RS232_cputs(cport_nr, dg); - wait(1); + wait(WAIT_TIME); unsigned char buf[MESSAGE_SIZE]; @@ -331,7 +339,7 @@ int main(int argc , char *argv[]){ char mode[]="sNc\r\n"; mode[1] = deviceID; RS232_cputs(cport_nr, mode); - wait(1); + wait(WAIT_TIME); RS232_PollComport(cport_nr, buf, COM_BUFFER); // Automatic mode @@ -376,25 +384,33 @@ int main(int argc , char *argv[]){ RS232_cputs(cport_nr, mode); free(mode); - wait(1); + wait(WAIT_TIME); + // Variables for errors from distance sensor; list of errors, counter of all errors and counter for errors in row char *errors = malloc(sizeof(char)); int errCount = 0; int errInRow = 0; int maxErrors = getVar(vars, "maxErrors"); int samples = getVar(vars, "samples"); + + // Counter for samples; value is checked if variable 'samples' is greater than -1 (set in config) int measureSamples = 0; + + // Control variable for error linked with client connection, changes to 1 if error is found later in conditions int e = 0; while(1){ + // If amount of errors in row (not interrupted by correct value) reach the limit if(errInRow >= maxErrors){ break; } + // If max samples variable is set in config and all samples reach the limit if(samples > -1 && samples == measureSamples){ break; } + // If client connection error occured if(e == 1){ break; } @@ -411,7 +427,39 @@ int main(int argc , char *argv[]){ char *first = NULL; printf("%s", buf); + /* EXPERIMENTAL CODE (not tested) + It happens that sometimes server receives measured value splitted in two halfs. + - it looks like g1g+112\r\n61444\r\n + Following code block demonstrates design of code which can solve this problem. + + char halfs[14] = {0}; + int halfcount = 0; + + // has to change loop(X) condition + while ((first = strtok_r(rest, "\n", &rest))){ + first[strcspn(token, "\r")] = 0; + + int checkLen = (int)strlen(first); + if(checkLen < 12){ + if(halfcount == 0){ + strcpy(halfs, first); + halfcount++; + } + else{ + strcat(halfs, first); + halfcount = 0; + } + } + first = halfs; + ... + ... + } + + + */ + // Split and handle data + // Loop(X) while((first = strtok_r(rest, "\r\n", &rest))){ // Check and handle error char *err = NULL; @@ -432,16 +480,30 @@ int main(int argc , char *argv[]){ } // Handle other values else{ - int res = 0; - if((res = atoi(first)) > 0){ - res = atoi(first); - } - else{ - char *numbers = NULL; - strtok_r(first, "+", &numbers); - res = atoi(numbers); - } + /* // EXPERIMENTAL (untested) function + // Check if output result doesnt have incorrect lenght (devided to 2 halfs) + + first[strcspn(token, "\r")] = 0; + + int checkLen = (int)strlen(first); + if(checkLen < 12){ + if(halfcount == 0){ + strcpy(halfs, first); + halfcount++; + } + else{ + strcat(halfs, first); + halfcount = 0; + } + } + first = halfs; + */ + // Change string to integer + int res = 0; + char *numbers = NULL; + strtok_r(first, "+", &numbers); + res = atoi(numbers); if(res == 0){ @@ -450,10 +512,12 @@ int main(int argc , char *argv[]){ int units = getVar(vars, "units"); + // Change integer to double and apply set units double resd = res * (1/(double)units); char output[MESSAGE_SIZE]; memset(output, 0, MESSAGE_SIZE); + // Add time stamp to result snprintf(output, MESSAGE_SIZE, "%02d/%02d/%d - %02d:%02d:%02d: %0.2f\n", timeinfo->tm_mday, timeinfo->tm_mon+1, timeinfo->tm_year+1900, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, resd); @@ -486,7 +550,7 @@ int main(int argc , char *argv[]){ } // Automatic mode end sequence - wait(1); + wait(WAIT_TIME); char stop[] = "sNc\r\n"; stop[1] = deviceID; RS232_cputs(cport_nr, stop); @@ -498,7 +562,7 @@ int main(int argc , char *argv[]){ send(client_sock, end, 1, 0); } - wait(1); + wait(WAIT_TIME); printf("End of measuring, shutting down server.\n"); break; } @@ -514,7 +578,7 @@ int main(int argc , char *argv[]){ int checkCOM = 0; while(RS232_PollComport(cport_nr, buf, COM_BUFFER) <= 0){ - wait(1); + wait(WAIT_TIME); if(checkCOM == 5){ char info[] = "No response from COM port, check your device and all cables!\n"; send(client_sock, info, strlen(info), 0);