javac++tcpdataformat

Read data in Java received on TCP port from hardware (C++)


I'm trying to receive data from TCP port and print in console. Since I have never experienced data receiving from TCP port, i tried to create a simple Socket to read data from port, and it works if i test it with text or numbers, but the actual data i have to receive on that port is sending from hardware sensor (using C++) in different format:

#pragma pack (4)
typedef struct{
    char imei[16];
    uint16_t read_cycle;
    uint16_t data_count;
    double data;
    int16_t interval[METER_READ_COUNT-1];
    uint16_t bat_p;
    uint16_t bat_v;
    uint16_t chsum;
} nb_packet_t;

so when i test to receive the data from hardware, i got something like - 869490051287079d�2

So how can I receive that data format in Java? I never experienced it and dont know where to start from even. Will appreciate any help, thanks in advance.

Heres my Socket to read the data:

import java.net.*;
import java.io.*;

import static java.lang.System.out;

public class EchoMultiServer {

    private ServerSocket serverSocket;

    public void start(int port) {
        try {
            serverSocket = new ServerSocket(port);
            while (true)
                new EchoClientHandler(serverSocket.accept()).start();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            stop();
        }

    }

    public void stop() {
        try {
            serverSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private static class EchoClientHandler extends Thread {
        private Socket clientSocket;
        private BufferedReader in;

        public EchoClientHandler(Socket socket) {
            this.clientSocket = socket;
        }

        public void run() {
            try {
                in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

                String strpath = "/var/new.log", ch;
                while ((ch = in.readLine()) != null) {
                    out.println(ch);
                }
                in.close();
                clientSocket.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        EchoMultiServer server = new EchoMultiServer();
        server.start(5555);
    }

}


Solution

  • Thanks to Arfur Narf i could get the data from port followinbg this steps: Create a byte array, read fully data from DataInputStream to that array(i knew the amount of bytes i have to receive), cover the byte array to ByteBuffer and set the byte order (because C++ and Java use different endianness for serializing and deserializing). Then taking bytes one by one from that ByteBuffer and convert them to primitive types. (byte order is important! has to be same as sended). And we are done

    in = new DataInputStream(clientSocket.getInputStream());
                byte[] packetArray = new byte[40];
                in.readFully(packetArray);
    
                ByteBuffer buffer = ByteBuffer.wrap(packetArray);
                buffer.order(ByteOrder.LITTLE_ENDIAN);
    
                String imei_p = new String(buffer.array(), 0, 15, StandardCharsets.UTF_8);
                readCycle_p = buffer.getShort(16);
                dataCount_p = buffer.getShort(18);
                volume_p = buffer.getDouble(20);
                interval1_p = buffer.getShort(28);
                interval2_p = buffer.getShort(30);
                interval3_p = buffer.getShort(32);
                batP_p = buffer.getShort(34);
                batV_p = buffer.getShort(36);
                chsum_p = buffer.getShort(38);