Facility Control

 View Only
  • 1.  Convert [B@ Byte Value to Hex

    Posted 01-28-2021 18:53

    Hey All,

    Wondering if someone can help me out?
    I'm currently trying receive TCP Payload Data in Dashboard. Its 11 Bytes long consistently.
    Using the event.getBytesAsString() command doesn't give me any data but event.getBytes() does.
    My issue is, is that the Bytes are displayed as [B@ values. Which, reading the internet are Object ID codes for Byte Arrays.

    So, can anyone help me out converting from these code values into my 11 byte Hex Data I can see is there in Wireshark? 

    Cheers,

    Chris



  • 2.  RE: Convert [B@ Byte Value to Hex

    Posted 01-29-2021 13:37

    Hi,

    Try this:

    var bytesArr = event.getBytes();

    var response = '';

    for (i = 0; i < 11; i++) {
    response += String.fromCharCode(bytesArr[i]);
    }

    11 is actually the length of you message. 

     


    #DashBoard


  • 3.  RE: Convert [B@ Byte Value to Hex

    Posted 01-29-2021 15:05

    Hi Alex,

    Thanks very much for the response!

    Your code seems to indeed convert the ID's into bytes (I see the output of "response" as squares when I do a debug on it.

    I presume somehow I'd need to convert these bytes again to get a string value?
    I'm familiar with the event.getBytesAsString attribute but have only used it as task via a listener before.
    How would I go about getting this to work with a variable, if indeed that's the correct way to do it.

    Thanks,

    Chris

     


    #DashBoard


  • 4.  RE: Convert [B@ Byte Value to Hex

    Posted 01-29-2021 16:01

    Hello Chris,

    This code should actually convert raw bytes to its hex values. if you want then convert those values to ascii you  need to perform one more manipulation with the string. Are you sure that the payload should be as ascii string in the end?

     


    #DashBoard


  • 5.  RE: Convert [B@ Byte Value to Hex

    Posted 01-29-2021 20:32

    Hey Alex,

    That's interesting as I'm seeing just a timestamp as the response variable output using your suggested code. 
    Payload doesn't need to be ascii, just hex.
    I've set the Listener Delimiter to Bytes and a fixed length of 65. This is consistent for the entire packet on all of my response messages.
    I've also tried a Fixed Length of 11 or a Bytes of 0x1003 as my response always ends in 10 03.

    All I'm trying to achieve is to be able to display a confirmed message in a panel based on whether a certain hex string is sent back to my panel 


    #DashBoard


  • 6.  RE: Convert [B@ Byte Value to Hex

    Posted 02-01-2021 12:00

    Hello Chris, 

    There is alternative concept to catch messages in dashboard, you need to set listener delimiter type to fixed length and 1 byte per message. 

    Select "stop all receive/task execution on pause", and try this code on listener (you should get some data, and then I think you can convert it to hex with String.fromCharCode):

    if (event.isMessageEvent())
    {
    ogscript.debug("Got message");
    var data = [];

    //we need to get the first byte the normal way
    data.push(event.getBytes()[0]);
    var stream = this.getInputStream();
    ogscript.debug("Data available: " + stream.available()); //this will print you message length

    //get the rest of the bytes in the message and add to our array
    while( stream.available())
    {
    data.push(stream.read());
    }

    //what we got?
    for(var i = 0; i< data.length; i++)
    {
    ogscript.debug(data[i]);
    }
    return;

    }

    #DashBoard


  • 7.  RE: Convert [B@ Byte Value to Hex

    Posted 02-01-2021 12:45

    Hi Alex,

    Thanks so much! This has worked perfectly.

    I've added a .toString(16) to the data and its now giving me the Hex values in the debug.

    Thanks again!


    #DashBoard