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
Amer Basem
2021년 6월 4일
Hello sir I'm facing the same problem Did you find the solution? Help me please Thank you
Walter Roberson
2021년 6월 4일
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
답변 (1개)
Walter Roberson
2021년 3월 7일
편집: Walter Roberson
2021년 3월 8일
0 개 추천
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
Ewan Harrison
2021년 3월 8일
Walter Roberson
2021년 3월 8일
delete(instrfind('type', 'serial'))
would be more focused, but if what you had did not work then this will not work either.
카테고리
도움말 센터 및 File Exchange에서 MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!