Exemplo n.º 1
0
/**
 * Program which prints ThinkGear EEG_POWERS values to stdout.
 */
int main( void ) {

	opensocket();
	dataFile = fopen("dataStore.txt", "a+");

    /* Print driver version number */
    dllVersion = TG_GetDriverVersion();
    printf( "ThinkGear DLL version: %d\n", dllVersion );

    /* Get a connection ID handle to ThinkGear */
    connectionId = TG_GetNewConnectionId();
    if( connectionId < 0 ) {
        fprintf( stderr, "ERROR: TG_GetNewConnectionId() returned %d.\n", 
                 connectionId );
        wait();
        exit( EXIT_FAILURE );
    }

    /* Attempt to connect the connection ID handle to serial port "COM5" */
    //comPortName = "\\\\.\\COM19";
	comPortName = "COM7:";
    errCode = TG_Connect( connectionId, 
                          comPortName, 
                          TG_BAUD_57600, 
                          TG_STREAM_PACKETS );

	printf("TG_Connect struct: %d, %s, %d \n", connectionId, comPortName, TG_BAUD_57600);
    if( errCode < 0 ) {
        fprintf( stderr, "ERROR: TG_Connect() returned %d.\n", errCode );
        wait();
        exit( EXIT_FAILURE );
    }

    errCode = TG_EnableBlinkDetection( connectionId, 1 );
    if( errCode < 0 ) {
        fprintf( stderr, "ERROR: TG_EnableBlinkDetection() returned %d.\n", errCode );
        wait();
        exit( EXIT_FAILURE );
    }


	do
	{
		readdata();
		if(blink>=80)
			trncmd();
		else if(att>=68)
			fwdcmd();
		//wait();
		 
	}while(1);

    /* Clean up */
    TG_FreeConnection( connectionId );

    /* End program */
    wait();
    return( EXIT_SUCCESS );
}
Exemplo n.º 2
0
// 开始 think gear 数据读取
bool TGManager::PreStartThinkgear(const int baud_rate, const int stream_format )
{
	// 保存波特率和流格式
	m_baud_rate = baud_rate;		
	m_stream_format = stream_format;

	if (m_connectionId != -1)
	{
		TotalEndThinkgear();
	}

	string	errmsg;
	int		errCode = 0;
	// 获取 dll 版本号
	m_dllVersion = TG_GetDriverVersion();

	// 获取到 ThinkGear 的链接 ID 
	m_connectionId = TG_GetNewConnectionId();
	if( m_connectionId < 0 ) 
	{
		// 非法的 connection id
		m_connectionId = -1;
		errmsg =format("TG_GetNewConnectionId() returned %d.", m_connectionId);
		cout << errmsg << endl;
		return false;
	}

	// 设置并打开流 log file 
	errCode = TG_SetStreamLog( m_connectionId, "streamLog.txt" );
	if( errCode < 0 ) 
	{
		// 流 log file error
		m_connectionId = -1;
		errmsg =format("TG_SetStreamLog() returned %d.", errCode);
		cout << errmsg << endl;
		return false;
	}

	// 打开并设置 data (ThinkGear values) log file for connection 
	errCode = TG_SetDataLog( m_connectionId, "dataLog.txt" );
	if( errCode < 0 ) 
	{
		// 数据 log file error
		m_connectionId = -1;
		errmsg =format("TG_SetDataLog() returned %d.", errCode);
		cout << errmsg << endl;
		return false;
	}

	// Attempt to connect the connection ID handle to serial port "COM5" 
	if (m_comPortName.length() < 0)
	{
		m_comPortName = "\\\\.\\COM1";
	}

	return true;
}
Exemplo n.º 3
0
int MindwaveModule::connect(){
    char *comPortName;

    int   connectionId = -1;

    int   errCode      = 0;

    cout << "Connecting..."  << endl;

    /* Get a connection ID handle to ThinkGear */
    connectionId = TG_GetNewConnectionId();

    if( connectionId < 0 ) {
        cout << "ERROR: TG_GetNewConnectionId() returned " << connectionId << endl;
        return connectionId;
    }

    /* Set/open stream (raw bytes) log file for connection */
    errCode = TG_SetStreamLog( connectionId, "streamLog.txt" );
    if( errCode < 0 ) {
        cout << "ERROR: TG_SetStreamLog() returned " << errCode << endl;
        return connectionId;
    }

    /* Set/open data (ThinkGear values) log file for connection */
    errCode = TG_SetDataLog( connectionId, "dataLog.txt" );
    if( errCode < 0 ) {
        cout << "ERROR: TG_SetDataLog() returned " << errCode << endl;
        return connectionId;
    }

    /* Attempt to connect the connection ID handle to serial port "COM5" */
    /* NOTE: On Windows, COM10 and higher must be preceded by \\.\, as in
     *       "\\\\.\\COM12" (must escape backslashes in strings).  COM9
     *       and lower do not require the \\.\, but are allowed to include
     *       them.  On Mac OS X, COM ports are named like
     *       "/dev/tty.MindSet-DevB-1".
     */
    comPortName = "\\\\.\\COM5";
    errCode = TG_Connect( connectionId,
                         comPortName,
                         TG_BAUD_57600,
                         TG_STREAM_PACKETS );
    if( errCode < 0 ) {
        cout << "ERROR: TG_Connect() returned " << errCode << endl;
        return -1;
    }

    cout << "Connected: " << connectionId << endl;
    return connectionId;
}
bool ThinkgearCommsDriver::connect() {

    reset();
    
	comPortName = "\\\\.\\COM3";

	// driver version
	dllVersion = TG_GetDriverVersion();
	printf("ThinkGear DLL version: %d\n", dllVersion);

	// id conection
	connectionId = TG_GetNewConnectionId();
	printf("ThinkGear connectionId: %d\n", connectionId);


	if (connectionId < 0) {
		fprintf(stderr, "ERROR: TG_GetNewConnectionId() returned %d.\n",connectionId);
	}

    // attempt to connect    
    int conResult = TG_Connect(connectionId, deviceName.c_str(), TG_BAUD_57600, TG_STREAM_PACKETS);

	ofLogVerbose() << "Connecting to connectionID " << connectionId << ". Driver comPortName: " << deviceName;

    if (conResult != 0) {
        ofLogVerbose() << "Error: Connection Failed! " << conResult;
        if(conResult == -2)            
            ofLogVerbose() << "Serial port could not be opened!";
        isReady = false;
        return isReady;
    }
    
    
    // enable blinking for connection
    int resBlink = TG_EnableBlinkDetection(connectionId, 1);
    if (resBlink != 0)
        ofLogVerbose() << "Blink: Failed to enable blink detection";
    
    isReady = true;
    return isReady;
}
Exemplo n.º 5
0
void Mindwave::connect()
{
	_MdataFile = fopen("dataStore.txt", "a+");

	/* Print driver version number */
	_MdllVersion = TG_GetDriverVersion();
	printf( "ThinkGear DLL version: %d\n", _MdllVersion );

	/* Get a connection ID handle to ThinkGear */
	_MconnectionId = TG_GetNewConnectionId();
	if( _MconnectionId < 0 ) {
		fprintf( stderr, "ERROR: TG_GetNewConnectionId() returned %d.\n", 
			_MconnectionId );
		wait();
		exit( EXIT_FAILURE );
	}

	/* Attempt to connect the connection ID handle to serial port "COM5" */
	_McomPort = "\\\\.\\COM28";
	errCode = TG_Connect( _MconnectionId, 
		_McomPort, 
		TG_BAUD_57600, 
		TG_STREAM_PACKETS );
	if( errCode < 0 ) {
		fprintf( stderr, "ERROR: TG_Connect() returned %d.\n", errCode );
		wait();
		exit( EXIT_FAILURE );
	}

	errCode = TG_EnableBlinkDetection( _MconnectionId, 1 );
	if( errCode < 0 ) {
		fprintf( stderr, "ERROR: TG_EnableBlinkDetection() returned %d.\n", errCode );
		wait();
		exit( EXIT_FAILURE );
	}

}
Exemplo n.º 6
0
/**
 * Program which prints ThinkGear EEG_POWERS values to stdout.
 */
