Why does TG_Connect() return -2

I am currently attempting to connect my NeuroSky Mindwave Mobile 2 to matlab, via the readRaw function provided by neurosky. It works ~1 in 20 times and most of the time returns a value of -2, meaning there were no bytes available to read from the headset. I have tried resetting the COM port, but to no avail.
function readRAW
function readRAW
%run this function to connect and plot raw EEG data
%make sure to change portnum1 to the appropriate COM port
clear all
close all
data = zeros(1,256); %preallocate buffer
portnum1 = 3; %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_RAW = 4;
%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 ) );
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 ) );
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 ) );
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 ) );
end
fprintf( 'Connected. Reading Packets...\n' );
%%
%record data
j = 0;
i = 0;
while (i < 10240) %loop for 20 seconds
if (calllib('thinkgear64','TG_ReadPackets',connectionId1,1) == 1) %if a packet was read...
if (calllib('thinkgear64','TG_GetValueStatus',connectionId1,TG_DATA_RAW) ~= 0) %if RAW has been updated
j = j + 1;
i = i + 1;
data(j) = calllib('thinkgear64','TG_GetValue',connectionId1,TG_DATA_RAW);
end
end
if (j == 256)
plotRAW(data); %plot the data, update every .5 seconds (256 points)
j = 0;
end
end
%disconnect
calllib('thinkgear64', 'TG_FreeConnection', connectionId1 );
Error Message:
Error using readRAW (line 57)
ERROR: TG_Connect() returned -2.

2 commentaires

Amer Basem
Amer Basem le 4 Juin 2021
Hello sir I'm facing the same problem Did you find the solution? Help me please Thank you
Walter Roberson
Walter Roberson le 4 Juin 2021
Error -2 is access unavailable. Some of the potential causes are listed below in https://www.mathworks.com/matlabcentral/answers/765506-why-does-tg_connect-return-2#answer_641371

Connectez-vous pour commenter.

Réponses (1)

Walter Roberson
Walter Roberson le 7 Mar 2021
Modifié(e) : Walter Roberson le 8 Mar 2021

0 votes

No, -2 indicates that the device could not be connected to:
  • the device might be in use by a different program
  • the device might be in use from a previous run where the program stopped before closing
  • sometimes drivers get stuck
  • the device might not be turned on
  • cable might be unplugged
  • cable might be faulty

2 commentaires

Hi thanks for your answer. So my suspicion is that the program crashes without closing the serial port properly.
I've tried running this script to close the ports:
clear
clc
instrfind;
delete(instrfind);
clear
clc
But it hasn't worked. I'm a bit stuck for ideas now.
Again, thanks a lot for your answer
delete(instrfind('type', 'serial'))
would be more focused, but if what you had did not work then this will not work either.

Connectez-vous pour commenter.

Catégories

En savoir plus sur MATLAB dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by