diff --git a/openbci_c.c b/openbci_c.c index 477dd3a..9f1bf49 100644 --- a/openbci_c.c +++ b/openbci_c.c @@ -20,7 +20,7 @@ int isStreaming = FALSE; unsigned char parseBuffer[BUFFERSIZE] = {'\0'}; //Array of Unsigned Chars, initilized with the null-character ('\0') -int dollaBills = 0; //Used to determine if a string was sent (depreciated?) +int dollaBills = 0; //Used to determine if a string was sent (deprecated?) @@ -28,7 +28,7 @@ int dollaBills = 0; //Used to determine if a * Function: set_port * -------------------- * Sets the name of the serial port that the dongle is connected to. -* E.g. 'COM1', '/dev/ttyUSB0', '/dev/ttyACM0' +* E.g. 'COM1', '/dev/ttyUSB0', '/dev/ttyACM0', 'dev/ttyS0' * */ void set_port(char* input){ @@ -50,31 +50,47 @@ int open_port(){ return fd; } - +/** +* Function: find_port +* -------------------- +* Attempts to find the port that is in use by the device by iterating through commonly +* used Linux serial and usb ports. +* +* TODO: Make the probing a little more elegant than a hard coded string literal passed into +* set_port. +*/ void find_port(){ //get values from the system itself (particularly utsname.sysname) struct utsname unameData; uname(&unameData); int return_val = 0; char stringLiteral[12]; + char successfulPort[12]; if(strcmp(unameData.sysname, "Linux") == 0 || strcmp(unameData.sysname, "cygwin")) { //Linux int repeat = TRUE; + char deviceprefix[3][12] = {"/dev/ttyACM", "/dev/ttyUSB", "/dev/ttyS"}; while(repeat==TRUE){ for(int i = 0; i < 34; i++){ - TRY{ - sprintf(stringLiteral, "/dev/ttyUSB%i",i ); - set_port(stringLiteral); - return_val = open_port(); - if(return_val == -1) THROW; - else return; - } - CATCH{ - printf("\nError opening on port /dev/ttyUSB%i %s\n",i , strerror(errno)); + for (size_t j = 0; j < sizeof(deviceprefix)/sizeof(deviceprefix[0]); j++){ + TRY{ + sprintf(stringLiteral, "%s%i", deviceprefix[j], i); + set_port(stringLiteral); + return_val = open_port(); + if(return_val == -1) + THROW; + else + sprintf(successfulPort, "%s%i", deviceprefix[j], i); + printf("Using port %s\n", successfulPort); + return; + } + CATCH{ + printf("\nError opening on port %s%i %s\n", deviceprefix[j], i , strerror(errno)); + } + ETRY; } - ETRY; } sleep(3); @@ -82,8 +98,10 @@ void find_port(){ } - else if(strcmp(unameData.sysname, "ERROR") == 0) printf("Windows\n"); - else if(strcmp(unameData.sysname, "darwin") == 0) printf("Darwin\n"); + else if(strcmp(unameData.sysname, "ERROR") == 0) + printf("Windows device discovery not yet supported.\n"); + else if(strcmp(unameData.sysname, "darwin") == 0) + printf("Darwinian descendant device discovery not yet supported.\n"); } @@ -326,12 +344,12 @@ void clear_buffer(){ /* Prints the packet passed to it */ void print_packet(struct packet p){ - printf("\nSAMPLE NUMBER %g\n",p.output[0]); + printf("\nSAMPLE NUMBER %g\n", p.output[0]); int acc_channel = 0; - for(int i = 1; i <= 8; i++) printf("Channel Number %i : %g\n",i,p.output[i]); + for(int i = 1; i <= 8; i++) printf("Channel Number %i : %g\n", i, p.output[i]); - for(int i = 9; i <= 11; i++) printf("Acc Channel %i : %f\n",acc_channel++, p.output[i]); + for(int i = 9; i <= 11; i++) printf("Acc Channel %i : %f\n", acc_channel++, p.output[i]); }