Payload Decoder with Device variables

Hello, everyone,

I am in the process of writing a decoder that also reads the variables of the device. This is supposed to work with “variables”, but I don’t understand how to implement it.
I have several device variables that I need for a calculation within the decoder. I’m not very good at Javascript, so I don’t quite understand some of the connections.
I tried it with JSON.parse() but it doesn’t work, it’s probably not supported or I’m using it wrong.
Here is the decoder, it is a self-made device with 4 analogue and 2 digital inputs. The ranges of the analog inputs should be able to be set with the variables.

Who can help? A code example would be helpful.


Hallo zusammen,

ich bin dabei einen decoder zu schreiben der auch die Variablen des Gerätes ausliest. Dies soll ja mit “variables” gehen, ich verstehe allerdings nicht wie man diese dann umsetzt.
Ich habe mehrere Geräte Variablen die ich für eine Berechnung innerhalb des decoders brauche. Ich bin noch nicht sehr gut in Javascript, manche zusammenhänge verstehe ich noch nicht ganz.
Ich hatte es mit JSON.parse() probiert, aber dies funktioniert nicht, diese wird wohl nicht unterstützt oder ich wende diese falsch an.
Hier noch der Decoder, es ist ein Selbstbau Gerät mit 4 Analogen und 2 digitalen Eingängen. Die Bereiche der Analogen Eingänge soll man mit den Variablen einstellen können.

Wer kann helfen? Ein Code Beispiel wäre hilfreich.

// ai*_val, *_max, *_min -> variables from device
function Decode(fPort, bytes, variables) {
	var decoded ={};
        var obj = JSON.parse(variables); // don't work

  	decoded.adc_1 = (((obj.ai1_val-0)/(obj.ai1_max-obj.ai1_min)) * ((bytes[0] << 8 | bytes[1]) - obj.ai1_min));
/*      decoded.adc_2 = (((obj.ai2_val-0)/(ai2_max-ai2_min)) * ((bytes[2] << 8 | bytes[3]) - ai2_min));
      decoded.adc_3 = (((obj.ai3_val-0)/(ai3_max-ai3_min)) * ((bytes[4] << 8 | bytes[5]) - ai3_min));
      decoded.adc_4 = (((obj.ai4_val-0)/(ai4_max-ai4_min)) * ((bytes[6] << 8 | bytes[7]) - ai4_min));
 */     decoded.in_1 = bytes[8];
      decoded.in_2 = bytes[9];

return decoded;
}

You don’t need the JSON.parse(), variables is passed in as an object not a string, so you can just access them as

foo = variables.x;

1 Like

Oh ok, then I thought too complicated.

:thinking: