2010-04-26 2 views
0

Matlab에서 DAQ 보드로 루프 (40 kHz)로 디지털 펄스를 출력하려면 MEX 파일을 사용해야한다고 생각했습니다. DAQ 보드 공급 업체이지만 실제로 유용한 지 잘 모릅니다. Mathworks 웹 사이트에서 MEX-File 및 API에 대한 큰 문서가 나와 혼란 스럽습니다. 누군가가 나에게 동쪽으로 향하게 할 수 있는지 또는 나에게 예제 코드를 보여줄 수 있는지 여기에서 묻습니다.루프에서 DAQ 보드에 출력 펄스를 출력하는 MEX 파일

답변

0

Matlab의 tcpip 항목이 이미지와 같이 많은 양의 데이터를 보내는데 문제가있어서 mex 함수를 사용하여 작은 winsock 패키지를 작성했습니다. 나는 그 패키지를 작동시키는 법을 배웠던 것 외에 mex 기능에 대해 많이 모른다. 그리고 심지어 꽤 오래 전이었다. 그러나 여기에 필자가 몇 가지 도움을 줄 수있는 예로서 필자가 작성한 기능 중 하나와 이전의 내 노트 중 일부가 있습니다.

mex 함수를 작성하기 전에 Matlab을 컴파일하도록 구성해야합니다. 이 작업은 matlab 명령 줄에 "mex -setup"을 입력하고 지시 사항에 따라 수행하십시오. Visual Studio 컴파일러를 사용하도록 구성했습니다 (이 옵션을 표시하려면 Visual Studio가 설치되어 있어야합니다).

컴파일러를 구성한 후 Matlab 명령 줄에 "mex filename.cpp"를 입력하여 mex 함수를 컴파일합니다. 이렇게하면 mex 함수를 호출 할 때 Matlab에서 사용하는 .mexw32 파일 (32 비트라고 가정)이 생성됩니다.

mex 함수 자체를 작성하려면 m- 파일을 작성하여이를 선언하고 실제 구현과 함께 cpp 파일을 주석으로 제공하십시오.

function sendColorImage(socketHandle, colorImage) %#ok<*INUSD> 
%SENDCOLORIMAGE Sends a color image over the given socket 
% This function sends a color image over the socket provided. The image 
% is really just an MxNx3 matrix. Note that this function sends the 
% image data in the order in which Matlab stores it (non-interlaced 
% column major order), which is different from most other languages. 
% This means the red values for every pixel will be sent first, then the 
% green values, then the blue values. Furthermore, the scanlines read 
% from the top of the image to the bottom, starting at the left side of 
% the image. 
% 
% socketHande - A handle to the socket over which the image should be 
% sent. This handle is returned by the openSocket function when the 
% socket is first created. 
% 
% colorImage - An MxNx3 matrix containing the image data. This matrix 
% should be in the same format as a matrix loaded using Matlabs imread 
% function. 
% 
% This is a mex function and is defined in its corresponding .cpp file. 

을 그리고 여기에 해당하는 CPP 파일입니다 :

예를 들어, 여기에 내가 쓴 m-파일 중 하나입니다. 방금 내 자신의 메시지 형식을 구성하고 해당 바이트 스트림에서 다시 파싱 한 해당 C# 코드가 있음에 유의하십시오.

// Instruct the compiler to link with wsock32.lib (in case it isn't specified on the command line) 
#pragma comment(lib,"wsock32.lib") 

#include "mex.h" 
#include <winsock2.h> 
#include <cstdio> 
#include "protocol.h" 

// See the corresponding .m file for documentation on this mex function. 
void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]){ 

    char errorMessage[100]; 

    // Validate input and output arguments 
    if(nlhs != 0) 
     mexErrMsgTxt("There are no output arguments for this function."); 
    if(nrhs != 2) 
     mexErrMsgTxt("Must have 2 input parameters: the socket handle and the MxNx3 image matrix"); 
    if(!mxIsClass(prhs[0], "uint32")) 
     mexErrMsgTxt("The first input parameter should be a uint32 containing the socket handle"); 
    if(!mxIsClass(prhs[1], "uint8") || mxGetNumberOfDimensions(prhs[1]) != 3 || mxGetDimensions(prhs[1])[2] != 3) 
     mexErrMsgTxt("The 2nd input parameter should be an MxNx3 uint8 matrix containing the image"); 

    // Get the socket handle 
    SOCKET socketHandle = (int)(mxGetPr(prhs[0])[0]); 

    // Set up the header 
    int frameWidth = mxGetDimensions(prhs[1])[1]; 
    int frameHeight = mxGetDimensions(prhs[1])[0]; 
    int header[3]; 
    header[0] = COLOR_IMAGE; 
    header[1] = frameWidth; 
    header[2] = frameHeight; 

    // Send the header 
    int bytesSent; 
    int totalBytesSent = 0; 
    while(totalBytesSent < 3*sizeof(int)){ 
     bytesSent = send(socketHandle, ((char*)header) + totalBytesSent, 3*sizeof(int) - totalBytesSent, 0); 
     if(bytesSent == SOCKET_ERROR){ 
      sprintf(errorMessage, "Error sending image header over the socket: %d", WSAGetLastError()); 
      mexErrMsgTxt(errorMessage); 
     } 
     totalBytesSent += bytesSent; 
    } 

    // Send the image 
    totalBytesSent = 0; 
    int totalBytesToSend = frameWidth * frameHeight * 3; 
    char* dataPointer = (char*)mxGetData(prhs[1]); 
    while(totalBytesSent < totalBytesToSend){ 
     bytesSent = send(socketHandle, dataPointer + totalBytesSent, totalBytesToSend - totalBytesSent, 0); 
     if(bytesSent == SOCKET_ERROR){ 
      sprintf(errorMessage, "Error sending image over the socket: %d", WSAGetLastError()); 
      mexErrMsgTxt(errorMessage); 
     } 
     totalBytesSent += bytesSent; 
    } 
}