簡體   English   中英

MEX文件可在循環中將脈沖輸出到DAQ板

[英]MEX-File to Output Pulse in a Loop to a DAQ-Board

我以為我必須使用MEX文件在Matlab到DAQ板的循環(40 kHz)中輸出數字脈沖,我從DAQ板供應商那里獲得了一些API,但是我真的不知道它們是否有用。 這是Mathworks網站上有關MEX文件和API的重要文檔,這讓我感到困惑。 因此,我在這里問是否有人可以讓我適應方向或向我展示示例代碼來實現這一目標!

我前一段時間使用mex函數編寫了一個小的winsock程序包,因為Matlab的tcpip東西在發送大量數據(例如圖像)時遇到問題。 除了我了解到如何使該程序包正常工作之外,我對mex函數了解不多,即使是很久以前。 但是,這是我之前的一些筆記,以及我作為示例編寫的功能之一,希望對您有所幫助。

在編寫任何混合功能之前,您需要配置Matlab使其能夠進行編譯。 您可以通過在matlab命令行中輸入“ mex -setup”並按照其說明進行操作。 我將其配置為使用Visual Studio編譯器(請注意,必須安裝Visual Studio才能顯示此選項)。

配置編譯器后,您可以通過在Matlab命令行中鍵入“ mex filename.cpp”來編譯mex函數。 這將生成一個.mexw32文件(假定為32位),當您調用mex函數時,Matlab將使用該文件。

要編寫mex函數本身,您可以編寫一個m文件來聲明它並提供注釋以及帶有實際實現的cpp文件。

例如,這是我編寫的m文件之一:

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文件。 請注意,我只是編寫了自己的消息格式,並具有將其解析出字節流的相應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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM