Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab-一个串口的回调函数,不知道引用_Matlab - Fatal编程技术网

Matlab-一个串口的回调函数,不知道引用

Matlab-一个串口的回调函数,不知道引用,matlab,Matlab,您好,我正在使用thinkgear的库通过蓝牙连接mindwave和matlab,但我需要建立串行端口的回调函数,但我没有该端口的参考。我可以这样做吗?我不想做任何类似代码的循环:这就是代码 portnum1 = 5; %COM Port # comPortName1 = sprintf('\\\\.\\COM%d', portnum1); % Baud rate for use with TG_Connect() and TG_SetBaudrate(). TG_BAUD_57600

您好,我正在使用thinkgear的库通过蓝牙连接mindwave和matlab,但我需要建立串行端口的回调函数,但我没有该端口的参考。我可以这样做吗?我不想做任何类似代码的循环:这就是代码

portnum1 = 5;   %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');
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 < 102400)   %loop for 20 seconds
    if (calllib('Thinkgear','TG_ReadPackets',connectionId1,1) == 1)   %if a packet was read...

        if (calllib('Thinkgear','TG_GetValueStatus',connectionId1,TG_DATA_RAW) ~= 0)   %if RAW has been updated 
            j = j + 1;
            i = i + 1;
            data(j) = calllib('Thinkgear','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('Thinkgear', 'TG_FreeConnection', connectionId1 );
portnum1=5;%COM端口#
comPortName1=sprintf('\\.\\COM%d',portnum1);
%用于TG_Connect()和TG_SetBaudrate()的波特率。
TG_BAUD_57600=57600;
%用于TG_Connect()和TG_SetDataFormat()的数据格式。
TG_流_包=0;
%可以从TG_GetValue()请求的数据类型。
TG_DATA_RAW=4;
%加载thinkgear动态链接库
loadlibrary('Thinkgear.dll');
fprintf('Thinkgear.dll已加载\n');
%获取dll版本
dllVersion=calllib('Thinkgear','TG_GetDriverVersion');
fprintf('ThinkGear DLL版本:%d\n',dllVersion);
%%
%获取到ThinkGear的连接ID句柄
connectionId=calllib('Thinkgear','TG_GetNewConnectionId');
如果(连接1<0)
错误(sprintf('error:TG_GetNewConnectionId()返回%d.\n',ConnectionId));
结束;
%设置/打开连接的流(原始字节)日志文件
errCode=calllib('Thinkgear','TG_SetStreamLog',connectionId1',streamLog.txt');
如果(错误代码<0)
错误(sprintf('error:TG_SetStreamLog()返回%d.\n',errCode));
结束;
%为连接设置/打开数据(ThinkGear值)日志文件
errCode=calllib('Thinkgear','TG_SetDataLog',connectionId1',dataLog.txt');
如果(错误代码<0)
错误(sprintf('error:TG_SetDataLog()返回%d.\n',errCode));
结束;
%尝试将连接ID句柄连接到串行端口“COM3”
errCode=calllib('Thinkgear','TG_Connect',connectionId1,comPortName1,TG_BAUD_57600,TG_STREAM_数据包);
如果(错误代码<0)
错误(sprintf('error:TG_Connect()返回%d.\n',errCode));
结束
fprintf('Connected.Reading Packets…\n');
%%
%记录数据
j=0;
i=0;
而(i<102400)%20秒循环
如果(calllib('Thinkgear','TG_ReadPackets',connectionId1,1)==1)%if(如果数据包被读取。。。
if(calllib('Thinkgear','TG\u GetValueStatus',connectionId1,TG\u DATA\u RAW)~=0)%if RAW已更新
j=j+1;
i=i+1;
数据(j)=calllib('Thinkgear','TG_GetValue',connectionId1,TG_data_RAW);
结束
结束
如果(j==256)
plotRAW(数据);%绘制数据,每0.5秒更新一次(256点)
j=0;
结束
结束
%断开
calllib('Thinkgear','TG_FreeConnection',Connection1);

您所说的“您没有参考资料”是什么意思。您不知道正在使用哪个端口?我知道它的端口5,但通过Matlab回调,我需要SerialPort变量