The online racing simulator
Outgauge Packet - Java
(6 posts, started )
Outgauge Packet - Java
Hello,
i'm programming my own Outgauge Tool in Java.
I take the Outgauge UDP Packet and want to get the 4 Speed Bytes (12 - 15). After that i use a bitshifter to convert this 4 bytes into float.

But the float is totally wrong with the values.
Can anybody see my fault?

Here is the code:

<?php 
import java
.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

/* Data Packet in detail
 * Time [0 - 3]
 * AutoName [4 - 7]
 * Flags [8 - 9]
 * Gang [10]
 * SpareB [11]
 * Speed [12 - 15]
 * RPM [16 - 19]
 * Turbo [20 - 23]
 * EngTemp [24-27]
 * Fuel [28 - 31]
 * Öldruck [32 - 35]
 * ÖlTemp [36 - 39]
 * Dashlight [40 - 43]
 * ShowLights [44 - 47]
 * Throttle [48 - 51]
 * Brake [52 - 55]
 * Clutch [56 - 59]
 * Display1 [60 - 75]
 * Display2 [76 - 91]
 * ID [92 - 95]
 */

public class client {
    private static final 
int MASK 0xff;
    
/**
     * @param args
     * @throws IOException 
     */
    
public static void main(String[] args) {
        
DatagramSocket socket null;
        
DatagramPacket packet = new DatagramPacket(new byte[92], 92);
        
        try {
            
socket = new DatagramSocket(9090);
            for(;;) {
                
socket.receive(packet);

                
byte data[] = packet.getData();
                
String AutoName = new String();
                
AutoName += (char)data[4];
                
AutoName += (char)data[5];
                
AutoName += (char)data[6];
                
AutoName += (char)data[7];
                
                
int bits 0;
                
int i 12;
                for (
int shifter 3shifter >= 0shifter--) {
                    
bits |= ((int)data[i] & MASK) << (shifter 8);
                    
i++;
                }
                
Float Speed Float.intBitsToFloat(bits);

                
System.out.print("Car: " AutoName " | Gear: " + (int)data[10]);
                
System.out.print(" | Speed: " Speed "| Len: " data.length "\n");
            }
            
        } catch (
IOException e) {
            
e.printStackTrace();
        } finally {
            
socket.close();
        }
        
        
    }
}
?>

thx
I'm not much into low-level bitwise operations, but have you tried doing it like this?


for(int shifter = 0; shifter < 4; shifter++)
{
bits |= ((long)data[i] & MASK) << (shifter * 8);
i++;
}[COLOR=#000000][COLOR=#007700]
[/COLOR][/COLOR][CENTER][LEFT][COLOR=#000000][COLOR=#007700]

[/COLOR][/COLOR]
[/LEFT]
[/CENTER]
No, because the function Float Speed = Float.intBitsToFloat(bits);
needs as parameter a integer value.
Integer is in Java 4byte and Float 4byte too.

Are the packets right?
You were right about using "int" instead of "long", "long" is needed when you convert to a 64 bit long variable. Anyway, following code seems to be getting correct data for me:


public static void main(String[] args) {
byte[] ogBuf= new byte[92];

DatagramSocket sock = null;
DatagramPacket udpPack = new DatagramPacket(ogBuf, 92);

try
{
sock = new DatagramSocket(30000);

while(true)
{
sock.receive(udpPack);

int bits = 0;
int idx = 12;
for(int shiftBy = 0; shiftBy < 4; shiftBy++)
{
bits |= ((int)ogBuf[idx++] & 0xFF) << (shiftBy * 8);
}

float speed = Float.intBitsToFloat(bits);

System.out.println("Current speed: " + speed*3.6f + " km/h");
}
} catch (Exception ex)
{
ex.printStackTrace();
}
}

It's run. Thank you
I think it was the bitwise operation

Outgauge Packet - Java
(6 posts, started )
FGED GREDG RDFGDR GSFDG