GithubHelp home page GithubHelp logo

Array of Struct about jbinary HOT 5 CLOSED

jdataview avatar jdataview commented on July 29, 2024
Array of Struct

from jbinary.

Comments (5)

RReverser avatar RReverser commented on July 29, 2024

For reading it as an array, the below code does not do the job

What you're getting as a result? Can you put the code and sample file together in some repo I could take a look at?

from jbinary.

psamim avatar psamim commented on July 29, 2024

@RReverser Thank for the quick reply! I appreciate it. The code is as above but as requested I put it in the a repo along with the binary file and its C header. I get no results and no errors.

https://github.com/psamim/jbinary-test/blob/master/index.js

from jbinary.

RReverser avatar RReverser commented on July 29, 2024

First mistake - I see that C version defines DATE_t and TIME_t as unions of u32 and struct (that is, they both point to the same data), while you define them as consequent fields in JS version, which makes them reading 2 times more bytes than needed, and then everything goes wrong. I guess you don't really care about u32 part, so can just drop it and leave fields inside of structure like following:

var TIME_T = {
      hour: 'int16',
      min: 'int8',
      type: 'int8'
};

var DATE_T = {
      day: 'int8',
      month: 'int8',
      year: 'int16'
};

Second - if I understand correctly, in C, you're reading structures by simply mapping them onto the memory region. The problem that happens is that in C, structures are automatically aligned (not packed) by default for performance reasons, while jBinary does not make any assumptions about the behavior of the underlying data types. If you compile your C version with -Wpadded, you can see where compiler adds alignment bytes, and replicate them in your data structures explicitly using skip type:

screen shot 2016-07-21 at 14 22 46

    LOG_T: {
        time: 'TIME_T', // time of log
        date: 'DATE_T', // date of log
        temp: 'float', // chamber temperature
        tempSp: 'float', // temperature set point
        tmpRamp: 'float', // temperature ramp
        hum: 'float', // chamber humidity
        humSp: 'float', // humidity set point
        light: 'uint8', // chamber light
        fan: 'uint8', // chamber fan
        _align1: ['skip', 2], // simulating C alignment
        tmpMax: 'float', // maximum temperature allowed
        tmpMin: 'float', // minimum temperature allowed
        humMax: 'float', // maximum humidity allowed
        humMin: 'float', // minimum humidity allowed
        roomTemp: 'uint8', // room temperature
        _align2: ['skip', 1], // simulating C alignment
        alarmStatus: 'uint16' // alarm status (refer to alarm mask defines)
    }

Finally, looks like you're assuming little-endian byte order (most common on x86 CPUs). If so, you need to add 'jBinary.littleEndian': true to your typeset.

With these fixes, everything should work fine. I played a bit with your code and you can find version with those modifications here: https://gist.github.com/RReverser/bf31bfd30a09362c95a78894a73d359b

Good luck! :)

from jbinary.

psamim avatar psamim commented on July 29, 2024

Thanks for taking your time and this complete answer. I did not know about the concepts you mentioned above. Your code works as expected.

May I ask what is the program in the screenshot, that indicated the alignments as warnings? How did you find them?

from jbinary.

RReverser avatar RReverser commented on July 29, 2024

May I ask what is the program in the screenshot, that indicated the alignments as warnings? How did you find them?

Sure. I wrote above:

If you compile your C version with -Wpadded

So you can just pass this as an argument to gcc / clang when compiling C version, and it will report those paddings as warnings. Specific editor doesn't really matter.

from jbinary.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.