Jump to content

Sending Labview data via TCP


Recommended Posts

Hi,

I'm building an application that allow to control a DAQ.

This DAQ acquires data and turn on and off a light.

Labview and Java communicates, but I read wrong data.

The DAQ acquires double samples that are converted in String format and sent via tcp. Numbers are like 0,00006 ; 1, 50000043 etc...

In Java i receive data like 54; 42 etc...

What's wrong? I also tried the big-endian/little-endian conversione without results.

Can someone help me? In labview I can write only Strings, but if I try to use a buffered reader with the instrucion br.readLine() the program doesn't work.

Thanks and sorry for my english,

Veronica

Link to comment

Hi,

I'm building an application that allow to control a DAQ.

This DAQ acquires data and turn on and off a light.

Labview and Java communicates, but I read wrong data.

The DAQ acquires double samples that are converted in String format and sent via tcp. Numbers are like 0,00006 ; 1, 50000043 etc...

In Java i receive data like 54; 42 etc...

What's wrong? I also tried the big-endian/little-endian conversione without results.

Can someone help me? In labview I can write only Strings, but if I try to use a buffered reader with the instrucion br.readLine() the program doesn't work.

Thanks and sorry for my english,

Veronica

Veronica,

It would help if you posted an example of your LabVIEW code and Java code so we can understand what's going wrong.

Link to comment

Thanks for replays.

This is my VI:

2iq4hf.png

And this is the related java code... I know that I shoudn't use int to read from the socket, but if I use a BufferedReader the program doesn't work (no output).


import java.io.*;

import java.net.*;

import java.nio.ByteBuffer;

import java.nio.ByteOrder;



public class ServerThread extends Thread {

 

   public ServerThread() {   

   }

 

	@Override

   public void run(){

  	ServerSocket welcomeSocket;

  	InputStream myInputStream;

  	try {

      	welcomeSocket = new ServerSocket(2056);

      	Socket connectionSocket = welcomeSocket.accept();

      	System.out.println(connectionSocket);

      	myInputStream=connectionSocket.getInputStream();

      	int b;

      	while ((b = myInputStream.read()) != -1) {

          	System.out.println(b);

      	}

  	} catch (IOException ex) {

  	}

   }

[/CODE]

Edited by Skeggy88
Link to comment

Can it be that you see the ASCII values corresponding to the characters you send from LabVIEW?

/J


   public void run(){

  	ServerSocket welcomeSocket;

  	InputStream myInputStream;

  	try {

      	welcomeSocket = new ServerSocket(2056);

      	Socket connectionSocket = welcomeSocket.accept();

      	System.out.println(connectionSocket);

      	myInputStream=connectionSocket.getInputStream();

      	int b;

      	while ((b = myInputStream.read()) != -1) {

          	System.out.println(b);

      	}

  	} catch (IOException ex) {

  	}

[/CODE]

Mellroth is right. The documentation for InputStream.read() says that it returns a byte at a time. You're reading byte values. You need to convert them to text. Store them as bytes and then put those bytes into a string constructor where you specify the character set as "ISO-8859-1": new String(bytes, "ISO-8859-1"). Of course, then your data would be a string, not a double.

[u]However[/u], this is probably the worst way possible to transfer data short of using a messenger pigeon. A) It's slower than molasses. B) You lose precision. C) It places the burden of formatting the data for display on the part of the code that gets the data.

You should probably use Flatten to String and then transmit that string. On the Java side, read [b]8[/b] bytes at a time and then use Java to assemble those bytes into a double. Note: you may need to change the endianness!

  • Like 1
Link to comment

I think that with the name 'Veronica,' the way you speak, and that you said "Sorry for my English" you're Italian. No? You can speak Italian here, here, and here.

Penso che col nome 'Veronica,' Suo modo di parlare, e che Lei ha detto "Sorry for my English" Lei è italiana. No? Lei puo parlare in italiano qui, qui, e qui. Mi sono dimenticato la maggiorparte del mio italiano perche non l'ho parlato da 2006. Mi dispiace.

Link to comment

I modified the VI and also the code.

http://bayimg.com/EAgiGAAeI

In labview I can choose between little endian and big endian and I either tried, but I obtained values like 1.1319598827449316E-72 instead of, for example, 0,0145643.

However thanks because is really fast :)


import java.io.*;

import java.net.*;

import java.nio.ByteBuffer;

import java.nio.ByteOrder;

public class ServerThread extends Thread {

 

   public ServerThread() {  

   }

  

	@Override

   public void run(){

  	ServerSocket welcomeSocket;

  	InputStream myInputStream;

  	byte[] array = new byte[8];

  	int i=0;

  	try {

      	welcomeSocket = new ServerSocket(2056);

      	Socket connectionSocket = welcomeSocket.accept();

      	System.out.println(connectionSocket);

      	myInputStream=connectionSocket.getInputStream();

      	Double num;

      	int b;

      	while ((b = myInputStream.read()) != -1) {

          	array[i]= (byte) b;

          	if(i==7){

              	num = toDouble(array);

              	System.out.println(num);

              	i=0;

          	}

              	else {

              	i++;

          	}

      	}

  	} catch (IOException ex) {

  	}

   }

public static double toDouble(byte[] bytes) {

	return ByteBuffer.wrap(bytes).getDouble();

}

}

[/CODE]

I know that there are italian forums, but I prefer american forums because members are more active. And I also know that my english is terrible :P I hope you understand me!

Edited by Skeggy88
Link to comment

I modified the VI and also the code.

