Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor improvements to find_port() #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 36 additions & 18 deletions openbci_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ 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?)



/**
* 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){
Expand All @@ -50,40 +50,58 @@ 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);

}

}

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");

}

Expand Down Expand Up @@ -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]);

}

Expand Down