i am using mindwave mobile , have problem to data acquition in matlab

16 views (last 30 days)
in loadlibrary at 181 in savecomponenttocsv at 32
thinkgear.dll loaded
thinkgear dll version: 22
error using savecomponenttocsv (line 64) error: tg_connect() returned -2.
used code
function SaveComponentToCsv
clear all
close all
data = ones(61440,14);
portnum1 = 1; %COM Port #
comPortName1 = sprintf('\\\\.\\COM%d', portnum1);
% Baud rate for use with TG_Connect() and TG_SetBaudrate().
TG_BAUD_57600 = 57600;
% Data format for use with TG_Connect() and TG_SetDataFormat().
TG_STREAM_PACKETS = 0;
% Data type that can be requested from TG_GetValue().
TG_DATA_POOR_SIGNAL = 1;
TG_DATA_ATTENTION = 2;
TG_DATA_MEDITATION = 3;
TG_DATA_RAW = 4;
TG_DATA_DELTA = 5;
TG_DATA_THETA = 6;
TG_DATA_ALPHA1 = 7;
TG_DATA_ALPHA2 = 8;
TG_DATA_BETA1 = 9;
TG_DATA_BETA2 = 10;
TG_DATA_GAMMA1 = 11;
TG_DATA_GAMMA2 = 12;
TG_DATA_BLINK_STRENGTH = 37;
%load thinkgear dll
loadlibrary('Thinkgear.dll','thinkgear.h');
fprintf('Thinkgear.dll loaded\n');
%get dll version
dllVersion = calllib('Thinkgear', 'TG_GetDriverVersion');
fprintf('ThinkGear DLL version: %d\n', dllVersion );
Get a connection ID handle to ThinkGear
connectionId1 = calllib('Thinkgear', 'TG_GetNewConnectionId');
if ( connectionId1 < 0 )
error( sprintf( 'ERROR: TG_GetNewConnectionId() returned %d.\n', connectionId1 ) );
calllib('Thinkgear', 'TG_FreeConnection', connectionId1 );
end;
% Set/open stream (raw bytes) log file for connection
errCode = calllib('Thinkgear', 'TG_SetStreamLog', connectionId1, 'streamLog.txt' );
if( errCode < 0 )
error( sprintf( 'ERROR: TG_SetStreamLog() returned %d.\n', errCode ) );
calllib('Thinkgear', 'TG_FreeConnection', connectionId1 );
end;
% Set/open data (ThinkGear values) log file for connection
errCode = calllib('Thinkgear', 'TG_SetDataLog', connectionId1, 'dataLog.txt' );
if( errCode < 0 )
error( sprintf( 'ERROR: TG_SetDataLog() returned %d.\n', errCode ) );
calllib('Thinkgear', 'TG_FreeConnection', connectionId1 );
end;
% Attempt to connect the connection ID handle to serial port "COM3"
errCode = calllib('Thinkgear', 'TG_Connect', connectionId1,comPortName1,TG_BAUD_57600,TG_STREAM_PACKETS );
if ( errCode < 0 )
error( sprintf( 'ERROR: TG_Connect() returned %d.\n', errCode ) );
calllib('Thinkgear', 'TG_FreeConnection', connectionId1 );
end
fprintf( 'Connected. Reading Packets...\n' );
record data and save to csv file
%i = 1;
%j = 1;
%recording data
for i=1:61440 %loop for 120 seconds
if (calllib('Thinkgear','TG_ReadPackets',connectionId1,1) == 1) %if a packet was read...
for j=1:14
if(j == 1)
data(i,j) = now;
end
if(j == 2)
if (calllib('Thinkgear','TG_GetValueStatus',connectionId1,TG_DATA_POOR_SIGNAL) ~= 0) %if RAW has been updated
data(i,j) = calllib('Thinkgear','TG_GetValue',connectionId1,TG_DATA_POOR_SIGNAL);
end
end
if(j == 3)
if (calllib('Thinkgear','TG_GetValueStatus',connectionId1,TG_DATA_ATTENTION) ~= 0)
data(i,j) = calllib('Thinkgear','TG_GetValue',connectionId1,TG_DATA_ATTENTION);
end
end
if(j == 4)
if (calllib('Thinkgear','TG_GetValueStatus',connectionId1,TG_DATA_MEDITATION) ~= 0)
data(i,j) = calllib('Thinkgear','TG_GetValue',connectionId1,TG_DATA_MEDITATION);
end
end
if(j == 5)
if (calllib('Thinkgear','TG_GetValueStatus',connectionId1,TG_DATA_RAW) ~= 0)
data(i,j) = calllib('Thinkgear','TG_GetValue',connectionId1,TG_DATA_RAW);
end
end
if(j == 6)
if (calllib('Thinkgear','TG_GetValueStatus',connectionId1,TG_DATA_DELTA) ~= 0)
data(i,j) = calllib('Thinkgear','TG_GetValue',connectionId1,TG_DATA_DELTA);
end
end
if(j == 7)
if (calllib('Thinkgear','TG_GetValueStatus',connectionId1,TG_DATA_THETA) ~= 0)
data(i,j) = calllib('Thinkgear','TG_GetValue',connectionId1,TG_DATA_THETA);
end
end
if(j == 8)
if (calllib('Thinkgear','TG_GetValueStatus',connectionId1,TG_DATA_ALPHA1) ~= 0)
data(i,j) = calllib('Thinkgear','TG_GetValue',connectionId1,TG_DATA_ALPHA1);
end
end
if(j == 9)
if (calllib('Thinkgear','TG_GetValueStatus',connectionId1,TG_DATA_ALPHA2) ~= 0)
data(i,j) = calllib('Thinkgear','TG_GetValue',connectionId1,TG_DATA_ALPHA2);
end
end
if(j == 10)
if (calllib('Thinkgear','TG_GetValueStatus',connectionId1,TG_DATA_BETA1) ~= 0)
data(i,j) = calllib('Thinkgear','TG_GetValue',connectionId1,TG_DATA_BETA1);
end
end
if(j == 11)
if (calllib('Thinkgear','TG_GetValueStatus',connectionId1,TG_DATA_BETA2) ~= 0)
data(i,j) = calllib('Thinkgear','TG_GetValue',connectionId1,TG_DATA_BETA2);
end
end
if(j == 12)
if (calllib('Thinkgear','TG_GetValueStatus',connectionId1,TG_DATA_GAMMA1) ~= 0)
data(i,j) = calllib('Thinkgear','TG_GetValue',connectionId1,TG_DATA_GAMMA1);
end
end
if(j == 13)
if (calllib('Thinkgear','TG_GetValueStatus',connectionId1,TG_DATA_GAMMA2) ~= 0)
data(i,j) = calllib('Thinkgear','TG_GetValue',connectionId1,TG_DATA_GAMMA2);
end
end
if(j == 14)
if (calllib('Thinkgear','TG_GetValueStatus',connectionId1,TG_DATA_BLINK_STRENGTH) ~= 0)
data(i,j) = calllib('Thinkgear','TG_GetValue',connectionId1,TG_DATA_BLINK_STRENGTH);
end
end
end
end
end
%save data to csv file
csvwrite('G:\matlab_function\data.csv',data);
dlmwrite('G:\matlab_function\data.csv',data,'precision', '%.6f');
disconnect
calllib('Thinkgear', 'TG_FreeConnection', connectionId1 );

