0
votes

I am programming using Java for a data acquisition project. I completed the interface to the device part in Python, but it's way too heavy on the processing.

I have been converting it to Java (newbie!) and was able to send and receive the setup data, which are all ASCII.

Once I ask for the data to start flowing, it's like an open faucet.

I can understand the first 4 character but then I don't.

53 31 5C B5 61 99 AB 8B A9 85 91 89 55 85 (Hex)
S  1  This is the echo to say 'starting'
      5C B5 61 99 AB 8B A9 85 91 89 55 85 (Hex)

This is the data I need and how it was sent. I can sniff it on the network monitor.

83 49 92 65533 97 65533 65533 65533 65533 (dec)

I have tried various methods, but currently:

Socket socket=new Socket(ipAddress , portNumber);       //Create Socket
inputStream=socket.getInputStream();
inputStreamReader=new InputStreamReader(inputStream); // Tried (inputStream,"UTF-8")

stringBuffer=new StringBuffer();

public static StringBuffer  recMsg(InputStreamReader inputStreamReader, StringBuffer stringBuffer) throws IOException {
    int x;
    stringBuffer.setLength(0);
    while(true)
    {
        x=inputStreamReader.read();
        stringBuffer.append((char)x);
        //System.out.println("Ready to read:2 "+ inputStreamReader.ready());
        if (stringBuffer.length() >= 500) break;
        if (!inputStreamReader.ready()) break;    //
    }
    System.out.println("Response "+ stringBuffer);
    System.out.println(" --- ");
    return stringBuffer;
}

PS in Python I just used

s.connect((host, port))
s.recv(2048)
Please edit your question to show how data is being sent from the server at the other end of the socket. If you are just sending raw bytes then we need to read it differently than If you were sending a text in the ASCII character set. Note that if you are sending ASCII characters then you might want to be using new InputStreamReader(inputStream, "US-ASCII");.sorifiend
You are ignoring end of stream on the Reader. ready() is not a test for end of stream: it has few correct uses, and this isn't one of them.user207421
inputStreamReader.read() will return -1 on EOF, eg: while ((x=inputStreamReader.read()) != -1) { stringBuffer.append((char)x); if (stringBuffer.length() >= 500) break; }Remy Lebeau
This is the actual data it uses. 53 31 5C B5 61 99 AB 8B A9 85 91 89 55 85 (Hex) The server is a Dataq module. There are no end of message characters. It just sends in blocks of 4 to 204 words, depending on the acquisition rate. It uses TCP/IP Push flag set. The data a believe is UTF-8. The data uses 2 words combined to provide the actual number and syncing. each word could be 00 to FF. The 65533 (FFFD) that the reader is pumping out is wrong. The FF and FD do not even make sense. I am obviously doing something wrong. The data is there but i'm not accessing it correctly.user15317996
The Reader. ready() seems to work on the shorter setup dialog but i will use the ((x=inputStreamReader.read()) != -1). The EOF does not actually exist in this scenario. i came back to it this morning and it's still sending data to the network. I'm just not listening...user15317996