How can I input data in real time?

19 views (last 30 days)
James116
James116 on 24 Feb 2014
Edited: James116 on 25 Feb 2014
I have a C++ program that spits out data from an instrument. It can not be embedded in Matlab. I need a way to stream the data into matlab/simulink in real time.
I have used a text file method, but it only runs at about 100 Hz. It seems opening and closing the file takes too long. I need my program to run at 10Khz.
Is there any way to get data into Matlab this quickly? Perhaps some type of port emulation thing? Some type of direct read and write from memory?
  2 Comments
Suneesh
Suneesh on 24 Feb 2014
1. Does the C++ program offer some kind of an API? You could use that from MATLAB/Simulink perhaps from an S-Function.
2. If the program is able to send out UDP or TCP/IP packets then the Instrument Control Toolbox has features that allows to receive such packets.
Note, Simulink does not run in realtime. You could run a model in "pseudo-realtime" using something like this:
This might not be applicable to you but, we do have real-time products like Real Time Windows target and xPC Target.
James116
James116 on 25 Feb 2014
Edited: James116 on 25 Feb 2014
The c program is just something I wrote. I should have specified that I am building to an external target. The target will not accept my s functions for this particular c++ code.
Here is what I ended up doing:
-Downloaded a serial port emulator (Virtual Serial Ports Emulator, available from Cnet)
- Wrote the following code into to my C++ program:
#include <windows.h>
HANDLE hSerial;
hSerial = CreateFile( "COM2", GENERIC_WRITE, 0,0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
DCB dcbSerialParams = {0};
dcbSerialParams.BaudRate= 921600;
dcbSerialParams.ByteSize=8;
dcbSerialParams.StopBits=ONESTOPBIT;
dcbSerialParams.Parity=NOPARITY;
DWORD dwBytesWritten = 0;
WriteFile(hSerial, &intdata, sizeof(intdata), &dwBytesWritten, NULL);
Then I used a serial receive block to get the data into simulink. I achieved about 10x the speed of the text file method. I may try TCP/IP next.
Thank you for your response

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!