Answers (1)

Walter Roberson
Walter Roberson on 6 Apr 2016
* @return -2 if there were not even any bytes available to be read from
* the Connection's serial communication stream.
Possibly you have the wrong COM port?
Also the code I see people using would correspond to
comPortName1 = sprintf('COM%d', portnum1);
and it is possible that the \\.\COM* form is leading to the problem.
  2 Comments
Mohamed farhat
Mohamed farhat on 7 Apr 2017
Edited: Walter Roberson on 19 Aug 2020
enter this link https://store.neurosky.com/products/pc-developer-tools press add to cart button and complete information of shipping then you get link to download all SDK files for free. after downloading you will find thinkgear.dll and thinkgear.h with two versions x86 bit and x64 bit. for newer version of matlab like 2016 and 2017 you should use x64 bit lib.
for thinkgear64.dll you can rename it to thinkgear.dll to load thinkgear.h correctly.
you will find that function TG_GetDriverVersion changed to TG_GetVersion.
loadlibrary('thinkgear');
Dllversion=calllib('thinkgear','TG_GetVersion')
you get version 3.
goodluck.
Walter Roberson
Walter Roberson on 7 Apr 2017
Mohamed farhat,
Although that is generally useful to know, section 1.2 of the license agreement restricts use of the software to "Neurosky approved applications". The implication is that before you could even start experiments to determine whether you can talk to the hardware from MATLAB, you would have to write up some kind of proposal about what you are doing to do, and send it to Neurosky to get their approval. That is probably not an acceptable restriction.
The Github link I gave above is, I think, open source software, so there would be no need to get Neurosky to approve of the application.

Sign in to comment.

Tags

No tags entered yet.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!