2010-04-26 2 views
0

Je me suis dit que je devais utiliser un fichier MEX pour sortir des impulsions numériques en boucle (40 kHz) de Matlab vers ma carte DAQ, j'ai quelques API de le vendeur DAQ-Board, mais je ne sais vraiment pas si elles sont utiles. C'est une grande documentation sur le site de Mathworks à propos du MEX-File et des APIs, ça me rend confus. Donc je demande ici si quelqu'un peut m'orienter ou me montrer un exemple de code pour le réaliser !!Fichier MEX pour envoyer une impulsion en boucle à une carte DAQ

Répondre

0

J'ai écrit un petit paquet winsock en utilisant les fonctions de mex il ya quelque temps parce que le truc de Matlab tcpip avait des problèmes avec l'envoi de grandes quantités de données (comme des images). Je ne sais pas grand-chose sur les fonctions de mex autres que ce que j'ai appris pour faire fonctionner ce paquet, et même c'était il y a un certain temps. Mais, voici quelques-unes de mes notes d'avant et l'une des fonctions que j'ai écrites comme un exemple qui, je l'espère, pourrait être une aide pour vous. Avant d'écrire toutes les fonctions de mex, vous devez configurer Matlab pour pouvoir les compiler. Vous le faites en tapant "mex -setup" dans la ligne de commande matlab et en suivant les instructions qu'il donne. Je l'ai configuré pour utiliser le compilateur Visual Studio (notez que Visual Studio doit être installé pour que cette option apparaisse). Après avoir configuré le compilateur, vous compilez vos fonctions mex en tapant "mex filename.cpp" dans la ligne de commande Matlab. Cela produit un fichier .mexw32 (en supposant 32 bits) que Matlab utilise lorsque vous appelez votre fonction mex.

Pour écrire la fonction mex elle-même, vous écrivez un fichier m pour le déclarer et fournir des commentaires ainsi qu'un fichier cpp avec l'implémentation réelle.

À titre d'exemple, voici l'un des fichiers de m-je écrit:

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. 

Et voici le fichier cpp correspondant. Notez que je viens de créer mon propre format de message et que le code C# correspondant l'a analysé en dehors du flux d'octets.

// 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; 
    } 
} 
Questions connexes