I'm using MATLAB to read from a serial port. A colleague of mine is doing the same thing with LabVIEW. He told me that I needed a pause in my code to allow the system time to write the data back. However, I've read that "pause(n)" halts execution for n seconds.
I'm not totally sure what it means by "halts execution." Does is stop the serial port from reading and writing, therefore nullifying my purpose?
Should I use another function, or should pause(n) be okay for my purposes?
pause(n) basically makes your program sleep for n seconds. As such, when you invoke pause, it makes your program wait there for n seconds, then proceeds to the next line of code.
For example:
a = rand(3,3);
pause(2); % // Pause for 2 seconds
b = rand(4,4);
This creates a random 3 x 3 matrix stored in a, then the program waits at the second line for two seconds. The program does nothing and sleeps. After, a 4 x 4 random matrix is created.
To answer your question, this does not stop the serial port. All you're doing is allowing the data enough time to be written to the serial port before you decide to write more to the port. Similarly, you're allowing the serial port enough time to buffer enough data to the port so you can read the right amount of bytes in one read.
Related
As a student I am currently working on a Matlab Simulink project. I am quite new to using Matlab/Simulink (few weeks).
I want to implement and run a Matlab “.m” file with which I can open Simulink and start the simulation. The aim is to do a 24h Test with a load cell cut into 1h “pieces” and to save the data to different sheets of an excel file each hour. So my simulation runs for 1h, stops and starts again, and so on. Through Matlab and a “for” loop I do the measures 24 times.
Between measuring steps I have to wait for simulink to finish its measures and saving the file in order for the Simulink window to be able to get closed by close_system('Thesis_SerDatTransm_Simulink').
So I tried to implement the delay with a while loop and by checking if the measures I get fit into an array of the size bigger than 449 (I measure 449 values):
for k=0:1:24
% Load Simulink
load_system('Thesis_SerDatTransm_Simulink.slx')
% Open Simulink
open_system('Thesis_SerDatTransm_Simulink.slx')
% Start Simulation
set_param('Thesis_SerDatTransm_Simulink', 'SimulationCommand', 'Start');
% Save Data
my_cell = sprintf('A%d',k);
xlswrite('file.xlsx',y,my_cell)
% Wait for Simulation
while 1
test=size(y)>=449;
if (test)
close_system('Thesis_SerDatTransm_Simulink')
break
end
end
end
The Problem now is, that program gets stuck at the while loop. Simulink is started, but no simulation or data gathering is done.
So I wondered if anyone could check if something is wrong with my While loop, since the rest of the programm works all fine without the loop (but receiving an error message, that during the simulation, Simulink window can't be closed).
I know there is a way to create a delay with waitforin matlab and create another function which I could call, but I couldn't figure out how to do this yet.
thanks
Regards
hohmchri
The right way to do this is to use the sim function to run your model (not the sequence of load_system, open_system and set_param that you have).
sim will block the execution of m-code until the model completes executing. Data can either be returned into the workspace (when used with no output arguments) or returned as an output from the call to sim. (And then you can write it to Excel as you've done.)
The only reason not to use sim, and perhaps use the commands you have, is if the model takes a long time to initialize, and you don't want to to open and close it every time through the loop. However, even in this case your code isn't correct. The load_system would be outside the loop; the open_system is not required; in your while loop you would poll the model's SimulationStatus property to see if it is still running (not the size of the y variable); and the close_system would be after the loop (as indicated by #m_power in one of the comments).
As written you should use the matlab pause command. This stops your execution for X seconds.
You should also look to optimize your code as m_power states
I have an incoming data and I would like to store it and then Output this data but after a certain delay, after some milliseconds i Output this data.
I used the Queue block inside an enabled Subsystem and the Trigger Signal is the clock divided by 10, So i have evrery time .. every 0.1 second i Output the values from the block,.. but the data is accumulated, not delayed. any idea why?
Here is the Picture of this Operation
and
EDIT: You now show how are you storing the signal. And The queue block is used wrongly.
If you want just to delay a signal, and output it delayed, then use my answer below. I am unsure what you mean by store it by N time and then output it. Simulink is "continuous" thus you can not output it "in one go" after N time, that makes no sense. The closest thing to that is to delay the singal, and for that, you dont need that enabled subsystem, you just need the Transport delay block.
ORIGINAL
What about the Transport Delay block?
It looks like this:
and It allows you to set the delay time in seconds, isntead of in ticks (as z^-1 does).
I'm having an issue which is partially Matlab- and partially general programming-related, I'm hoping that somebody can help me brainstorm for solutions.
I have an external microcontroller that generates a large stream of binary data (~40kb) every 400ms and sends it via UART to a PC running Matlab scripts. The data is not encoded in hexa or dec characters, but true binary (hence, there's no terminator defined as all 256 values are possible, valid combinations of data). Baudrate is set at 1024000. In short, it takes roughly 375ms for a whole stream of data to be sent, with 25ms of dead time in between streams
In Matlab, the serial port is configured correctly (also 1024000, 8x bits, 1x stop bit, no parity, no hardware flow control, etc.). I am able to readout the data I'm sending via the microcontroller correctly (i.e. there's no corruption of data), but I'm not being able to synchronize the serial readout on Matlab. My script is as follows:
function data_show = GetDATA
if ~isempty(instrfind)
fclose(instrfind);
end
DATA_TOTAL_SIZE = 38400;
DATA_buffer = uint8(zeros(DATA_TOTAL_SIZE,1));
DATA_show = reshape(DATA_buffer(1:2:end)',[160,120])';
f_data_in = false;
f_data_out = true;
serialport = serial('COM11','BaudRate',1024000,'DataBits',8,'FlowControl','none','Parity','none','StopBits',1,...
'BytesAvailableFcnCount',DATA_TOTAL_SIZE,'BytesAvailableFcnMode','byte','InputBufferSize',DATA_TOTAL_SIZE * 2,...
'BytesAvailableFcn',#GetPortData);
fopen(serialport);
while (get(serialport,'BytesAvailable') ~= 0) % Skip first packet which might be incomplete
fread(serialport,DATA_TOTAL_SIZE,'uint8');
end
f_data_out = true;
while (1)
if (f_data_in)
DATA_buffer = fread(serialport,DATA_TOTAL_SIZE,'uint8');
DATA_show = reshape(DATA_buffer(1:2:end)',[160,120])'; %Reshape array as matrix
DATAsc(DATA_show);
disp('DATA');
end
pause(0.01);
end
fclose(serialport);
delete(serialport);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function GetPortData (obj,~)
if f_data_out
f_data_in = true;
end
end
end
The problem I see is that what I end up reading is always the correct size, but belongs to multiple streams, because I haven't found a way to tell Matlab that these 25ms of no data should be used to synchronize (i.e. data from before and after that blank period should belong to different streams).
Does anyone have any suggestions for this?
Thanks a lot!
For completeness, I would like to post the current implementation I have fixing this issue, which is probably not a suitable solution in all cases but might be useful in some.
The approach I took consists in moving into a bi-directional communication protocol, in which Matlab initiates the streaming by sending a very short command as a trigger (e.g. single, non-printable character). Given the high baudrate it does not add significant delay due to processing in the microcontroller's side.
The microcontroller, upon reception of this trigger, proceeds to transmit only one full package (as opposed to continuously streaming package at a 5Hz rate). By forcing Matlab to pickup a serial package of the known length right after issuing the trigger, it ensures that only one package and without synchronization issues is received.
Then it becomes just a matter of encapsulating the Matlab script in a routine with a 5Hz tick given by a timer, in which the sequence is repeated (send trigger, retrieve package, do whatever processing, and repeat).
Advantages of this:
It solves the synchronization problems
Disadvantages of this:
Having Matlab running on a timer tick does not ensure perfect periodicity, and hence the triggers might not always be sent at exactly 5Hz. If triggers are sent at "inconvenient" times for the microcontroller, packages might need to be skipped in order to avoid that a package is updated in memory while it is still being transmitted (since transmission takes a significant part of the 200ms time slot)
From experience, performance can vary a lot depending on what the PC running Matlab is doing. For example, it works fine when the PC is left on its own to do the acquisition, but if another program is used (e.g. Chrome), Matlab begins to lag and that results in delays in transmission of triggers.
As mentioned above, it's not a complete answer, but it is an approach that might be sufficient in some situations. If someone has a more efficient option, please fell free to share!
I have a DAQ for Temperature measurment. I take a continuous sample rate and after DAQ, calculating temperature difference per minute (Cooling Rate: CR) during this process. This CR and temperature values are inserted into the Matlab script for a physical model running (predicting the temperature drop for next 30 sec). Then, I record and compare the predicted and experimental values in LabVIEW.
What i am trying to do is the matlab model is executing every 30 sec, and send out its predictions as an output from matlab script. One of this outputs helps me to change the Air Blower Motor Speed until next matlab run( eventually affect the temperature drop for next 30 sec as well, which becomes a closed loop). After 30 sec while main process is still running, sending CR and temperature values to matlab model again, and so on.
I have a case structure for this Matlab script. And inside of case structure i applied an elapsed time function to control the timing for the matlab script, but this is not working.
Yes. Short answer: I believe (one of) the reasons the program behaves weird on changed timing are several race conditions present in the code.
The part of the diagram presented shows several big problems with the code:
Local variables lead to race conditions. Use dataflow. E.g. you are writing to Tinitial local variable, and reading from Tinitial local varaible in the chunk of code with no data dependencies. It is not known whether reading or writing will happen first. It may not manifest itself badly with small delays, while big delays may be an issue. Solution: rewrite you program using the following example:
From Bad:
To Good:
(nevermind broken wires)
Matlab script node executes in the main UI execution system. If it is executing for a long time, it may freeze indicators/controls as well as execution of other pieces of code. Change execution system of other VIs in your program (say to "other 1") and see if the situation improves.
I am trying to establish a serial link in Matlab with an Arduino board. Reading data from the board goes well. However, writing data to the board takes about a second for each block of information I send.
The code I am running to write data:
s = serial(comprt,'BaudRate',9600,'DataBits',8);
fopen(s);
fprintf(s, '%c', 'c');
fprintf(s, '%u %u %u %u \n', [A B C D]);
pause(1);
fprintf(s, '%c', 'a');
pause(1);
A, B, C, D are 8-bit numbers anywhere from 0 - 255, 'c' and 'a' are characters commands that do stuff on the Arduino board and tap into the firmware on the board.
If I do not include the pause(1) commands, so when I do not stop Matlab from executing the next command for at least a second, the serial information doesn't get through.
Can anyone help me to speed up writing stuff to the serial port? I checked with the Arduino editor, and when I enter equivalent commands via their interface, everything is fine. So the delays are not related to the Arduino board or device drivers, it's definitely on the Matlab side of things.
I have used MATLAB quite a bit with Arduino. Ex: see here (http://www.instructables.com/id/Arduino-to-MATLAB-GUI-Live-Data-Acquisition-Plotti/) [see link in instructable for my GitHub Arduino and MATLAB code] and here (https://www.youtube.com/watch?v=wY3oh2GIfCI).
I believe your problem IS on your Arduino side of things.
Add this line to your setup() function:
Serial.setTimeout(100); //this will make the Arduino wait a max of only 100ms per incoming set of serial data, before moving on
Read here: http://arduino.cc/en/Serial/SetTimeout
Then, decrease the timeout progressively until you get bad results, to minimize wasted waiting time. Then increase it a bit again to ensure it's set high enough.
This is a quick and dirty method. Basically, your Arduino is set to wait 1 sec by default before continuing on, once incoming data is read in.
A better method is to use a terminating character. Ex: have the MATLAB send a terminating Newline character, and use the Arduino function Serial.readBytesUntil() to read up to the terminating character. Then, the serial input timeout will never be reached, and you can set the timeout to be long again (Ex: 1 sec), without actually having to wait for that delay.