LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

labview matlab tcp ip communication problem

Hi!

 

I am creating a tcp/ip communication between labview and matlab. I am trying to send different information at the same time, strings and arrays mostly from Matlab to Labview. I used a template that I found in internet that uses Java language and from there I have been doing modifications.

This is the first time that I work with tcp/ip communication, for that matter until now I have been only able to send  one string to Labview and convert it array, from there on I have been stuck, I cannot visualize the second string sent.

The code from Matlab does not give me errors and it seems to be that sends the information, but I do not know how to reach the second string sent.

 

 

Can anybody help me please?

 

Thank you very much!!

 

 

Download All
0 Kudos
Message 1 of 9
(5,551 Views)

That's not a good tester VI.  You have everything in a while loop together, no idea when things are happening, waiting on reads and writes in the same loop, waiting forever for 2000000 bytes to show up, and more.

 

You also close the connections immediately after the read / write operations (for some reason you close one of the connections twice).  Do you know that you don't even need two separate connections?

 

It's possible that you aren't seeing anything past the initial connection and string because your VI is waiting to finish its first iteration of that huge while loop.

 

I haven't looked at your MATLAB code yet.



0 Kudos
Message 2 of 9
(5,541 Views)

Hi VItan,

 

Thank you very very much for answering, so what you suggest is to put everything in different loops? and no, I did not know that with only one connection was possible. This is the first time that I have been working with Labview and have not found to many examples of tcp/ip communication in the way that I am doing, since I want it to be receiving and sending data at the same time.

I really do need help, so please which would be your suggestions?

 

Hope to hear from you soon

0 Kudos
Message 3 of 9
(5,484 Views)

For just a quick test, I would use something like this to connect and send/receive data:

 

TCP_Example.png

 

I didn't include converting it from the bytestream string to array, but it should be simple to make a conversion SubVI and use it with the read string.



Message 4 of 9
(5,472 Views)

Hello once again!

 

Thanks very much! I have tried the code for several days while at the same time doing some modifications specially on the Matlab since perhap there was the mistake, but the connection between Labview and Matlab does not occurs. Both programs do not seem to present by themselves any problem with code, simply they do not connect when I run them simultaneously using localhost

 

Hope to hear from you, either way thanks for your time in helping me

0 Kudos
Message 5 of 9
(5,428 Views)

I would try a simple connection first to make sure that you have things working.

 

I haven't investigated your original MATLAB code, but you may want to consider simply using MATLAB's tcpip object first.  For example, in MATLAB:

 

% Create the tcpip object in the server NetworkRole

My_Connection = tcpip('localhost', 2057, 'NetworkRole', 'server');

 

% Listen on the socket.  This will wait forever until a connection is made.

fopen(My_Connection);

 

%%

% Once we get to this point, the connection is active.  Use fread and fwrite to interact with the stream.

%%

 

% Disconnect and delete the tcpip object

fclose(My_Connection);

delete(My_Connection);

clear My_Connection;



Message 6 of 9
(5,403 Views)

Hi VItan I managed to get working everything! Thanks very much, and I managed to get the string into a array as well, but I have several question that would might help me understand some stuff:

 

the only format that the information is passing from Matlab to Labview if I use "regexprep(num2str(Vout2(1),6),'\.',',')" converting the data of for example Vout2 vector into num2str and using that expression while at the same time changing it to ",". Is it the only format that Labview accepts? or do you know of any other way?

Also I am trying to send several information and to be saved in different arrays and numeric indicators lets say I write:

 

fwrite(Connection,data_to_send_serie);

fwrite(Connection,data_to_send_serie1);

fwrite(Connection,data_to_send_serie2);

 

But if I try to do several tcp read I do not seem to extract each information, in other words one array for data_to_send_serie other for data_to_send_serie1 and so on, it is just received into one single string. So far if I only send one fwrite it does create the array but that is it.

 

I have been trying unsuccessfully to do this, maybe you have some idea that might help me do this.

Thank you very much once again!!

 

 

0 Kudos
Message 7 of 9
(5,337 Views)

TCP is a connection-based octet-stream protocol.  This means two things related to your questions:

  1. You can send any data that you want.  However, you have to know how that data is organized for it to be of any use.  Therefore, when communicating across software platforms, such as MATLAB to LabVIEW, it is common to convert to a common representation, such as a string of tokens.  In your case, it looks like you made a human-readable character string of your value(s) for transfer and then replaced all periods with commas.  I'm not sure exactly what your data looked like after num2str in MATLAB or all of what you did to interpret it in LabVIEW, but the takeaway is that you can do whatever you want as long as you match on both ends.  Both LabVIEW and MATLAB view it as a string when it is written to / read from the TCP socket.
  2. Because it is a stream protocol, you will not receive individual "messages" from separate sends automatically, since they will all be put together into one long stream.  There are techniques for handling this; however, you may also want to consider using UDP (User Datagram Protocol) instead of TCP, which is a connectionless transport-layer protocol that operates with user datagrams like single "messages".


0 Kudos
Message 8 of 9
(5,308 Views)

VItan thanks for your explanation specially regarding communications between platform and how tcp/ip works, once more has become helpful!. It is a nice suggestion and very tempting the idea of using UDP but sadly it is demanded to be done by tcp/ip and through Labview 😕 those are the two requirements that I have been impose. You say that there exist some techniques, some path that I could follow? any direction to it? seriously I have been running out of ideas in this aspect......

0 Kudos
Message 9 of 9
(5,276 Views)