• For one, you're trying to manually handle endian conversion, and you've ended up tying yourself in knots. Funnily enough, I'm pretty sure your checksum routine is getting the right answer!

    First: the CASIC packet format is 0xBA,0xCE,2-byte payload length,class,id,payload,uint32 checksum. All little-endian. The example packet you have in the comment right above your test() function has this backwards.

    So for your example packet (class=5, id=1, payload=[6,0,0,0]) it should look like BA,CE,04,00,05,01,06,00,00,00. The checksum bytes at the end are 0A,00,05,01. And that is 0x0105000A.

    function casic_checksum(id, cl, payload) {
      let buf = new Uint8Array(4+payload.length);
    
      // Varying field sizes is what DataView is for
      let dv = new DataView(buf.buffer);
      dv.setUint16(0,payload.length,true);
      dv.setUint8(2,cl);
      dv.setUint8(3,id);
    
      buf.set(Uint8Array(payload),4);
    
      // jazz hands
      return Uint32Array(buf.buffer).reduce((a,b) => a+b, 0);
    }
    
    function test() {
      let c;
    
      c = casic_checksum(1, 5, [6, 0, 0, 0]);
      print("test1 - expecting 0x0105000A");
      print(c.toString(16).padStart(8,'0'));  
      print("");
      print("");
      print("");
    
      c = casic_checksum(0, 6, [1, 7, 192, 8, 0, 194, 1, 0]);
      print("test2 - expecting 0x08c7c909");
      print(c.toString(16).padStart(8,'0'));
    }
    
    
About

Avatar for pelrun @pelrun started