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 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; } }
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; } }
int main(void) { int c; #ifdef ALLEGRO_H unsigned char ch; allegro_init(); install_keyboard(); #endif dzcomm_init(); /* Set up comm1 */ if ((port1 = comm_port_init(_com1)) == NULL) { dz_print_comm_err(); exit(1); } if (comm_port_load_settings(port1, "exterm1.ini") == 0) { dz_print_comm_err(); exit(1); } if (!comm_port_install_handler(port1)) { dz_print_comm_err(); exit(1); } cur_port = port1; #ifdef ALLEGRO_H /* Set up comm2 */ if ((port2 = comm_port_init(_com2)) == NULL) { dz_print_comm_err(); exit(1); } if (comm_port_load_settings(port2, "exterm2.ini") == 0) { dz_print_comm_err(); exit(1); } if (!comm_port_install_handler(port2)) { dz_print_comm_err(); exit(1); } #else port2 = port1; #endif #ifdef ALLEGRO_H initialise_screen(); #else printf("Press Ctrl-C for a messy quit.\n"); printf("\nCurrent port is: %s.\n\n", cur_port->szName); #endif while(1) { #ifdef ALLEGRO_H if (keypressed()) { c = readkey(); ch = ascii_(c); if (ctrl_(c,'C')) { return (0); } else if (ctrl_(c,'B')) { comm_port_send_break(cur_port, 500); } else if (ctrl_(c,'M')) { if (cur_port == port2) cur_port = port1; else cur_port = port2; inform_port_change(); } else comm_port_out(cur_port, ch); } #endif if ((c = comm_port_test(cur_port)) != -1) { show_received_character(data_(c)); } } }
int main_loop(char *desc, int max_val) { static int nsy = 1; int r; int got_i1; int sent_i1; int got_i2; int sent_i2; got_i1 = -1; sent_i1 = 0; if (port2) { got_i2 = -1; sent_i2 = 0; } else { got_i2 = max_val; sent_i2 = max_val; } comm_port_reinstall(port1); if (port2 != NULL) comm_port_reinstall(port2); if (nsy) { fprintf(stderr, "Press return to start\n"); #ifdef ALLEGRO_H while (!keypressed()); readkey(); #else getc(stdin); #endif nsy = 0; } /* We are now listening at the new configuration. Wait for 0.1 s * before starting to transmit to allow the slower system to also * be listening before we tranmit. Don't need to do this on the * first time thorugh (nsy condition above) as the user has provides * the synchronisation then. */ else if (port2 == NULL) { #ifdef ALLEGRO_H rest(100); #else usleep(100000); #endif } do { if (sent_i1 < max_val) { comm_port_out(port1, (unsigned char) sent_i1++); } if ((r = comm_port_test(port1)) >= 0) { if (r == (got_i1+1)) got_i1++; else fprintf(stderr, "ERROR on 1 [expected %d got %d]\n", got_i1+1, r); } if (port2) { if (sent_i2 < max_val) { comm_port_out(port2, (unsigned char) sent_i2++); } if ((r = comm_port_test(port2)) >= 0) { if (r == (got_i2+1)) got_i2++; else fprintf(stderr, "ERROR on 2 [expected %d got %d]\n", got_i2+1, r); } } /* Any key will result in a stop if compiled with allegro. Otherwise, * use ctrl-c. */ #ifdef ALLEGRO_H if (keypressed()) { comm_port_flush_output(port1); if (port2 != NULL) comm_port_flush_output(port2); fprintf(stderr, "\nSTOPPED BY USER\n"); return -1; } #endif show_status(desc, sent_i1-1, got_i1, sent_i2-1, got_i2); } while ((got_i1 < (max_val-1)) || (sent_i1 < (max_val-1)) || (got_i2 < (max_val-1)) || (sent_i2 < (max_val-1))); show_status(desc, -2, 0, 0, 0); return 1; }