i have a code written in visual studio to get data from 4 sensors and it is using matlab for real time plot can u please tell me how to save data from visual studio to matlab workspace??

2 views (last 30 days)
include "stdafx.h"
HANDLE g_hComPort = INVALID_HANDLE_VALUE; // handle for the serial communication port mxArray* g_mxSignalData = NULL; // mxArray pointer for signal data Engine* g_mat; // matlab engine pointer.
// // FUNCTION: _tmain(int argc, _TCHAR* argv[]) // // PURPOSE: Entry. // int _tmain(int argc, _TCHAR* argv[]) { if ( Initial() == FALSE ) return FALSE; DisplayData(); CloseCommsPort(); mxDestroyArray(g_mxSignalData); engClose(g_mat);
return 0; }
// // FUNCTION: Initial(void) // // PURPOSE: Initial the matlab engine and the serial port.. // BOOL Initial(void) { if(!(g_mat = engOpen(NULL))) { cout<<"\nCannont open connection to MATLAB!\n"<<endl; return FALSE; } g_mxSignalData = mxCreateDoubleMatrix(50, 1, mxREAL); // Create a matlab matrix with 50 elements engEvalString(g_mat,"clear;clc;"); engEvalString(g_mat,"figure(1);"); engEvalString(g_mat,"subplot(4,1,1);");
if ( OpenCommsPort() == FALSE ) return FALSE;
return TRUE; }
// // FUNCTION: DisplayData(void) // // PURPOSE: Display the acquired data in Matlab figure. The figure is updated every 50 samples // // COMMENT: The header of the package from the XBee module starts with 0x7E 0x00 0x10 0x83. This information can be obtained by X-CTU // Generally, the range of the ADC result is from 0xAA to 0x3FF. The minimum value may vary. // void DisplayData() { unsigned char gyroBuffer[10], tempchar; DWORD dwBytesRead, fdwCommMask;; int i, j =0; short int tempshortint; float CH1, CH2, CH3, CH4; double dBuffer1[50], dBuffer2[50], dBuffer3[50], dBuffer4[50];
SetCommMask (g_hComPort, EV_RXCHAR); while(g_hComPort != INVALID_HANDLE_VALUE) { ReadFile(g_hComPort, &tempchar, 1, &dwBytesRead, NULL); if (dwBytesRead == 0) { WaitCommEvent(g_hComPort, &fdwCommMask, 0); SetCommMask (g_hComPort, EV_RXCHAR); continue; }
if(tempchar != 0x7E) { continue; }
ReadFile(g_hComPort, &tempchar, 1, &dwBytesRead, NULL); if(tempchar != 0x00) { continue; }
ReadFile(g_hComPort, &tempchar, 1, &dwBytesRead, NULL); if(tempchar != 0x10) { continue; }
ReadFile(g_hComPort, &tempchar, 1, &dwBytesRead, NULL); if(tempchar != 0x83) { continue; }
ReadFile(g_hComPort, gyroBuffer, 7, &dwBytesRead, NULL); ReadFile(g_hComPort, gyroBuffer, 9, &dwBytesRead, NULL); // Read the ADC results of 4 channels
i = 0; tempshortint = gyroBuffer[i++] << 8; // Convert the ADC result to pound ( 0--1 lb ) tempshortint += (gyroBuffer[i++] & 0xff); tempshortint -= 0xAA; CH1 = (float)tempshortint / 853; dBuffer1[j] = CH1;
tempshortint = gyroBuffer[i++] << 8; tempshortint += (gyroBuffer[i++] & 0xff); tempshortint -= 0xAA; CH2 = (float)tempshortint / 853; dBuffer2[j] = CH2;
tempshortint = gyroBuffer[i++] << 8; tempshortint += (gyroBuffer[i++] & 0xff); tempshortint -= 0xAA; CH3 = (float)tempshortint / 853; dBuffer3[j] = CH3;
tempshortint = gyroBuffer[i++] << 8; tempshortint += (gyroBuffer[i++] & 0xff); tempshortint -= 0xAA; CH4 = (float)tempshortint / 853; dBuffer4[j] = CH4; j++; if (j==50) // The data figure is updated every 50 samples { j = 0; memcpy((void *)mxGetPr(g_mxSignalData), (void *)dBuffer1, 400); engPutVariable(g_mat, "dBuffer1", g_mxSignalData); engEvalString(g_mat,"subplot(4,1,1);"); engEvalString(g_mat,"plot(dBuffer1);"); engEvalString(g_mat,"axis([0 50 0 1]);");
memcpy((void *)mxGetPr(g_mxSignalData), (void *)dBuffer2, 400); engPutVariable(g_mat, "dBuffer2", g_mxSignalData); engEvalString(g_mat,"subplot(4,1,2);"); engEvalString(g_mat,"plot(dBuffer2);"); engEvalString(g_mat,"axis([0 50 0 1]);");
memcpy((void *)mxGetPr(g_mxSignalData), (void *)dBuffer3, 400); engPutVariable(g_mat, "dBuffer3", g_mxSignalData); engEvalString(g_mat,"subplot(4,1,3);"); engEvalString(g_mat,"plot(dBuffer3);"); engEvalString(g_mat,"axis([0 50 0 1]);");
memcpy((void *)mxGetPr(g_mxSignalData), (void *)dBuffer4, 400); engPutVariable(g_mat, "dBuffer4", g_mxSignalData); engEvalString(g_mat,"subplot(4,1,4);"); engEvalString(g_mat,"plot(dBuffer4);"); engEvalString(g_mat,"axis([0 50 0 1]);"); }
if(_kbhit()) // Press "ESC" to exit the program { if( _getch() == 27 ) break; } }
return; }

Answers (0)

Categories

Find more on C Shared Library Integration in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!