http://bayimg.com/EAgiGAAeI

In labview I can choose between little endian and big endian and I either tried, but I obtained values like 1.1319598827449316E-72 instead of, for example, 0,0145643.

However thanks because is really fast :)

I think the problem is that you're not wiring a double into the Flatten To String. Check the length of the string produced by Flatten To String. It should only be 8 if you wire in a double. No more and no less. You had a node to convert the data wire to a double in the original. You should use that. I recognize the node, but I can't remember what it's called. (You might be able to use a regular Double conversion bullet).

Link to comment

Mellroth is right. The documentation for InputStream.read() says that it returns a byte at a time. You're reading byte values. You need to convert them to text. Store them as bytes and then put those bytes into a string constructor where you specify the character set as "ISO-8859-1": new String(bytes, "ISO-8859-1"). Of course, then your data would be a string, not a double.

However, this is probably the worst way possible to transfer data short of using a messenger pigeon. A) It's slower than molasses. B) You lose precision. C) It places the burden of formatting the data for display on the part of the code that gets the data.

You should probably use Flatten to String and then transmit that string. On the Java side, read 8 bytes at a time and then use Java to assemble those bytes into a double. Note: you may need to change the endianness!

Aside from any encoding issues, it would be enough to change the line:

                System.out.println(b);[/CODE]

[font=arial,helvetica,sans-serif]to[/font]

[CODE] System.out.println((char)b);[/CODE]

[font=arial,helvetica,sans-serif]The first invokes the println(int number) method, while the second invokes the println(char character) method. This of course will only work for ASCII characters up to decimal code 127 which LabVIEW should be normally generating, unless you use foreign language characters in the format specifiers itself.[/font][font=arial,helvetica,sans-serif]If you need proper character encoding too, you would have to wrap the input stream into an input reader of some sort.[/font]

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Similar Content

    • By David Akinpelu
      I am implementing a TCP connection between MyRio(client) and Python(server). The goal is to send data from the server to the client, perform some calculations, and send the result to the server. I need to keep changing the data being sent to the client. I noticed the first data works fine and the result returned to the server, but the client could not read subsequent data. I debugged and was able to detect that the error is coming from the first read function is the "Reading values subvi"  but I don't know how to correct it. How do I go about it?
      I have attached the files below.
      Reading Unknown Bytes from TCP.vi Second_client.vi SimpleServer.py
    • By Diegodemf
      Hello, I'm trying to send data from my RPI b+ to labview and store the information in a spreadsheet, right now the data I'm using is just a sawtooth generated in the python code but eventually it will be sensor data adquired via spi, the data comes at =~ 6-7khz but the time it takes to send the data to labview is much greater, I fear the circular buffer I intended to use to store the data before sending it via tcp will get filled pretty fast. I'm not very experienced in either python nor Labview, I was wondering if i could get some advice in the optimization of the code.

      I attached the VI of the client and a VI that graphs the data on the spreadsheet, also the python code.
      Thanks in advance, sorry for the mess.
      Waveform Grapher.vi
      Client.vi
      serverSerial.py
    • By bigjoepops
      I am working with a Universal Robot arm.  I believe it is a UR-5.  I was under the assumption that it could be controlled via Modbus, but I found that was incorrect.  I need to use URScript commands.  I am having a hard time getting started with this.  I was wondering if anyone has written a VI that would control the movement of one of these robot arms.
      Thanks.
    • By Zyl
      Hi everybody,
       
      I'm actually running on a problem with a  TCP connection between 2 cRIOS.
      One cRIO is a server which writes 76 bytes long messages every 10ms (today, but can be anything between 1ms and 1s) using STM Write VI (so at the end it pushes 82 bytes long message in the TCP write function). I want that message to be sent only if the client as time to read it, so I set the timeout to 0.
      The other cRIO is a the client with tries to read on the TCP link at a speed of 1000Hz (1ms, Wait next Multiple used to ensure that the loop is not running faster). I use the STM Read VI to get the data sent from the other cRIO. The read function has a timeout of 100ms.
      What I was expected is that the client loop would actually runs at 10ms rate (server writing rate) due to the 100ms timeout. And if the server writes faster, it would follow the server rate. If the server rate is greater that 100ms, error 56 would be fired, and I would handle it.
      What happens is that the server writes the 82 bytes every 10ms. But the client loop is always getting data and runs at 1ms ; which means that the timeout is not respected ! I disabled Nagel algorithm on the server side to be sure that the message is sent when requested, but it didn't help. The client acts like there is always data in the read buffer. Even if it can be right for the first iterations, I would expect that running at 1ms rate, the client would empty the buffer rapidly, but it seems that it never ends... Moreover, the longer the server writes, the longer it takes for the client to see empty buffer (timeout reached again and error 56) when the server is stopped (but connection not closed).
      Does somebody already ran into this issue ?
      Any idea on how I can solve this ?
       
      Server code is attached to the post. 2 TCP connections are established between the server and client (so same IP address, but different ports), but only one is used (upper loop). The other opens and close immediately because EnableStream boolean is always false.

    • By mhy
      How do you decode, unpack a flattened string in python which was sent by a LabVIEW TCP server?
      I want to exchange data via loopback. Therefore I take a sine wave and flatten it to string and sent over the network Simple TCP - ServerSINE.viSimple TCP - ServerSINE.vi. Then I have to decode the incoming data in a way that I have the correct numerical values like when I plot them in LabVIEW. I did this so far in python_client.py but the values are wrong.
      Does anyone know how to work with the transferred Data in python?
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.