int
main( void ) {

    char *comPortName = NULL;
    int   dllVersion = 0;
    int   connectionId = 0;
    int   packetsRead = 0;
    int   errCode = 0;

    /* Print driver version number */
    dllVersion = TG_GetDriverVersion();
    printf( "ThinkGear DLL version: %d\n", dllVersion );

    /* Get a connection ID handle to ThinkGear */
    connectionId = TG_GetNewConnectionId();
    if( connectionId < 0 ) {
        fprintf( stderr, "ERROR: TG_GetNewConnectionId() returned %d.\n", 
                 connectionId );
        wait();
        exit( EXIT_FAILURE );
    }

    /* Set/open stream (raw bytes) log file for connection */
    errCode = TG_SetStreamLog( connectionId, "streamLog.txt" );
    if( errCode < 0 ) {
        fprintf( stderr, "ERROR: TG_SetStreamLog() returned %d.\n", errCode );
        wait();
        exit( EXIT_FAILURE );
    }

    /* Set/open data (ThinkGear values) log file for connection */
    errCode = TG_SetDataLog( connectionId, "dataLog.txt" );
    if( errCode < 0 ) {
        fprintf( stderr, "ERROR: TG_SetDataLog() returned %d.\n", errCode );
        wait();
        exit( EXIT_FAILURE );
    }

    /* Attempt to connect the connection ID handle to serial port "COM5" */
    comPortName = "\\\\.\\COM3";
    errCode = TG_Connect( connectionId, 
                          comPortName, 
                          TG_BAUD_57600, 
                          TG_STREAM_PACKETS );
    if( errCode < 0 ) {
        fprintf( stderr, "ERROR: TG_Connect() returned %d.\n", errCode );
        wait();
        exit( EXIT_FAILURE );
    }

		//To get eyeblinks, you will need to first call the following function:
		TG_EnableBlinkDetection(connectionId, 1 );

    /* Read 10 ThinkGear Packets from the connection, 1 Packet at a time */
    packetsRead = 0;
    while( 1 ) {

        /* Attempt to read a Packet of data from the connection */
        errCode = TG_ReadPackets( connectionId, 1 );

        /* If TG_ReadPackets() was able to read a complete Packet of data... */
        if( errCode == 1 ) {
            packetsRead++;

            /* If attention value has been updated by TG_ReadPackets()... */
            if( TG_GetValueStatus(connectionId, TG_DATA_ATTENTION) != 0 ) {

                /* Get and print out the updated attention value */
                fprintf( stdout, "New attention value: %d\n",
                         (int)TG_GetValue(connectionId, TG_DATA_ATTENTION) );
                fflush( stdout );

            } /* end "If attention value has been updated..." */

            /* If attention value has been updated by TG_ReadPackets()... */
            if( TG_GetValueStatus(connectionId, TG_DATA_MEDITATION) != 0 ) {

                /* Get and print out the updated attention value */
                fprintf( stdout, "New meditation value: %d\n",
                         (int)TG_GetValue(connectionId, TG_DATA_MEDITATION) );
                fflush( stdout );
                fprintf( stdout, "New blink value: %f\n",
                         TG_GetValue(connectionId, TG_DATA_BLINK_STRENGTH) );
                fflush( stdout );

            } /* end "If attention value has been updated..." */

        } /* end "If a Packet of data was read..." */

    } /* end "Read 10 Packets of data from connection..." */

    /* Clean up */
    TG_FreeConnection( connectionId );

    /* End program */
    wait();
    return( EXIT_SUCCESS );
}
Exemplo n.º 7
0
/**
* Program which prints ThinkGear EEG_POWERS values to stdout.
*/
int
	main( void ) {

		// http://andre.stechert.org/urwhatu/2006/01/error_c2143_syn.html
		// It's no surprise that Microsoft's compiler wants the variables at the beginnig of the function: Standard C wants them there.
		// At least, this is what C89 defines. C99 allows variable declarations also in the middle of a function.
		char *comPortName = NULL;
		char *portNumber = (char*)malloc(sizeof(char) * (2 + 1));
		char *comPortBase = NULL;
		int   dllVersion = 0;
		int   connectionId = 0;
		int   packetsRead = 0;
		int   errCode = 0;
		int comPortFound = 0;
		const int MAX_PORT = 16;
		size_t length = 0;
		int i = 0;
		int j = 0;
		int x = 0;
		int y = 0;
		INPUT input;
		POINT cursorPos;

		/* Print driver version number */
		dllVersion = TG_GetDriverVersion();
		printf( "ThinkGear DLL version: %d\n", dllVersion );

		/* Get a connection ID handle to ThinkGear */
		connectionId = TG_GetNewConnectionId();
		if( connectionId < 0 ) {
			fprintf( stderr, "ERROR: TG_GetNewConnectionId() returned %d.\n", 
				connectionId );
			wait();
			exit( EXIT_FAILURE );
		}
		fprintf( stderr, "ThinkGear Connection ID is: %d.\n\n", connectionId );

		/* Set/open stream (raw bytes) log file for connection */
		errCode = TG_SetStreamLog( connectionId, "streamLog.txt" );
		if( errCode < 0 ) {
			fprintf( stderr, "ERROR: TG_SetStreamLog() returned %d.\n", errCode );
			wait();
			exit( EXIT_FAILURE );
		}

		/* Set/open data (ThinkGear values) log file for connection */
		errCode = TG_SetDataLog( connectionId, "dataLog.txt" );
		if( errCode < 0 ) {
			fprintf( stderr, "ERROR: TG_SetDataLog() returned %d.\n", errCode );
			wait();
			exit( EXIT_FAILURE );
		}

		/* Attempt to connect the connection ID handle to serial ports between COM0 and "COM MAX_PORT" */
		fprintf(stdout, "Scanning COM ports 0 to %d...\n", MAX_PORT);
		comPortBase = "\\\\.\\COM";
		length = strlen(comPortBase);
		comPortName = (char *)realloc (comPortName, (length + 5)*sizeof(char)); 

		for(i=0; i <= MAX_PORT && comPortFound == 0; i++)
		{

			// Generating the serial port number			
			portNumber = itoa(i, portNumber, 10);
			fprintf( stderr, portNumber);
			strcpy(comPortName,comPortBase);

			for(j=0; j<strlen(portNumber); j++)
			{
				comPortName[length+j] = portNumber[j];
			}

			comPortName[length+strlen(portNumber)] = '\0';
			// Maybe I could have used something like strcat(comPortBase, portNumber);

			fprintf( stdout, "ok");
			fprintf( stdout, comPortName);

			//comPortName = "\\\\.\\COM11";

			// Trying to connect on the generated serial port number
			fprintf( stdout, "trying to connect on");
			fprintf( stdout, comPortName);
			fprintf( stdout, "\n");
			errCode = TG_Connect( connectionId, 
				comPortName, 
				TG_BAUD_9600, 
				TG_STREAM_PACKETS );
			if( errCode < 0 ) {
				fprintf( stderr, "ERROR: TG_Connect() returned %d.\n", errCode );
				if(errCode == -1) printf("FAILED connection (-1 connectionId does not refer to a valid ThinkGear Connection ID handle.)\n");
				if(errCode == -2) printf("FAILED connection (-2 serialPortName could not be opened as a serial communication port for any reason.)\n");
				if(errCode == -3) printf("FAILED connection (-3 serialBaudrate is not a valid TG_BAUD_* value.)\n");
				if(errCode == -4) printf("FAILED connection (-4 serialDataFormat is not a valid TG_STREAM_* type.)\n");
			} 
			else
			{

				// Trying to read one packet to check the connection.
				printf("Connection available...\n");
				Sleep(10000); // sometimes we need to wait a little...
				errCode = TG_ReadPackets(connectionId, 1);
				if(errCode >= 0)
				{	
					printf("OK\n");
					comPortFound = 1;
				}
				else
				{
					if(errCode == -1) printf("FAILED reading (Invalid connection ID)\n");
					if(errCode == -2) printf("FAILED reading (0 bytes on the stream)\n");
					if(errCode == -3) printf("FAILED reading (I/O error occured)\n");
				}


			}
		} 	/* end: "Attempt to connect the connection ID handle to serial ports between COM0 and "COM16"" */


		// Hopefully the connection should have been established now.
		// Otherwise we stop here.
		if (comPortFound == 1) {
			printf( "connection established");
		} else {
			printf( "connection could not be established");
			wait();
			exit( EXIT_FAILURE ); 
		}

		//To get eyeblinks, you will need to first call the following function:
		TG_EnableBlinkDetection(connectionId, 1 );

		/* Read 10 ThinkGear Packets from the connection, 1 Packet at a time */
		packetsRead = 0;
		while (1){
			//while( packetsRead < 10000 ) {

			/* Attempt to read a Packet of data from the connection */
			errCode = TG_ReadPackets( connectionId, 1 );


			/* If TG_ReadPackets() was able to read a complete Packet of data... */
			if( errCode == 1 ) {
				packetsRead++;
				printf( ".");

				/* If attention value has been updated by TG_ReadPackets()... */
				if( TG_GetValueStatus(connectionId, TG_DATA_ATTENTION) != 0 ) {

					/* Get and print out the updated attention value */
					fprintf( stdout, "New attention value: %f\n",
						TG_GetValue(connectionId, TG_DATA_ATTENTION) );
					fflush( stdout );

				} /* end "If attention value has been updated..." */


				/* If eye blinking value has been updated by TG_ReadPackets()... */
				if( TG_GetValueStatus(connectionId, TG_DATA_BLINK_STRENGTH) != 0 ) {

					/* Get and print out the updated eye blinking value */
					fprintf( stdout, "New eye blinking value: %f\n",
						TG_GetValue(connectionId, TG_DATA_BLINK_STRENGTH) );
					fflush( stdout );

					// Get mouse position: 
					// * http://msdn.microsoft.com/en-us/library/ms648380(v=vs.85).aspx#_win32_Using_the_Keyboard_to_Move_the_Cursor 
					// * http://msdn.microsoft.com/en-us/library/ms648390%28VS.85%29.aspx
					// * http://stackoverflow.com/questions/2396576/how-to-get-the-cursor-position

					GetCursorPos(&cursorPos);
					x = (int) cursorPos.x;
					y = (int) cursorPos.y;
					//x = 10;
					//y = 20;

					// http://stackoverflow.com/questions/4540282/using-mouse-with-sendinput-in-c
					input.type = INPUT_MOUSE;
					input.mi.mouseData=0;
					input.mi.dx = x*(65536.0f/GetSystemMetrics(SM_CXSCREEN));//x being coord in pixels
					input.mi.dy = y*(65536.0f/GetSystemMetrics(SM_CYSCREEN));//y being coord in pixels
					input.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE | MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP;
					SendInput(1,&input,sizeof(input));

				} /* end "If eye blinking value has been updated..." */



			} /* end "If a Packet of data was read..." */

			// Posted by Friedemann Wolpert on July 04, 2012 @ 11:45 PM
			// Regarding your 100% CPU Problem
			// http://support.neurosky.com/discussions/mindwave/916-can-the-neurosky-mindwave-be-used-to-emulate-mouse-clicks-for-somebody-using-computers-all-day-long#comment_17102761
			if( errCode == 0 ) {
				printf( "z");
				Sleep(200);   // use #include Windows.h  
			}


		} /* end "Read 10 Packets of data from connection..." */

		/* Clean up */
		TG_FreeConnection( connectionId );

		/* End program */
		wait();
		return( EXIT_SUCCESS );
}
/**
 * Program which prints ThinkGear Raw Wave Values to stdout.
 */
int
main( void ) {

    char *comPortName  = NULL;
    int   dllVersion   = 0;
    int   connectionId = 0;
    int   packetsRead  = 0;
    int   errCode      = 0;

    double secondsToRun = 0;
    time_t startTime    = 0;
    time_t currTime     = 0;
    char  *currTimeStr  = NULL;

    /* Print driver version number */
    dllVersion = TG_GetDriverVersion();
    printf( "ThinkGear DLL version: %d\n", dllVersion );

    /* Get a connection ID handle to ThinkGear */
    connectionId = TG_GetNewConnectionId();
    if( connectionId < 0 ) {
        fprintf( stderr, "ERROR: TG_GetNewConnectionId() returned %d.\n", 
                 connectionId );
        wait();
        exit( EXIT_FAILURE );
    }

    /* Set/open stream (raw bytes) log file for connection */
    errCode = TG_SetStreamLog( connectionId, "streamLog.txt" );
    if( errCode < 0 ) {
        fprintf( stderr, "ERROR: TG_SetStreamLog() returned %d.\n", errCode );
        wait();
        exit( EXIT_FAILURE );
    }

    /* Set/open data (ThinkGear values) log file for connection */
    errCode = TG_SetDataLog( connectionId, "dataLog.txt" );
    if( errCode < 0 ) {
        fprintf( stderr, "ERROR: TG_SetDataLog() returned %d.\n", errCode );
        wait();
        exit( EXIT_FAILURE );
    }

    /* Attempt to connect the connection ID handle to serial port "COM5" */
    /* NOTE: On Windows, COM10 and higher must be preceded by \\.\, as in
     *       "\\\\.\\COM12" (must escape backslashes in strings).  COM9
     *       and lower do not require the \\.\, but are allowed to include
     *       them.  On Mac OS X, COM ports are named like 
     *       "/dev/tty.MindSet-DevB-1".
     */
    comPortName = "\\\\.\\COM5";
    errCode = TG_Connect( connectionId, 
                          comPortName, 
                          TG_BAUD_57600, 
                          TG_STREAM_PACKETS );
    if( errCode < 0 ) {
        fprintf( stderr, "ERROR: TG_Connect() returned %d.\n", errCode );
        wait();
        exit( EXIT_FAILURE );
    }

    /* Keep reading ThinkGear Packets from the connection for 5 seconds... */
    secondsToRun = 5;
    startTime = time( NULL );
    while( difftime(time(NULL), startTime) < secondsToRun ) {

        /* Read all currently available Packets, one at a time... */
        do {

            /* Read a single Packet from the connection */
            packetsRead = TG_ReadPackets( connectionId, 1 );

            /* If TG_ReadPackets() was able to read a Packet of data... */
            if( packetsRead == 1 ) {

                /* If the Packet containted a new raw wave value... */
                if( TG_GetValueStatus(connectionId, TG_DATA_RAW) != 0 ) {

                    /* Get the current time as a string */
                    currTime = time( NULL );
        			currTimeStr = ctime( &currTime );

                    /* Get and print out the new raw value */
                    fprintf( stdout, "%s: raw: %d\n", currTimeStr,
                             (int)TG_GetValue(connectionId, TG_DATA_RAW) );
                    fflush( stdout );

                } /* end "If Packet contained a raw wave value..." */

            } /* end "If TG_ReadPackets() was able to read a Packet..." */

        } while( packetsRead > 0 ); /* Keep looping until all Packets read */

    } /* end "Keep reading ThinkGear Packets for 5 seconds..." */

    /* Clean up */
    TG_FreeConnection( connectionId );

    /* End program */
    wait();
    return( EXIT_SUCCESS );
}
Exemplo n.º 9
0
/**
 * Program which prints ThinkGear Raw Wave Values to stdout.
 */
int
main( void ) {
    
    char *comPortName  = NULL;
    int   dllVersion   = 0;
    int   connectionId = 0;
    int   packetsRead  = 0;
    int   errCode      = 0;
    
    double secondsToRun = 0;
    time_t startTime    = 0;
    time_t currTime     = 0;
    char  *currTimeStr  = NULL;

	FILE* attention;
	FILE* bufferState=fopen("state.txt","w+");
	FILE* raw;
	FILE* alpha1;
	FILE* alpha2;
	FILE* beta1;
	FILE* beta2;
	FILE* delta;
	FILE* theta;
	FILE* gamma1;
	FILE* gamma2;
    
    /* Print driver version number */
    dllVersion = TG_GetDriverVersion();
    printf( "ThinkGear DLL version: %d\n", dllVersion );
	fwrite("1",sizeof(char),1,bufferState);
	fclose(bufferState);
    /* Get a connection ID handle to ThinkGear */
    connectionId = TG_GetNewConnectionId();
    if( connectionId < 0 ) {
        fprintf( stderr, "ERROR: TG_GetNewConnectionId() returned %d.\n",
                connectionId );
        wait2();
        exit( EXIT_FAILURE );
    }
    
    /* Set/open stream (raw bytes) log file for connection */
    errCode = TG_SetStreamLog( connectionId, "streamLog.txt" );
    if( errCode < 0 ) {
        fprintf( stderr, "ERROR: TG_SetStreamLog() returned %d.\n", errCode );
       wait2();
        exit( EXIT_FAILURE );
    }
    
    /* Set/open data (ThinkGear values) log file for connection */
    errCode = TG_SetDataLog( connectionId, "dataLog.txt" );
    if( errCode < 0 ) {
        fprintf( stderr, "ERROR: TG_SetDataLog() returned %d.\n", errCode );
        wait2();
        exit( EXIT_FAILURE );
    }
    
    /* Attempt to connect the connection ID handle to serial port "COM5" */
    /* NOTE: On Windows, COM10 and higher must be preceded by \\.\, as in
     *       "\\\\.\\COM12" (must escape backslashes in strings).  COM9
     *       and lower do not require the \\.\, but are allowed to include
     *       them.  On Mac OS X, COM ports are named like
     *       "/dev/tty.MindSet-DevB-1".
     */
    comPortName = "\\\\.\\COM3";
    errCode = TG_Connect( connectionId,
                         comPortName,
                         TG_BAUD_57600,
                         TG_STREAM_PACKETS );
    if( errCode < 0 ) {
        fprintf( stderr, "ERROR: TG_Connect() returned %d.\n", errCode );
        wait2();
        exit( EXIT_FAILURE );
    }
   

    /* Keep reading ThinkGear Packets from the connection for 5 seconds... */
    secondsToRun = 5;
    startTime = time( NULL );
	while(1){
		int i=0;
		attention=fopen("attention.txt","w+");
		raw=fopen("raw.txt","w+");
        alpha1=fopen("alpha1.txt","w+");
		alpha2=fopen("alpha2.txt","w+");
		beta1=fopen("beta1.txt","w+");
		beta2=fopen("beta2.txt","w+");
		delta=fopen("delta.txt","w+");
		theta=fopen("theta.txt","w+");
		gamma1=fopen("gamma1.txt","w+");
		gamma2=fopen("gamma2.txt","w+");
		
    while(i<50){// difftime(time(NULL), startTime) < secondsToRun ) {
		
        /* Read all currently available Packets, one at a time... */ 
        do {
            //printf("%d\n",i);
            /* Read a single Packet from the connection */
            packetsRead = TG_ReadPackets( connectionId, 1 );
            
            /* If TG_ReadPackets() was able to read a Packet of data... */
            if( packetsRead == 1 ) {
                
                /* If the Packet containted a new raw wave value... */
                if( TG_GetValueStatus(connectionId, TG_DATA_RAW) != 0 ) {
                    
                    /* Get the current time as a string */
                    currTime = time( NULL );
        			currTimeStr = ctime( &currTime );
					//int check;
					//check= (int)TG_GetValue(connectionId,TG_DATA_POOR_SIGNAL);
					if(TG_GetValue(connectionId,TG_DATA_POOR_SIGNAL)> 90)
					{
						printf("%d\n",(int)TG_GetValue(connectionId,TG_DATA_POOR_SIGNAL));
						fprintf(raw,FORMAT,0);
						fprintf(alpha1,FORMAT,0);
						fprintf(alpha2,FORMAT,0);
						fprintf(beta1,FORMAT,0);
						fprintf(beta2,FORMAT,0);
						fprintf(delta,FORMAT,0);
						fprintf(gamma1,FORMAT,0);
						fprintf(gamma2,FORMAT,0);
						fprintf(theta,FORMAT,0);
						fprintf(attention,FORMAT,0);
					}
					else
					{
						fprintf(raw,FORMAT,TG_GetValue(connectionId,TG_DATA_RAW));
						fprintf(alpha1,FORMAT,(int)TG_GetValue(connectionId,TG_DATA_ALPHA1));
						fprintf(alpha2,FORMAT,(int)TG_GetValue(connectionId,TG_DATA_ALPHA2));
						fprintf(beta1,FORMAT,(int)TG_GetValue(connectionId,TG_DATA_BETA1));
						fprintf(beta2,FORMAT,(int)TG_GetValue(connectionId,TG_DATA_BETA2));
						fprintf(delta,FORMAT,(int)TG_GetValue(connectionId,TG_DATA_DELTA));
						fprintf(gamma1,FORMAT,(int)TG_GetValue(connectionId,TG_DATA_GAMMA1));
						fprintf(gamma2,FORMAT,(int)TG_GetValue(connectionId,TG_DATA_GAMMA2));
						fprintf(theta,FORMAT,(int)TG_GetValue(connectionId,TG_DATA_THETA));
						fprintf(attention,FORMAT,(int)TG_GetValue(connectionId,TG_DATA_ATTENTION));
					}
					//printf(stdout,FORMAT,TG_GetValue(connectionId,TG_DATA_THETA));
                    /* Get and print out the new raw value */
                    //fprintf( stdout, "%s: raw: %d\n", currTimeStr,
                          //  (int)TG_GetValue(connectionId, TG_DATA_RAW) );
                    //fflush( stdout );
                      i++;
                } /* end "If Packet contained a raw wave value..." */
                
            } /* end "If TG_ReadPackets() was able to read a Packet..." */
          
        } while( packetsRead > 0 ); /* Keep looping until all Packets read */
    } /* end "Keep reading ThinkGear Packets for 5 seconds..." */
	fclose(raw);
	fclose(alpha1);
	fclose(alpha2);
	fclose(beta1);
	fclose(beta2);
	fclose(delta);
	fclose(theta);
	fclose(gamma1);
	fclose(gamma2);
	fclose(attention);
	bufferState=fopen("state.txt","w");
	fwrite("0",sizeof(char),1,bufferState);
	fclose(bufferState);
	wait();
	//printf("check\n");
	//fflush(stdout);
	/*bufferState=fopen("state.txt","w+");
	fprintf(bufferState,"%c","0");
	fclose(bufferState);*/
	}
    /* Clean up */
    TG_FreeConnection( connectionId );
    
    /* End program */
    //wait();
    return( EXIT_SUCCESS );
}