/********** * Polprog 2018 * 3 clause BSD licence * http://polprog.net */ #include #include #include #include "util.h" #include #define BUFSIZE 128 #define LINESIZE 26 #define SLEEP_MS 2500 void xxd(char* d){ while(*d != 0){ printf("%02X ", *d); d++; } } int main(int argc, char** argv){ if (argc < 3) { printf("Usage: %s \n", argv[0]); exit(1); } int sleeptime = atoi(argv[2]); struct termios options; //serial port options printf("Initializing serial port: 9600 8n1\n"); //initialize serial port int portfd = open_port(argv[1]); tcgetattr(portfd, &options); cfsetispeed(&options, B9600); cfsetospeed(&options, B9600); options.c_cflag &= ~CRTSCTS; //no hw flow control options.c_cflag &= ~PARENB; options.c_cflag &= ~CSTOPB; options.c_cflag &= ~CSIZE; /* Mask the character size bits */ options.c_cflag |= CS8; /* Select 8 data bits */ options.c_cflag |= (CLOCAL | CREAD); options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); //raw input fcntl(portfd, F_SETFL, FNDELAY); //enable blocking tcsetattr(portfd, TCSANOW, &options); printf("Port attributes set\n"); test:; /* char *input, *output; input = malloc(16); output = malloc(16); scanf("%s", input); write_chars(portfd, input); write_chars(portfd, "\r"); tcdrain(portfd); usleep(10000); read_line(portfd, output); printf("AVR returned: %s\n", output); */ char * buffer = malloc(BUFSIZE); char * line = malloc(LINESIZE); if ( line == NULL || buffer == NULL) exit(1); int bytes_read; int i = 0, j = 0; //i-buffer, j-line; do{ bytes_read = read(STDIN_FILENO, buffer, sizeof(buffer) - 1); if (bytes_read <= 0) break; buffer[bytes_read] = 0; while(i < bytes_read){ //printf("=== handling char %c\n", buffer[i]); line[j] = buffer[i]; if(buffer[i] == '\n'){ if(j <= LINESIZE-2) { line[j] = '\n'; line[j+1] = 0; //j = -1; printf(">%dch:\t%s\n", strlen(line), line); write_chars(portfd, line); xxd(line); printf("\n"); usleep(SLEEP_MS*sleeptime); } else { printf("line longer than allowed:\n"); printf(" %dch:\t%s\n", strlen(line), line); // j = -1; } j = -1; } j++; i++; } i=0; // printf("REREADING STDIN\n"); }while(1); //free(input); //free(output); return 0; }