int serialOpenByNumber(SerialPort *SP, int port) { char device[32]; sprintf(device,"\\\\.\\COM%d",port); return serialOpenByName(SP, device); }
int main(int argc, char *argv[]) { SerialPort SP; char buf[1025]; int nr; if (argc<1) { printf("Usage: setupbluesmirf <device>\n"); printf(" <device> must be your serial port, e.g, COM3: on Windows, or /dev/ttyS0 on Linux\n"); return 0; } if (!serialOpenByName(&SP, argv[1])) { fprintf(stderr, "Could not open serial port %s\n", argv[1]); return 1; } // last parameter is timeout in 1/10 of a second if (!serialSetParameters(&SP, 57600, 8, 0, 1, 1)) { fprintf(stderr, "Could not modify serial port parameters\n"); return 1; } nr = serialRead(&SP, 1024, buf); serialWrite(&SP, 3, "$$$"); nr = serialRead(&SP, 1024, buf); if (nr > 0) { buf[nr]=0; printf("Received after $$$: %s", buf); } else { goto cleanup; } serialWrite(&SP, 2, "D\r"); nr = serialRead(&SP, 1024, buf); if (nr > 0) { buf[nr]=0; printf("Received after D<cr>: %s", buf); } else { goto cleanup; } serialWrite(&SP, 9, "U,576K,N\r"); nr = serialRead(&SP, 1024, buf); if (nr > 0) { buf[nr]=0; printf("Received after U,576K,N<cr>: %s", buf); } else { goto cleanup; } cleanup: serialClose(&SP); return 0; }
int main(int argc, char *argv[]) { int n, i, c, count = 0, sample = 0, chan = 0, status = 0, verbose = 0, labelSize; unsigned char buf[OPENBCI_BUFLEN], byte; char *labelString; SerialPort SP; host_t host; struct timespec tic, toc; /* these represent the general acquisition system properties */ int nchans = OPENBCI_NCHANS; float fsample = OPENBCI_FSAMPLE; /* these are used in the communication with the FT buffer and represent statefull information */ int ftSocket = -1; ft_buffer_server_t *ftServer; message_t *request = NULL; message_t *response = NULL; header_t *header = NULL; data_t *data = NULL; ft_chunkdef_t *label = NULL; /* this contains the configuration details */ configuration config; /* configure the default settings */ config.blocksize = 10; config.port = 1972; config.hostname = strdup("-"); config.serial = strdup("/dev/tty.usbserial-DN0094FY"); config.reset = strdup("on"); config.datalog = strdup("off"); config.testsignal = strdup("off"); config.timestamp = strdup("on"); config.timeref = strdup("start"); config.enable_chan1 = strdup("on"); config.enable_chan2 = strdup("on"); config.enable_chan3 = strdup("on"); config.enable_chan4 = strdup("on"); config.enable_chan5 = strdup("on"); config.enable_chan6 = strdup("on"); config.enable_chan7 = strdup("on"); config.enable_chan8 = strdup("on"); config.enable_chan9 = strdup("on"); config.enable_chan10 = strdup("on"); config.enable_chan11 = strdup("on"); config.label_chan1 = strdup("ADC1"); config.label_chan2 = strdup("ADC2"); config.label_chan3 = strdup("ADC3"); config.label_chan4 = strdup("ADC4"); config.label_chan5 = strdup("ADC5"); config.label_chan6 = strdup("ADC6"); config.label_chan7 = strdup("ADC7"); config.label_chan8 = strdup("ADC8"); config.label_chan9 = strdup("AccelerationX"); config.label_chan10 = strdup("AccelerationY"); config.label_chan11 = strdup("AccelerationZ"); config.label_chan12 = strdup("TimeStamp"); config.setting_chan1 = strdup("x1060110X"); config.setting_chan2 = strdup("x2060110X"); config.setting_chan3 = strdup("x3060110X"); config.setting_chan4 = strdup("x4060110X"); config.setting_chan5 = strdup("x5060110X"); config.setting_chan6 = strdup("x6060110X"); config.setting_chan7 = strdup("x7060110X"); config.setting_chan8 = strdup("x8060110X"); config.impedance_chan1 = strdup("z100Z"); config.impedance_chan2 = strdup("z200Z"); config.impedance_chan3 = strdup("z300Z"); config.impedance_chan4 = strdup("z400Z"); config.impedance_chan5 = strdup("z500Z"); config.impedance_chan6 = strdup("z600Z"); config.impedance_chan7 = strdup("z700Z"); config.impedance_chan8 = strdup("z800Z"); if (argc<2) { printf(usage); exit(0); } if (argc==2) { if (strncmp(argv[1], "/dev", 4)==0 || strncasecmp(argv[1], "COM", 3)==0) /* the second argument is the serial port */ config.serial = strdup(argv[1]); else { /* the second argument is the configuration file */ fprintf(stderr, "openbci2ft: loading configuration from '%s'\n", argv[1]); if (ini_parse(argv[1], iniHandler, &config) < 0) { fprintf(stderr, "Can't load '%s'\n", argv[1]); return 1; } } } if (argc>2) strcpy(host.name, argv[2]); else { strcpy(host.name, config.hostname); } if (argc>3) host.port = atoi(argv[3]); else { host.port = config.port; } #define ISTRUE(s) strcasecmp(s, "on")==0 nchans = 0; if (ISTRUE(config.enable_chan1)) nchans++; if (ISTRUE(config.enable_chan2)) nchans++; if (ISTRUE(config.enable_chan3)) nchans++; if (ISTRUE(config.enable_chan4)) nchans++; if (ISTRUE(config.enable_chan5)) nchans++; if (ISTRUE(config.enable_chan6)) nchans++; if (ISTRUE(config.enable_chan7)) nchans++; if (ISTRUE(config.enable_chan8)) nchans++; if (ISTRUE(config.enable_chan9)) nchans++; if (ISTRUE(config.enable_chan10)) nchans++; if (ISTRUE(config.enable_chan11)) nchans++; if (ISTRUE(config.timestamp)) nchans++; fprintf(stderr, "openbci2ft: serial = %s\n", config.serial); fprintf(stderr, "openbci2ft: hostname = %s\n", host.name); fprintf(stderr, "openbci2ft: port = %d\n", host.port); fprintf(stderr, "openbci2ft: blocksize = %d\n", config.blocksize); fprintf(stderr, "openbci2ft: reset = %s\n", config.reset); fprintf(stderr, "openbci2ft: datalog = %s\n", config.datalog); fprintf(stderr, "openbci2ft: timestamp = %s\n", config.timestamp); fprintf(stderr, "openbci2ft: testsignal = %s\n", config.testsignal); /* Spawn tcpserver or connect to remote buffer */ if (strcmp(host.name, "-") == 0) { ftServer = ft_start_buffer_server(host.port, NULL, NULL, NULL); if (ftServer==NULL) { fprintf(stderr, "openbci2ft: could not start up a local buffer serving at port %i\n", host.port); return 1; } ftSocket = 0; printf("openbci2ft: streaming to local buffer on port %i\n", host.port); } else { ftSocket = open_connection(host.name, host.port); if (ftSocket < 0) { fprintf(stderr, "openbci2ft: could not connect to remote buffer at %s:%i\n", host.name, host.port); return 1; } printf("openbci2ft: streaming to remote buffer at %s:%i\n", host.name, host.port); } /* allocate the elements that will be used in the communication to the FT buffer */ request = malloc(sizeof(message_t)); request->def = malloc(sizeof(messagedef_t)); request->buf = NULL; request->def->version = VERSION; request->def->bufsize = 0; header = malloc(sizeof(header_t)); header->def = malloc(sizeof(headerdef_t)); header->buf = NULL; data = malloc(sizeof(data_t)); data->def = malloc(sizeof(datadef_t)); data->buf = NULL; /* define the header */ header->def->nchans = nchans; header->def->fsample = fsample; header->def->nsamples = 0; header->def->nevents = 0; header->def->data_type = DATATYPE_FLOAT32; header->def->bufsize = 0; /* FIXME add the channel names */ labelSize = 0; /* count the number of bytes required */ if (ISTRUE (config.enable_chan1)) labelSize += strlen (config.label_chan1) + 1; if (ISTRUE (config.enable_chan2)) labelSize += strlen (config.label_chan2) + 1; if (ISTRUE (config.enable_chan3)) labelSize += strlen (config.label_chan3) + 1; if (ISTRUE (config.enable_chan4)) labelSize += strlen (config.label_chan4) + 1; if (ISTRUE (config.enable_chan5)) labelSize += strlen (config.label_chan5) + 1; if (ISTRUE (config.enable_chan6)) labelSize += strlen (config.label_chan6) + 1; if (ISTRUE (config.enable_chan7)) labelSize += strlen (config.label_chan7) + 1; if (ISTRUE (config.enable_chan8)) labelSize += strlen (config.label_chan8) + 1; if (ISTRUE (config.enable_chan9)) labelSize += strlen (config.label_chan9) + 1; if (ISTRUE (config.enable_chan10)) labelSize += strlen (config.label_chan10) + 1; if (ISTRUE (config.enable_chan11)) labelSize += strlen (config.label_chan11) + 1; if (ISTRUE (config.timestamp)) labelSize += strlen (config.label_chan12) + 1; if (verbose > 0) fprintf (stderr, "openbci2ft: labelSize = %d\n", labelSize); /* go over all channels for a 2nd time, now copying the strings to the destination */ labelString = (char *) malloc (labelSize * sizeof(char)); labelSize = 0; if (ISTRUE (config.enable_chan1)) { strcpy (labelString+labelSize, config.label_chan1); labelSize += strlen (config.label_chan1) + 1; } if (ISTRUE (config.enable_chan2)) { strcpy (labelString+labelSize, config.label_chan2); labelSize += strlen (config.label_chan2) + 1; } if (ISTRUE (config.enable_chan3)) { strcpy (labelString+labelSize, config.label_chan3); labelSize += strlen (config.label_chan3) + 1; } if (ISTRUE (config.enable_chan4)) { strcpy (labelString+labelSize, config.label_chan4); labelSize += strlen (config.label_chan4) + 1; } if (ISTRUE (config.enable_chan5)) { strcpy (labelString+labelSize, config.label_chan5); labelSize += strlen (config.label_chan5) + 1; } if (ISTRUE (config.enable_chan6)) { strcpy (labelString+labelSize, config.label_chan6); labelSize += strlen (config.label_chan6) + 1; } if (ISTRUE (config.enable_chan7)) { strcpy (labelString+labelSize, config.label_chan7); labelSize += strlen (config.label_chan7) + 1; } if (ISTRUE (config.enable_chan8)) { strcpy (labelString+labelSize, config.label_chan8); labelSize += strlen (config.label_chan8) + 1; } if (ISTRUE (config.enable_chan9)) { strcpy (labelString+labelSize, config.label_chan9); labelSize += strlen (config.label_chan9) + 1; } if (ISTRUE (config.enable_chan10)) { strcpy (labelString+labelSize, config.label_chan10); labelSize += strlen (config.label_chan10) + 1; } if (ISTRUE (config.enable_chan11)) { strcpy (labelString+labelSize, config.label_chan11); labelSize += strlen (config.label_chan11) + 1; } if (ISTRUE (config.timestamp)) { strcpy (labelString+labelSize, config.label_chan12); labelSize += strlen (config.label_chan12) + 1; } /* add the channel label chunk to the header */ label = (ft_chunkdef_t *) malloc (sizeof (ft_chunkdef_t)); label->type = FT_CHUNK_CHANNEL_NAMES; label->size = labelSize; header->def->bufsize = append (&header->buf, header->def->bufsize, label, sizeof (ft_chunkdef_t)); header->def->bufsize = append (&header->buf, header->def->bufsize, labelString, labelSize); FREE (label); FREE (labelString); /* define the constant part of the data and allocate space for the variable part */ data->def->nchans = nchans; data->def->nsamples = config.blocksize; data->def->data_type = DATATYPE_FLOAT32; data->def->bufsize = WORDSIZE_FLOAT32 * nchans * config.blocksize; data->buf = malloc (data->def->bufsize); /* initialization phase, send the header */ request->def->command = PUT_HDR; request->def->bufsize = append (&request->buf, request->def->bufsize, header->def, sizeof (headerdef_t)); request->def->bufsize = append (&request->buf, request->def->bufsize, header->buf, header->def->bufsize); /* this is not needed any more */ cleanup_header (&header); status = clientrequest (ftSocket, request, &response); if (verbose > 0) fprintf (stderr, "openbci2ft: clientrequest returned %d\n", status); if (status) { fprintf (stderr, "openbci2ft: could not send request to buffer\n"); exit (1); } if (status || response == NULL || response->def == NULL) { fprintf (stderr, "openbci2ft: error in %s on line %d\n", __FILE__, __LINE__); exit (1); } cleanup_message (&request); if (response->def->command != PUT_OK) { fprintf (stderr, "openbci2ft: error in 'put header' request.\n"); exit (1); } cleanup_message (&response); /* open the serial port */ fprintf (stderr, "openbci2ft: opening serial port ...\n"); if (!serialOpenByName (&SP, config.serial)) { fprintf (stderr, "Could not open serial port %s\n", config.serial); return 1; } if (!serialSetParameters (&SP, 115200, 8, 0, 0, 0)) { fprintf (stderr, "Could not modify serial port parameters\n"); return 1; } fprintf (stderr, "openbci2ft: opening serial port ... ok\n"); /* 8-bit board will always be initialized upon opening serial port, 32-bit board needs explicit initialization */ fprintf (stderr, "openbci2ft: initializing ...\n"); fprintf (stderr, "openbci2ft: press reset on the OpenBCI board if this takes too long\n"); if (ISTRUE (config.reset)) serialWrite (&SP, 1, "v"); /* soft reset, this will return $$$ */ else serialWrite (&SP, 1, "D"); /* query default channel settings, this will also return $$$ */ /* wait for '$$$' which indicates that the OpenBCI has been initialized */ c = 0; while (c != 3) { usleep (1000); n = serialRead (&SP, 1, &byte); if (n == 1) { if (byte == '$') c++; else c = 0; } } /* while waiting for '$$$' */ if (strcasecmp (config.datalog, "14s") == 0) serialWrite (&SP, 1, "a"); else if (strcasecmp (config.datalog, "5min") == 0) serialWrite (&SP, 1, "A"); else if (strcasecmp (config.datalog, "15min") == 0) serialWrite (&SP, 1, "S"); else if (strcasecmp (config.datalog, "30min") == 0) serialWrite (&SP, 1, "F"); else if (strcasecmp (config.datalog, "1hr") == 0) serialWrite (&SP, 1, "G"); else if (strcasecmp (config.datalog, "2hr") == 0) serialWrite (&SP, 1, "H"); else if (strcasecmp (config.datalog, "4hr") == 0) serialWrite (&SP, 1, "J"); else if (strcasecmp (config.datalog, "12hr") == 0) serialWrite (&SP, 1, "K"); else if (strcasecmp (config.datalog, "24hr") == 0) serialWrite (&SP, 1, "L"); else if (strcasecmp (config.datalog, "off") != 0) { fprintf (stderr, "Incorrect specification of datalog\n"); return 1; } serialWriteSlow (&SP, strlen (config.setting_chan1), config.setting_chan1); serialWriteSlow (&SP, strlen (config.setting_chan2), config.setting_chan2); serialWriteSlow (&SP, strlen (config.setting_chan3), config.setting_chan3); serialWriteSlow (&SP, strlen (config.setting_chan4), config.setting_chan4); serialWriteSlow (&SP, strlen (config.setting_chan5), config.setting_chan5); serialWriteSlow (&SP, strlen (config.setting_chan6), config.setting_chan6); serialWriteSlow (&SP, strlen (config.setting_chan7), config.setting_chan7); serialWriteSlow (&SP, strlen (config.setting_chan8), config.setting_chan8); if (strcasecmp (config.testsignal, "gnd") == 0) serialWrite (&SP, 1, "0"); else if (strcasecmp (config.testsignal, "dc") == 0) serialWrite (&SP, 1, "-"); else if (strcasecmp (config.testsignal, "1xSlow") == 0) serialWrite (&SP, 1, "="); else if (strcasecmp (config.testsignal, "1xFast") == 0) serialWrite (&SP, 1, "p"); else if (strcasecmp (config.testsignal, "2xSlow") == 0) serialWrite (&SP, 1, "["); else if (strcasecmp (config.testsignal, "2xFast") == 0) serialWrite (&SP, 1, "]"); else if (strcasecmp (config.testsignal, "off") != 0) { fprintf (stderr, "Incorrect specification of testsignal\n"); return 1; } fprintf (stderr, "openbci2ft: initializing ... ok\n"); printf ("Starting to listen - press CTRL-C to quit\n"); /* register CTRL-C handler */ signal (SIGINT, abortHandler); /* start streaming data */ serialWrite (&SP, 1, "b"); /* determine the reference time for the timestamps */ if (strcasecmp (config.timeref, "start") == 0) { /* since the start of the acquisition */ get_monotonic_time (&tic, TIMESTAMP_REF_BOOT); } else if (strcasecmp (config.timeref, "boot") == 0) { /* since the start of the day */ tic.tv_sec = 0; tic.tv_nsec = 0; } else if (strcasecmp (config.timeref, "epoch") == 0) { /* since the start of the epoch, i.e. 1-1-1970 */ tic.tv_sec = 0; tic.tv_nsec = 0; } else { fprintf (stderr, "Incorrect specification of timeref, should be 'start', 'day' or 'epoch'\n"); return 1; } while (keepRunning) { sample = 0; while (sample < config.blocksize) { /* wait for the first byte of the following packet */ buf[0] = 0; while (buf[0] != 0xA0) { if (serialInputPending (&SP)) n = serialRead (&SP, 1, buf); else usleep (1000); } /* while */ /* * Header * Byte 1: 0xA0 * Byte 2: Sample Number * * EEG Data * Note: values are 24-bit signed, MSB first * Bytes 3-5: Data value for EEG channel 1 * Bytes 6-8: Data value for EEG channel 2 * Bytes 9-11: Data value for EEG channel 3 * Bytes 12-14: Data value for EEG channel 4 * Bytes 15-17: Data value for EEG channel 5 * Bytes 18-20: Data value for EEG channel 6 * Bytes 21-23: Data value for EEG channel 6 * Bytes 24-26: Data value for EEG channel 8 * * Accelerometer Data * Note: values are 16-bit signed, MSB first * Bytes 27-28: Data value for accelerometer channel X * Bytes 29-30: Data value for accelerometer channel Y * Bytes 31-32: Data value for accelerometer channel Z * * Footer * Byte 33: 0xC0 */ /* read the remaining 32 bytes of the packet */ while (n < OPENBCI_BUFLEN) if (serialInputPending (&SP)) n += serialRead (&SP, (OPENBCI_BUFLEN - n), buf + n); else usleep (1000); if (verbose > 1) { for (i = 0; i < OPENBCI_BUFLEN; i++) printf ("%02x ", buf[i]); printf ("\n"); } chan = 0; if (ISTRUE (config.enable_chan1)) ((FLOAT32_T *) (data->buf))[nchans * sample + (chan++)] = OPENBCI_CALIB1 * (buf[2] << 24 | buf[3] << 16 | buf[4] << 8) / 255; if (ISTRUE (config.enable_chan2)) ((FLOAT32_T *) (data->buf))[nchans * sample + (chan++)] = OPENBCI_CALIB1 * (buf[5] << 24 | buf[6] << 16 | buf[7] << 8) / 255; if (ISTRUE (config.enable_chan3)) ((FLOAT32_T *) (data->buf))[nchans * sample + (chan++)] = OPENBCI_CALIB1 * (buf[8] << 24 | buf[9] << 16 | buf[10] << 8) / 255; if (ISTRUE (config.enable_chan4)) ((FLOAT32_T *) (data->buf))[nchans * sample + (chan++)] = OPENBCI_CALIB1 * (buf[11] << 24 | buf[12] << 16 | buf[13] << 8) / 255; if (ISTRUE (config.enable_chan5)) ((FLOAT32_T *) (data->buf))[nchans * sample + (chan++)] = OPENBCI_CALIB1 * (buf[14] << 24 | buf[15] << 16 | buf[16] << 8) / 255; if (ISTRUE (config.enable_chan6)) ((FLOAT32_T *) (data->buf))[nchans * sample + (chan++)] = OPENBCI_CALIB1 * (buf[17] << 24 | buf[18] << 16 | buf[19] << 8) / 255; if (ISTRUE (config.enable_chan7)) ((FLOAT32_T *) (data->buf))[nchans * sample + (chan++)] = OPENBCI_CALIB1 * (buf[20] << 24 | buf[21] << 16 | buf[22] << 8) / 255; if (ISTRUE (config.enable_chan8)) ((FLOAT32_T *) (data->buf))[nchans * sample + (chan++)] = OPENBCI_CALIB1 * (buf[23] << 24 | buf[24] << 16 | buf[25] << 8) / 255; if (ISTRUE (config.enable_chan9)) ((FLOAT32_T *) (data->buf))[nchans * sample + (chan++)] = OPENBCI_CALIB2 * (buf[26] << 24 | buf[27] << 16) / 32767; if (ISTRUE (config.enable_chan10)) ((FLOAT32_T *) (data->buf))[nchans * sample + (chan++)] = OPENBCI_CALIB2 * (buf[28] << 24 | buf[29] << 16) / 32767; if (ISTRUE (config.enable_chan11)) ((FLOAT32_T *) (data->buf))[nchans * sample + (chan++)] = OPENBCI_CALIB2 * (buf[28] << 24 | buf[31] << 16) / 32767; if (ISTRUE (config.timestamp)) { if (strcasecmp (config.timeref, "start") == 0) get_monotonic_time (&toc, TIMESTAMP_REF_BOOT); else if (strcasecmp (config.timeref, "boot") == 0) get_monotonic_time (&toc, TIMESTAMP_REF_BOOT); else if (strcasecmp (config.timeref, "epoch") == 0) get_monotonic_time (&toc, TIMESTAMP_REF_EPOCH); ((FLOAT32_T *) (data->buf))[nchans * sample + (chan++)] = get_elapsed_time (&tic, &toc); } sample++; } /* while c<config.blocksize */ count += sample; printf ("openbci2ft: sample count = %i\n", count); /* create the request */ request = malloc (sizeof (message_t)); request->def = malloc (sizeof (messagedef_t)); request->buf = NULL; request->def->version = VERSION; request->def->bufsize = 0; request->def->command = PUT_DAT; request->def->bufsize = append (&request->buf, request->def->bufsize, data->def, sizeof (datadef_t)); request->def->bufsize = append (&request->buf, request->def->bufsize, data->buf, data->def->bufsize); status = clientrequest (ftSocket, request, &response); if (verbose > 0) fprintf (stderr, "openbci2ft: clientrequest returned %d\n", status); if (status) { fprintf (stderr, "openbci2ft: error in %s on line %d\n", __FILE__, __LINE__); exit (1); } if (status) { fprintf (stderr, "openbci2ft: error in %s on line %d\n", __FILE__, __LINE__); exit (1); } /* FIXME do someting with the response, i.e. check that it is OK */ cleanup_message (&request); if (response == NULL || response->def == NULL || response->def->command != PUT_OK) { fprintf (stderr, "Error when writing samples.\n"); } cleanup_message (&response); } /* while keepRunning */ /* stop streaming data */ serialWrite (&SP, 1, "s"); cleanup_data (&data); if (ftSocket > 0) { close_connection (ftSocket); } else { ft_stop_buffer_server (ftServer); } return 0; } /* main */
int main(int argc, char *argv[]) { int n, i, c, sample = 0, status = 0, verbose = 0; unsigned char buf[BUFLEN], byte; SerialPort SP; host_t host; /* these represent the acquisition system properties */ int nchans = OPENBCI_NCHANS; int blocksize = BLOCKSIZE; float fsample = OPENBCI_FSAMPLE; /* these are used in the communication with the FT buffer and represent statefull information */ int ftSocket = -1; ft_buffer_server_t *ftServer; message_t *request = NULL; message_t *response = NULL; header_t *header = NULL; data_t *data = NULL; if (argc<2) { printf(usage); exit(0); } if (argc>2) strcpy(host.name, argv[2]); else { strcpy(host.name, FTHOST); } if (argc>3) host.port = atoi(argv[3]); else { host.port = FTPORT; } fprintf(stderr, "openbci2ft: device = %s\n", argv[1]); fprintf(stderr, "openbci2ft: hostname = %s\n", host.name); fprintf(stderr, "openbci2ft: port = %d\n", host.port); /* Spawn tcpserver or connect to remote buffer */ if (strcmp(host.name, "-") == 0) { ftServer = ft_start_buffer_server(host.port, NULL, NULL, NULL); if (ftServer==NULL) { fprintf(stderr, "openbci2ft: could not start up a local buffer serving at port %i\n", host.port); return 1; } ftSocket = 0; printf("openbci2ft: streaming to local buffer on port %i\n", host.port); } else { ftSocket = open_connection(host.name, host.port); if (ftSocket < 0) { fprintf(stderr, "openbci2ft: could not connect to remote buffer at %s:%i\n", host.name, host.port); return 1; } printf("openbci2ft: streaming to remote buffer at %s:%i\n", host.name, host.port); } /* allocate the elements that will be used in the communication to the FT buffer */ request = malloc(sizeof(message_t)); request->def = malloc(sizeof(messagedef_t)); request->buf = NULL; request->def->version = VERSION; request->def->bufsize = 0; header = malloc(sizeof(header_t)); header->def = malloc(sizeof(headerdef_t)); header->buf = NULL; data = malloc(sizeof(data_t)); data->def = malloc(sizeof(datadef_t)); data->buf = NULL; /* define the header */ header->def->nchans = nchans; header->def->fsample = fsample; header->def->nsamples = 0; header->def->nevents = 0; header->def->data_type = DATATYPE_FLOAT32; header->def->bufsize = 0; /* define the constant part of the data and allocate space for the variable part */ data->def->nchans = nchans; data->def->nsamples = blocksize; data->def->data_type = DATATYPE_FLOAT32; data->def->bufsize = WORDSIZE_FLOAT32*nchans*blocksize; data->buf = malloc(data->def->bufsize); /* initialization phase, send the header */ request->def->command = PUT_HDR; request->def->bufsize = append(&request->buf, request->def->bufsize, header->def, sizeof(headerdef_t)); request->def->bufsize = append(&request->buf, request->def->bufsize, header->buf, header->def->bufsize); /* this is not needed any more */ cleanup_header(&header); status = clientrequest(ftSocket, request, &response); if (verbose>0) fprintf(stderr, "openbci2ft: clientrequest returned %d\n", status); if (status) { fprintf(stderr, "openbci2ft: could not send request to buffer\n"); exit(1); } if (status || response==NULL || response->def == NULL) { fprintf(stderr, "openbci2ft: error in %s on line %d\n", __FILE__, __LINE__); exit(1); } cleanup_message(&request); if (response->def->command != PUT_OK) { fprintf(stderr, "openbci2ft: error in 'put header' request.\n"); exit(1); } cleanup_message(&response); /* open the serial port */ fprintf(stderr, "openbci2ft: opening serial port ...\n"); if (!serialOpenByName(&SP, argv[1])) { fprintf(stderr, "Could not open serial port %s\n", argv[1]); return 1; } if (!serialSetParameters(&SP, 115200, 8, 0, 0, 0)) { fprintf(stderr, "Could not modify serial port parameters\n"); return 1; } fprintf(stderr, "openbci2ft: opening serial port ... ok\n"); /* 8-bit board will always be initialized upon opening serial port, 32-bit board needs explicit initialization */ fprintf(stderr, "openbci2ft: initializing ...\n"); serialWrite(&SP, 1, "v"); fprintf(stderr, "openbci2ft: press reset on the OpenBCI board if this takes too long\n"); usleep(1000); /* wait for '$$$' which indicates that the OpenBCI has been initialized */ c = 0; while (c!=3) { n = serialRead(&SP, 1, &byte); if (n==1) { if (byte=='$') c++; else c = 0; } } /* while waiting for '$$$' */ fprintf(stderr, "openbci2ft: initializing ... ok\n"); printf("Starting to listen - press CTRL-C to quit\n"); /* register CTRL-C handler */ signal(SIGINT, abortHandler); /* start streaming data */ serialWrite(&SP, 1, "b"); while (keepRunning) { c = 0; while (c<blocksize) { /* wait for the first byte of the packet */ buf[0]=0; while (buf[0]!=0xA0) { if (serialInputPending(&SP)) n = serialRead(&SP, 1, buf); else usleep(1000); } /* while */ /* read the remaining 32 bytes of the packet */ while (n<BUFLEN) if (serialInputPending(&SP)) n += serialRead(&SP, (BUFLEN-n), buf+n); else usleep(100000); if (verbose>1) { for (i=0; i<BUFLEN; i++) printf("%02x ", buf[i]); printf("\n"); } ((FLOAT32_T *)(data->buf))[nchans*c + 0] = OPENBCI_CALIB1 * (buf[ 2]<<24 | buf[ 3]<<16 | buf[ 4]<<8)/255; ((FLOAT32_T *)(data->buf))[nchans*c + 1] = OPENBCI_CALIB1 * (buf[ 5]<<24 | buf[ 6]<<16 | buf[ 7]<<8)/255; ((FLOAT32_T *)(data->buf))[nchans*c + 2] = OPENBCI_CALIB1 * (buf[ 8]<<24 | buf[ 9]<<16 | buf[10]<<8)/255; ((FLOAT32_T *)(data->buf))[nchans*c + 3] = OPENBCI_CALIB1 * (buf[11]<<24 | buf[12]<<16 | buf[13]<<8)/255; ((FLOAT32_T *)(data->buf))[nchans*c + 4] = OPENBCI_CALIB1 * (buf[14]<<24 | buf[15]<<16 | buf[16]<<8)/255; ((FLOAT32_T *)(data->buf))[nchans*c + 5] = OPENBCI_CALIB1 * (buf[17]<<24 | buf[18]<<16 | buf[19]<<8)/255; ((FLOAT32_T *)(data->buf))[nchans*c + 6] = OPENBCI_CALIB1 * (buf[20]<<24 | buf[21]<<16 | buf[22]<<8)/255; ((FLOAT32_T *)(data->buf))[nchans*c + 7] = OPENBCI_CALIB1 * (buf[23]<<24 | buf[24]<<16 | buf[25]<<8)/255; ((FLOAT32_T *)(data->buf))[nchans*c + 8] = OPENBCI_CALIB2 * (buf[26]<<24 | buf[27]<<16)/32767; ((FLOAT32_T *)(data->buf))[nchans*c + 9] = OPENBCI_CALIB2 * (buf[28]<<24 | buf[29]<<16)/32767; ((FLOAT32_T *)(data->buf))[nchans*c +10] = OPENBCI_CALIB2 * (buf[28]<<24 | buf[31]<<16)/32767; c++; } /* while c<blocksize */ sample += blocksize; printf("openbci2ft: sample count = %i\n", sample); /* * Header * Byte 1: 0xA0 * Byte 2: Sample Number * * EEG Data * Note: values are 24-bit signed, MSB first * Bytes 3-5: Data value for EEG channel 1 * Bytes 6-8: Data value for EEG channel 2 * Bytes 9-11: Data value for EEG channel 3 * Bytes 12-14: Data value for EEG channel 4 * Bytes 15-17: Data value for EEG channel 5 * Bytes 18-20: Data value for EEG channel 6 * Bytes 21-23: Data value for EEG channel 6 * Bytes 24-26: Data value for EEG channel 8 * * Accelerometer Data * Note: values are 16-bit signed, MSB first * Bytes 27-28: Data value for accelerometer channel X * Bytes 29-30: Data value for accelerometer channel Y * Bytes 31-32: Data value for accelerometer channel Z * * Footer * Byte 33: 0xC0 */ /* create the request */ request = malloc(sizeof(message_t)); request->def = malloc(sizeof(messagedef_t)); request->buf = NULL; request->def->version = VERSION; request->def->bufsize = 0; request->def->command = PUT_DAT; request->def->bufsize = append(&request->buf, request->def->bufsize, data->def, sizeof(datadef_t)); request->def->bufsize = append(&request->buf, request->def->bufsize, data->buf, data->def->bufsize); status = clientrequest(ftSocket, request, &response); if (verbose>0) fprintf(stderr, "openbci2ft: clientrequest returned %d\n", status); if (status) { fprintf(stderr, "openbci2ft: error in %s on line %d\n", __FILE__, __LINE__); exit(1); } if (status) { fprintf(stderr, "openbci2ft: error in %s on line %d\n", __FILE__, __LINE__); exit(1); } /* FIXME do someting with the response, i.e. check that it is OK */ cleanup_message(&request); if (response == NULL || response->def == NULL || response->def->command!=PUT_OK) { fprintf(stderr, "Error when writing samples.\n"); } cleanup_message(&response); } /* while keepRunning */ /* stop streaming data */ serialWrite(&SP, 1, "s"); cleanup_data(&data); if (ftSocket > 0) { close_connection(ftSocket); } else { ft_stop_buffer_server(ftServer); } return 0; } /* main */
int main(int argc, char **argv) { SerialPort SP; int ftBuffer = -1; eventdef_t *evdef; UINT32_T sizetype, sizevalue, bufsize; char *valBuf; messagedef_t reqdef; message_t request, *response; char *confname; if (argc < 2) { confname = "serial_event.conf"; } else { confname = argv[1]; } if (parseConfig(&conf, confname) != 0) { printf("Errors during parsing the configuration file\n"); exit(1); } sizetype = wordsize_from_type(conf.type_type) * conf.type_numel; sizevalue = wordsize_from_type(conf.value_type) * conf.value_numel; bufsize = sizeof(eventdef_t) + sizetype + sizevalue; evdef = (eventdef_t *) malloc(bufsize); if (evdef == NULL) { printf("Out of memory\n"); exit(1); } /* prepare fixed fields */ reqdef.version = VERSION; reqdef.bufsize = bufsize; reqdef.command = PUT_EVT; request.def = &reqdef; request.buf = evdef; evdef->offset = conf.offset; evdef->duration = conf.duration; evdef->type_type = conf.type_type; evdef->type_numel = conf.type_numel; evdef->value_type = conf.value_type; evdef->value_numel = conf.value_numel; evdef->bufsize = sizetype + sizevalue; valBuf = (char *) evdef + sizeof(eventdef_t); memcpy(valBuf, conf.type_buf, sizetype); valBuf += sizetype; memcpy(valBuf, conf.value_buf, sizevalue); if (!serialOpenByName(&SP, conf.comport)) { printf("Could not open serial port.\n"); exit(1); } /* timeout = 1 decisecond (least common denominator) = 100 ms */ if (!serialSetParameters(&SP, conf.baudrate, conf.databits, conf.parity, conf.stopbits, 1)) { printf("Could not modify serial port parameters\n"); exit(1); } ftBuffer = open_connection(conf.hostname, conf.port); if (ftBuffer < 0) { printf("Connection to FieldTrip buffer failed.\n"); exit(1); } sample = conf.sample_start; udp_socket = create_udp_receiver(conf.udp_port); if (udp_socket != -1) { if (pthread_create(&udpThread, NULL, _udp_thread, NULL)) { printf("Warning: UDP socket thread could not be spawned.\n"); closesocket(udp_socket); udp_socket = -1; } } /* register CTRL-C handler */ signal(SIGINT, abortHandler); printf("Starting to listen - press CTRL-C to quit\n"); while (keepRunning) { char input; int n; n = serialRead(&SP, 1, &input); if (n<0) { printf("Error while reading from serial port - exiting\n"); break; } if (n==0) continue; /* timeout - just wait longer */ /* we got one */ if (conf.character != -1 && conf.character != input) { printf("Ignoring input %c\n", input); continue; } if (conf.set_value) *valBuf = input; pthread_mutex_lock(&sampleMutex); evdef->sample = sample; pthread_mutex_unlock(&sampleMutex); if (evdef->sample < 0) { printf("Ignoring negative sample (%i) event...\n", evdef->sample); } else { n = tcprequest(ftBuffer, &request, &response); if (n<0 || response == NULL) { printf("Error in FieldTrip connection\n"); } else { if (response->def == NULL || response->def->command != PUT_OK) { printf("FieldTrip server returned an error\n"); } else { printf("Sent off event (sample = %i, input = %c)\n", evdef->sample, input); } FREE(response->def); FREE(response->buf); free(response); } } pthread_mutex_lock(&sampleMutex); sample += conf.sample_increase; pthread_mutex_unlock(&sampleMutex); } if (udp_socket!=-1) { pthread_join(udpThread, NULL); closesocket(udp_socket); } close_connection(ftBuffer); serialClose(&SP); free(evdef); return 0; }
int serialOpenByNumber(SerialPort *SP, int port) { char device[16]; snprintf(device,16,"/dev/ttyS%d",port); return serialOpenByName(SP, device); }