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
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
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
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

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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

질문:

2021년 3월 7일

댓글:

2021년 6월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by