How to pass binary values to mex

Hello, I want to transmit 1x12 binary data to mex file for data transmission
b=70.19
Y = round(b,1);
dec= Y*10;
integers=dec;
temp = integers;
mask = temp < 0;
temp(mask) = 2^12 + temp(mask) ;
a=decimalToBinaryVector(temp, 12);
The output binary values (a) is : 0 0 1 0 1 0 1 1 1 1 1 0
When I pass these values to mex file,
b= mex(a);
I get a different binary value. The output of a is:
1.18870000000000e-320 1.18870000000000e-320 1.00000000000053 1.18870000000000e-320 1.00000000000053 1.18870000000000e-320 1.00000000000053 1.00000000000053 1.00000000000053 1.00000000000053 1.00000000000053 1.18870000000000e-320
mex.c
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[] )
{
double *a;
int M,N;
double* bb; /* 1xN output matrix */
const mwSize *dims;
a=mxGetData(prhs[0]);
plhs[0] = mxCreateDoubleMatrix(M, N, mxREAL);
bb = mxGetPr(plhs[0]);
analogoutputMatlab(a,bb,N);
}
Why do I get change in binary values? How to pass binary values to mex?

14 件のコメント

James Tursa
James Tursa 2020 年 3 月 3 日
M and N are not set to values, they are garbage. Also, what is the function signature (types of arguments) for analogoutputMatlab( )?
A R
A R 2020 年 3 月 3 日
Hello James, M and N corresponds to rows and columns, I have 1x12 data, so M=1 and N=12.
/* get dimensions of the input matricies */
dims=mxGetDimensions(prhs[0]);
M=(int)dims[0];
N=(int)dims[1]; //m and n is 1x12
mexPrintf("m and n is %d %d",M,N);
The type of arguments for analogoutputMatlab() function is:
int analogoutputMatlab(double*data,double *bb,int N)
The 1x12 data is transmitted to the DAQ using the below command
for (i=0;i<N;i++)
{
// mexPrintf("\nThe output is %f",data[i]);
ULStat = cbAOut(BoardNum, Chan1, Range, data[i]);
ULStat = cbAIn(BoardNum, Chan, Range, &data[i]);
mexPrintf("\nThe output is %f",data[i]);
bb[i] = data[i];
}
I dont know why I get junk values
James Tursa
James Tursa 2020 年 3 月 3 日
It is hard to debug when we are only given code snippets. We don't have all of the function signature information, we don't have all the variable types, etc. So we end up guessing what might be the problem. Can you post complete code for us to see?
Walter Roberson
Walter Roberson 2020 年 3 月 4 日
I do not see any signature for mexfile()?
The decimalToBinaryVector function is not documented as returning any values, and especially the data type of what it returns is not documented. It probably returns double through (the output of the examples would look slightly different otherwise.)
It is not good to rely on the idea that a value rounded to the nearest 1/10, when multiplied by 10, would give you an exact integer. It does happen to be true between -100 and 100, but it is risky.
James Tursa
James Tursa 2020 年 3 月 4 日
Signatures of cbAOut( ) and cbAIn( )? Shouldn't you be including prototypes for these?
A R
A R 2020 年 3 月 4 日
編集済み: A R 2020 年 3 月 4 日
Hello Walter, The decimalToBinaryVector function gives 1x12 double.
bb= mexfile(a);
I am passing the binary value 'a' to a mex file. I transmit the binary value to analog output pin. After that I am getting the transmitted binary value in the analog input pin and reading it in MATLAB . So In the code above 'bb' is the received binary value from DAQ.
b=70.19
Y = round(b,1);
dec= Y*10;
integers=dec;
temp = integers;
mask = temp < 0;
temp(mask) = 2^12 + temp(mask) ;
a=decimalToBinaryVector(temp, 12); %it gives 1x12 double
The output binary values (a) is : 0 0 1 0 1 0 1 1 1 1 1 0
The above code gives the correct binary value. But when I pass these value to my mex file like below code, the output of binary value (a) gets changed.
b=70.19
Y = round(b,1);
dec= Y*10;
integers=dec;
temp = integers;
mask = temp < 0;
temp(mask) = 2^12 + temp(mask) ;
a=decimalToBinaryVector(temp, 12);
bb= mexfile(a);
I get a different binary value. The output of a is:
1.18870000000000e-320 1.18870000000000e-320 1.00000000000053 1.18870000000000e-320 1.00000000000053 1.18870000000000e-320 1.00000000000053 1.00000000000053 1.00000000000053 1.00000000000053 1.00000000000053 1.18870000000000e-320
Why my binary value ( a=decimalToBinaryVector(temp, 12);) is getting changed when I call mex file? I only call my mex file after getting the binary values. So how calling my mex file alters the output of my binary values?
A R
A R 2020 年 3 月 4 日
Hello James, No I didnt prototype for cbAOut( ) and cbAIn( ).
James Tursa
James Tursa 2020 年 3 月 4 日
So, if you don't have prototypes, how can I (or the compiler) check to see that your arguments are correct?
A R
A R 2020 年 3 月 4 日
James, I think I didnt get your questions correctly. cbAOut( ) and cbAIn( ) are inbuilt functions from universal library.
int BoardNum = 0;
int ULStat = NOERRORS;
int Chan, NumAIChans;
int Range = BIP5VOLTS;
int Options = FOREGROUND;
int ChannelType = ANALOGINPUT;
char BoardName[BOARDNAMELEN];
int ch1 = 0;
int Chan1, ExitFlag;
int NumAOChans;
int ChannelType1 = ANALOGOUTPUT;
int i;
ULStat = cbAOut(BoardNum, Chan1, Range, data[i]);
I have mentioned the data types of the variables used in cbAOut().
I can attach the example analog output C program from universal library.
/*ULAO01.C****************************************************************
File: ULAO01.C
Library Call Demonstrated: cbAOut()
Purpose: Writes to a D/A Output Channel.
Demonstration: Sends values to D/A 0.
Other Library Calls: cbErrHandling()
cbFromEngUnits()
Special Requirements: Board 0 must have a D/A output port.
Copyright (c) 1995-2020, Measurement Computing Corp.
All Rights Reserved.
***************************************************************************/
/* Include files */
#include "..\cbw.h"
#include "Utilities.h"
int main(void)
{
/* Variable Declarations */
int ch = 0;
int Row, Col;
int BoardNum = 0;
int ULStat = NOERRORS;
int Range = BIP5VOLTS;
int Chan, ExitFlag;
int NumAOChans, dummy;
WORD DataValue;
float EngUnits, voltsSent;
float RevLevel = (float)CURRENTREVNUM;
int DefaultTrig = TRIG_POS_EDGE;
int ChannelType = ANALOGOUTPUT;
char rangeName[RANGENAMELEN];
double rangeVolts, fullScalePlus, fullScaleMinus;
char BoardName[BOARDNAMELEN];
/* Declare UL Revision Level */
ULStat = cbDeclareRevision(&RevLevel);
// get the name of the board
if (!GetNameOfBoard(BoardNum, BoardName)) {
DisplayMessage(NOERRORS);
return 0;
}
/* set up the display screen */
ClearScreen();
printf("Demonstration of cbAOut() using %s.\n\n", BoardName);
// Determine if device is compatible with this example
NumAOChans = FindAnalogChansOfType(BoardNum, ChannelType, &ADRes, &Range, &Chan, &DefaultTrig);
if (NumAOChans == 0) {
printf("%s (board %u) does not have analog output channels.\n", BoardName, BoardNum);
DisplayMessage(NOERRORS);
return 0;
}
GetRangeInfo(Range, rangeName, &rangeVolts);
fullScalePlus = rangeVolts;
fullScaleMinus = 0;
if(Range < 99) {
fullScalePlus = rangeVolts / 2;
fullScaleMinus = fullScalePlus * -1;
}
GetTextCursor(&Col, &Row);
ExitFlag = FALSE;
Chan = 0;
printf("Current output range is %s.\n\n", rangeName);
while (!ExitFlag) {
MoveCursor(0, 2);
printf("Enter a voltage between %.3f and %.3f: ", fullScaleMinus, fullScalePlus);
MoveCursor(44, 2);
dummy = scanf("%f", &EngUnits);
ULStat = cbFromEngUnits(BoardNum, Range, EngUnits, &DataValue);
ULStat = cbAOut(BoardNum, Chan, Range, DataValue);
if (ULStat != NOERRORS) {
DisplayMessage(ULStat);
return 0;
}
ClearScreen();
voltsSent = EngUnits;
if (EngUnits > fullScalePlus) voltsSent = (float)fullScalePlus;
if (EngUnits < fullScaleMinus) voltsSent = (float)fullScaleMinus;
printf("\n %.3f volts has been sent to D/A 0.\n\n", voltsSent);
printf("Press Q to quit , any other key to continue:\n ");
while (!_kbhit()) {}
ch = _getch();
if (ch == 'q' || ch == 'Q')
ExitFlag = TRUE;
ClearScreen();
}
DisplayMessage(NOERRORS);
return 0;
}
Walter Roberson
Walter Roberson 2020 年 3 月 4 日
if(nrhs=0)
That = is an assignment in C, not a comparison.
A R
A R 2020 年 3 月 4 日
Yes Walter, Thanks for mentioning. I changed it.
if(nrhs!=1) {
mexErrMsgTxt("one input required.");
}
But still I get the junk values.
A R
A R 2020 年 3 月 5 日
Hi, Can anyone please help me with this problem. I dont know why I am getting junk values, Is there any other way to alter my mex file?
Walter Roberson
Walter Roberson 2020 年 3 月 5 日
To be honest, I am not enthusiastic about rebooting my computer into Windows, doing hours and hours of Windows updates, downloading the Measurement Computing development library, installing a compiler, putting together the pieces of what you posted to try to produce a project, and figuring out how to use a Windows debugger.
James would probably appreciate if you put together a zip of everything together and a link of the Measurement Computing development package.
My personal suspicion is that you could perhaps produce a smaller example that did not call upon the measurement computing routines. And if you were to find that the trimmed example does not produce the same problem, then you would have isolated down to an interaction with MC libraries, which would give you information about what to look for.
A R
A R 2020 年 3 月 5 日
Walter, yes I will try the example with session based interface.

サインインしてコメントする。

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeWrite C Functions Callable from MATLAB (MEX Files) についてさらに検索

タグ

質問済み:

A R
2020 年 3 月 3 日

コメント済み:

A R
2020 年 3 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by