From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Data acquisition from MCU via TCP; write and read binary file

Solved!
Go to solution

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

https://forums.ni.com/t5/LabVIEW/How-to-use-fread-and-reshape-of-MATLAB-in-LabVIEW-properly/td-p/595...

 

Any ideas?

Thanks in advance!

0 Kudos
Message 1 of 9
(1,318 Views)

@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?

0 Kudos
Message 2 of 9
(1,258 Views)

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:

  • 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…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 3 of 9
(1,252 Views)

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);

 

 

0 Kudos
Message 4 of 9
(1,225 Views)

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.

Message 5 of 9
(1,191 Views)

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.

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 6 of 9
(1,186 Views)

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

0 Kudos
Message 7 of 9
(1,153 Views)
Solution
Accepted by topic author duc1

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…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 8 of 9
(1,144 Views)

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

0 Kudos
Message 9 of 9
(1,101 Views)