01-26-2021 05:52 AM
Hello everyone!
I'm trying to translate a matlab code to labview and have some troubles...
The MCU (ESP32) reads a voltage and is connected via WiFi to the computer and it sends data packets. Each data packet is 1024 bytes and it is equal to 1024 datapoints, uint8. I can receive the data packets.
I want to save all in coming datapoints in the same array (which will grow) to create a graph in real time. The graph is voltage over datapoints, which I'll transpose to voltage over time, later on.
The matlab code is using "InputBufferSize" and "fread()". So I create, write and read a binary file to do the stuff. But the problem is my Array is not working properly.
this was helpful for me
Any ideas?
Thanks in advance!
Solved! Go to Solution.
01-26-2021 11:40 AM
@duc1 wrote:
. But the problem is my Array is not working properly.
That statement doesn't tell us anything. How is is not working properly? What are you getting and what are you expecting?
01-26-2021 11:59 AM - edited 01-26-2021 12:00 PM
Hi duc,
@duc1 wrote:
The matlab code is using "InputBufferSize" and "fread()". So I create, write and read a binary file to do the stuff.
What's the point of writing data to a file and then to read the file again to display the data?
Why not display directly after receiving the data from TCP?
@duc1 wrote:
But the problem is my Array is not working properly.
Any ideas?
Ideas:
As RavensFan already wrote: it would help to have specific information on the problems you encounter. In your case it would also help if you would attach your Matlab code you are trying to convert…
01-27-2021 04:32 AM
Hello,
thanks for the replies!
@RavensFan
I'm expecting any values in any array output by reading the binary file, but the array is greyish.
There is another problem, when I press the Stop button the error message End of File appears. Is there a connection with the arrays or is the wiring not correct?
@GerdW wrote:
Hi duc,
@duc1 wrote:
The matlab code is using "InputBufferSize" and "fread()". So I create, write and read a binary file to do the stuff.
What's the point of writing data to a file and then to read the file again to display the data?
Why not display directly after receiving the data from TCP?
A temporary storage like InputBufferSize would do it too of course. Is there something like that in LABVIEW?
Step 2 after the matlab translation ..., I want to send voltage data and a synchronization trigger signal to LABVIEW, which is continuously in a known time for the time synchronization. I think, I need to adjust the array or trigger something in LabVIEW. I want to save the data from the graph at the end.
Ideas:
- File access can get tricky when you mix read and write operation: you need to be aware of the FilePosition pointer!
- (Your file reference should be held in a shift register instead of simple tunnels for safety…)
- When handling plain bytes (U8 or simple LabVIEW strings) you don't need to specify a byte endianness.
- You are writing the string with prepended string size, but then you (try to) read just 1024 bytes without handling the string size (I32 value = 4 bytes!)…
As RavensFan already wrote: it would help to have specific information on the problems you encounter. In your case it would also help if you would attach your Matlab code you are trying to convert…
Thank you!
% connect via tcp
% temporary storage
%
% read binary data and append consecutively
% x-axis datapoint, y-axis amplitude
% while loop ends with toc
clc
clear
t = tcpip('192.168.4.1', 6789); %address und port
t.ByteOrder = 'littleEndian'; %byte order
t.InputBufferSize = 2^20; %temporary storage during read operation
packCounter = 0; %counter
packSize = 1024; %numbers of data in bytes
%freq = "1";
%inc = "_test";
fopen(t);
figure
h = animatedline;
ax = gca;
ax.YGrid = 'on';
ax.YLim = [0 255];
tic
while toc < 15 % set timer in s with toc
%fread(fileID, sizeA, precision)
newData = fread(t, packSize, 'uint8');
% addpoints(line, x axis, y axis)
% addpoints(h, linspace(x1, x2, generates n points), newdata)
addpoints(h, linspace(packCounter * packSize, packCounter * packSize + packSize, packSize), newData)
% X-AXIS
ax.XLim = [packCounter * packSize - 10 * packSize, (packCounter + 1) * packSize];
drawnow
packCounter = packCounter + 1;
toc
end
fclose(t);
01-27-2021 03:02 PM
One thing I noticed is that your TCP/IP Read is set for a mode of CRLF which means the Read will end when it sees that byte combination. That is a bad idea when you are dealing with binary data as that byte combination might show up as a part of the data.
You should use Standard mode.
01-27-2021 03:19 PM
Hi duc,
@duc1 wrote:
A temporary storage like InputBufferSize would do it too of course. Is there something like that in LABVIEW?
InputBufferSize sets a buffer of 2^20 Bytes (= 1MiB) for the TCP reader process.
In LabVIEW this is an array of the same size, created with InitArray. Read blocks of 1024 bytes and replace elements in your array with these blocks.
01-29-2021 08:30 AM
Hello,
thanks for the help I appreciate it!
I've deleted the part with files and use the 'String to Byte Array Function' and 'Insert Into Array Function'. I'm getting a graph like this in matlab code.
Thanks again!
Duc
01-29-2021 12:23 PM - edited 01-29-2021 12:25 PM
Hi duc,
@duc1 wrote:
I've deleted the part with files and use the 'String to Byte Array Function' and 'Insert Into Array Function'.
Never use InsertIntoArray, when BuildArray can give the very same result!
Why do you build an array of relative time values when you never use that array?
When the "Ausgang" is used to send data to other VIs (using the ConPane) then that control belongs after the loop…
02-01-2021 09:02 AM
Hi GerdW,
Never use InsertIntoArray, when BuildArray can give the very same result!
Allright, I change it! I had problems with this function but I found out I can select Concatenate Inputs.
Why do you build an array of relative time values when you never use that array?
When the "Ausgang" is used to send data to other VIs (using the ConPane) then that control belongs after the loop…
I converted the X-Axis from Datapoints to Time. It 's an approximation, of course.
I hope I'll find out the time delay between a directly connected signal generator versus WiFi and sync the signals.
Thanks!
Duc