简体   繁体   中英

Read double values from TCP/IP connection (JAVA)

I need to read the speed value from a driving simulator which is send over TCP/IP.

The Packet Definition from the simulator looks like this:

<PacketDefinition> =  ((VariableNames, Datatype, Defaultvalue));

so my SendDefinition looks like this:

SendDefinition = ((speed, double, 0.0));

The documentation of the driving simulator gives the informtion that double has Data length = 8.

I already tried to read the value like this:

Socket s = new Socket(server, port);
InputStream is = s.getInputStream();
DataInputStream dis = new DataInputStream(is);
System.out.println(dis.readDouble());

But it gives me nonsense values.

Can anyone please help me to read the correct values? Additional information: the driving simulator is called "SILAB"

Maybe this C++ Code could help:

void ClusterClientSocket::on_readyRead(){

typedef enum _typeRead{typeRev, typeSpeed, typeSpeedLimit} TypeRead;
static TypeRead nextType = typeRev;

// read recurringly the data from the simulator in the order rev, speed, speed limit
while(m_socket->bytesAvailable() >= sizeof(double)) {
    char buffer[sizeof(double)];
    qint64 size = m_socket->read(buffer, sizeof(double));
    if(size != sizeof(double)) {
        break;
    }

    double value = *(reinterpret_cast<double *>(buffer));
    switch(nextType) {
    case typeRev:
        // qDebug() << "rev: " << value;
        emit revReceived(value);
        nextType = typeSpeed;
        break;
    case typeSpeed:
        //qDebug() << "speed: " << value;
        emit speedReceived(value);
        nextType = typeSpeedLimit;
        break;
    case typeSpeedLimit:
        //qDebug() << "speedLimit: " << value;
        emit speedLimitReceived(value);
        nextType = typeRev;
        break;
    default:
        qDebug() << "unknown value for nextType";
        break;
    }
}}

It is an example i got for the following SendDefinition

SendDefinition =
            (
                (rev, double, 0.0),
                (speed, double, 0.0),
                (speedLimit, double, 0.0)
            );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM