I am trying to configure my Measurement Computing 1208FS-Plus DAQ to send 8-bit triggers that are a binary representation of 0-255 (i.e., 1 =1 0 0 0 0 0 0 0). I'm using compiled mex files with functions from the Universal Library in MATLAB 2021b and have the Dat Acquisition Toolbox and add-on for Measurement Computing DAQs installed. The DAQ is set up and calibrated in Instacal in differential mode.
Here is the C code I am using:
#include "mex.h"
#include "cbw.h"
// Define the DAQ board number
#define BOARD_NUM 0
// Define the digital port
#define PORT_NUM FIRSTPORTA
// The entry point for the MEX function
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
if (nrhs != 1) {
mexErrMsgIdAndTxt("MATLAB:sendTTL:invalidNumInputs", "One input required.");
}
// Get the input value
if (!mxIsDouble(prhs[0]) || mxGetNumberOfElements(prhs[0]) != 1) {
mexErrMsgIdAndTxt("MATLAB:sendTTL:inputNotScalar", "Input must be a scalar.");
}
double inputValue = mxGetScalar(prhs[0]);
if (inputValue < 0 || inputValue > 255) {
mexErrMsgIdAndTxt("MATLAB:sendTTL:inputOutOfRange", "Input must be between 0 and 255.");
}
// Convert the input value to an integer
int value = (int)inputValue;
// Configure the digital port
int configResult = cbDConfigPort(BOARD_NUM, PORT_NUM, DIGITALOUT);
if (configResult != 0) {
char errorMessage[256];
cbGetErrMsg(configResult, errorMessage);
mexErrMsgIdAndTxt("MATLAB:sendTTL:cbDConfigPortFailed", "Failed to configure the prort. Error code: %d. Error message: %s", configResult, errorMessage);
}
// Send the TTL signal
int result = cbDOut(BOARD_NUM, PORT_NUM, value);
if (result != 0) {
char errorMessage[256];
cbGetErrMsg(result,errorMessage);
mexErrMsgIdAndTxt("MATLAB:sendTTL:cbDOutFailed", "Failed to send TTL signal. Error code: %d. Error message: %s", result, errorMessage);
}
// Wait for 5 milliseconds
#ifdef _WIN32
Sleep(5); // Sleep for 5 milliseconds on Windows
#else
usleep(5000); // Sleep for 5000 microseconds (5 milliseconds) on Unix/Linux
#endif
// Set the digital output low
result = cbDOut(BOARD_NUM, PORT_NUM, 0);
if (result != 0) {
char errorMessage[256];
cbGetErrMsg(result, errorMessage);
mexErrMsgIdAndTxt("MATLAB:sendTTL:cbDOutFailed", "Failed to reset TTL signal. Error code: %d. Error message: %s", result, errorMessage);
}
}
The problem I am encountering is that any integer I submit to the code once it is compiled (e.g., sendTTL([insert number here]) is triggering a falling TTL ALL of the Port A pins, rather than just the ones that should be triggered for just that number, so it only ever sends a decodable 255 for any integer. Could you give any advice on troubleshooting this?
Question
Darcy Diesburg
Hello,
I am trying to configure my Measurement Computing 1208FS-Plus DAQ to send 8-bit triggers that are a binary representation of 0-255 (i.e., 1 =1 0 0 0 0 0 0 0). I'm using compiled mex files with functions from the Universal Library in MATLAB 2021b and have the Dat Acquisition Toolbox and add-on for Measurement Computing DAQs installed. The DAQ is set up and calibrated in Instacal in differential mode.
Here is the C code I am using:
#include "mex.h"
#include "cbw.h"
// Define the DAQ board number
#define BOARD_NUM 0
// Define the digital port
#define PORT_NUM FIRSTPORTA
// The entry point for the MEX function
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
if (nrhs != 1) {
mexErrMsgIdAndTxt("MATLAB:sendTTL:invalidNumInputs", "One input required.");
}
// Get the input value
if (!mxIsDouble(prhs[0]) || mxGetNumberOfElements(prhs[0]) != 1) {
mexErrMsgIdAndTxt("MATLAB:sendTTL:inputNotScalar", "Input must be a scalar.");
}
double inputValue = mxGetScalar(prhs[0]);
if (inputValue < 0 || inputValue > 255) {
mexErrMsgIdAndTxt("MATLAB:sendTTL:inputOutOfRange", "Input must be between 0 and 255.");
}
// Convert the input value to an integer
int value = (int)inputValue;
// Configure the digital port
int configResult = cbDConfigPort(BOARD_NUM, PORT_NUM, DIGITALOUT);
if (configResult != 0) {
char errorMessage[256];
cbGetErrMsg(configResult, errorMessage);
mexErrMsgIdAndTxt("MATLAB:sendTTL:cbDConfigPortFailed", "Failed to configure the prort. Error code: %d. Error message: %s", configResult, errorMessage);
}
// Send the TTL signal
int result = cbDOut(BOARD_NUM, PORT_NUM, value);
if (result != 0) {
char errorMessage[256];
cbGetErrMsg(result,errorMessage);
mexErrMsgIdAndTxt("MATLAB:sendTTL:cbDOutFailed", "Failed to send TTL signal. Error code: %d. Error message: %s", result, errorMessage);
}
// Wait for 5 milliseconds
#ifdef _WIN32
Sleep(5); // Sleep for 5 milliseconds on Windows
#else
usleep(5000); // Sleep for 5000 microseconds (5 milliseconds) on Unix/Linux
#endif
// Set the digital output low
result = cbDOut(BOARD_NUM, PORT_NUM, 0);
if (result != 0) {
char errorMessage[256];
cbGetErrMsg(result, errorMessage);
mexErrMsgIdAndTxt("MATLAB:sendTTL:cbDOutFailed", "Failed to reset TTL signal. Error code: %d. Error message: %s", result, errorMessage);
}
}
The problem I am encountering is that any integer I submit to the code once it is compiled (e.g., sendTTL([insert number here]) is triggering a falling TTL ALL of the Port A pins, rather than just the ones that should be triggered for just that number, so it only ever sends a decodable 255 for any integer. Could you give any advice on troubleshooting this?
Link to comment
Share on other sites
6 answers to this question
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now