int read_comport(char *response) { char *prompt_pos = NULL; int i; #ifdef ALLEGRO_WINDOWS DWORD bytes_read = 0; DWORD errors; COMSTAT stat; int j; response[0] = '\0'; ClearCommError(com_port, &errors, &stat); if (stat.cbInQue > 0) ReadFile(com_port, response, stat.cbInQue, &bytes_read, 0); // Remove extraneous 0s for (i = 0, j = 0; i < bytes_read; i++) if (response[i] > 0) response[j++] = response[i]; response[j] = 0; #else char c; i = 0; while((c = comm_port_test(com_port)) >= 0) // while the serial buffer is not empty, read comport if (c > 0) response[i++] = c; response[i] = 0; #endif prompt_pos = strchr(response, '>'); if (prompt_pos != NULL) { #ifdef LOG_COMMS write_comm_log("RX", response); #endif *prompt_pos = '\0'; // erase ">" return PROMPT; // command prompt detected } else if (strlen(response) == 0) // if the string is empty, return EMPTY; else //otherwise, { #ifdef LOG_COMMS write_comm_log("RX", response); #endif return DATA; } }
int main() { char temp_buf[64]; time_t current_time; time(¤t_time); // get current time, and store it in current_time strcpy(temp_buf, ctime(¤t_time)); temp_buf[strlen(temp_buf)-1] = 0; strcpy(log_file_name, "log.txt"); remove(log_file_name); write_log(temp_buf); #ifdef LOG_COMMS strcpy(comm_log_file_name, "comm_log.txt"); remove(comm_log_file_name); write_comm_log("START_TIME", temp_buf); #endif sprintf(temp_buf, "\nVersion: %s for %s", SCANTOOL_VERSION_STR, SCANTOOL_PLATFORM_STR); write_log(temp_buf); write_log("\n\nInitializing All Modules...\n---------------------------"); init(); // initialize everything write_log("\n\nDisplaying Main Menu...\n-----------------------"); display_main_menu(); // dislpay main menu write_log("\nMain Menu Closed"); write_log("\n\nShutting Down All Modules...\n----------------------------"); shut_down(); // shut down return EXIT_SUCCESS; }
void send_command(const char *command) { char tx_buf[32]; #ifdef ALLEGRO_WINDOWS DWORD bytes_written; #endif sprintf(tx_buf, "%s\r", command); // Append CR to the command #ifdef ALLEGRO_WINDOWS PurgeComm(com_port, PURGE_TXCLEAR|PURGE_RXCLEAR); WriteFile(com_port, tx_buf, strlen(tx_buf), &bytes_written, 0); if (bytes_written != strlen(tx_buf)) { #ifdef LOG_COMMS log_comm("TX ERROR", tx_buf); // Log transmission error #endif return; } #else comm_port_flush_output(com_port); comm_port_flush_input(com_port); comm_port_string_send(com_port, tx_buf); #endif #ifdef LOG_COMMS write_comm_log("TX", tx_buf); #endif }
int read_comport(char *response) { char *prompt_pos = NULL; #ifdef ALLEGRO_WINDOWS DWORD bytes_read = 0; DWORD errors; COMSTAT stat; response[0] = '\0'; ClearCommError(com_port, &errors, &stat); if (stat.cbInQue > 0) ReadFile(com_port, response, stat.cbInQue, &bytes_read, 0); response[bytes_read] = '\0'; #else int i = 0; while((response[i] = comm_port_test(com_port)) != -1) // while the serial buffer is not empty, read comport i++; response[i] = '\0'; // terminate string, erase -1 #endif prompt_pos = strchr(response, '>'); if (prompt_pos != NULL) { #ifdef LOG_COMMS write_comm_log("RX", response); #endif *prompt_pos = '\0'; // erase ">" return PROMPT; // command prompt detected } else if (strlen(response) == 0) // if the string is empty, return EMPTY; else //otherwise, { #ifdef LOG_COMMS write_comm_log("RX", response); #endif return DATA; } }
void send_command(const char *command) { char tx_buf[32]; #ifdef ALLEGRO_WINDOWS DWORD bytes_written; #endif sprintf(tx_buf, "%s\r", command); // Append CR to the command #ifdef ALLEGRO_WINDOWS PurgeComm(com_port, PURGE_TXCLEAR|PURGE_RXCLEAR); WriteFile(com_port, tx_buf, strlen(tx_buf), &bytes_written, 0); if (bytes_written != strlen(tx_buf)) { #ifdef LOG_COMMS log_comm("TX ERROR", tx_buf); // Log transmission error #endif return; } #elif TERMIOS // printf("tx:'%s'\n",command); if( write(fdtty,tx_buf,strlen(tx_buf)) == -1 ) { perror("write tty"); close(fdtty); fdtty = -1; } else { tcflush(fdtty, TCIFLUSH); } #else comm_port_flush_output(com_port); comm_port_flush_input(com_port); comm_port_string_send(com_port, tx_buf); #endif #ifdef LOG_COMMS write_comm_log("TX", tx_buf); #endif }
void send_command(const char *command) { char tx_buf[32]; sprintf(tx_buf, "%s\r", command); // Append CR to the command #ifdef LOG_COMMS write_comm_log("TX", tx_buf); #endif #ifdef ALLEGRO_WINDOWS DWORD bytes_written; PurgeComm(com_port, PURGE_TXCLEAR|PURGE_RXCLEAR); WriteFile(com_port, tx_buf, strlen(tx_buf), &bytes_written, 0); #else comm_port_flush_output(com_port); comm_port_flush_input(com_port); comm_port_string_send(com_port, tx_buf); #endif }
int read_comport(char *response) { char *prompt_pos = NULL; #ifdef ALLEGRO_WINDOWS DWORD bytes_read = 0; DWORD errors; COMSTAT stat; int i, j; response[0] = '\0'; ClearCommError(com_port, &errors, &stat); if (stat.cbInQue > 0) ReadFile(com_port, response, stat.cbInQue, &bytes_read, 0); // Remove extraneous 0s for (i = 0, j = 0; i < bytes_read; i++) if (response[i] > 0) response[j++] = response[i]; response[j] = 0; #elif TERMIOS int res=1; fd_set readfs; struct timeval timeout; char tmp[64]; bzero(response,64); bzero(tmp,64); FD_ZERO(&readfs); FD_SET(fdtty, &readfs); while( res != 0 && fdtty != -1) { timeout.tv_usec = 200e3; /* millisecondes */ timeout.tv_sec = 0; /* secondes */ res = select(fdtty+1, &readfs, NULL, NULL, &timeout); if( res != 0 ) { if( read(fdtty, tmp, 64) != 0 ) { strcat( response, tmp ); bzero(tmp,64); } } } // printf("rx:'"); // i=0;while(response[i]!=0) { printf("%c",response[i]>=32?response[i]:'.');i++;} // printf("'\n"); #else int i; char c; i = 0; while((c = comm_port_test(com_port)) >= 0) // while the serial buffer is not empty, read comport if (c > 0) response[i++] = c; response[i] = 0; #endif prompt_pos = strchr(response, '>'); if (prompt_pos != NULL) { #ifdef LOG_COMMS write_comm_log("RX", response); #endif *prompt_pos = '\0'; // erase ">" return PROMPT; // command prompt detected } else if (strlen(response) == 0) // if the string is empty, return EMPTY; else //otherwise, { #ifdef LOG_COMMS write_comm_log("RX", response); #endif return DATA; } }