Comments (5)
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.
@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.
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:
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.
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.
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)
- im noob HOT 6
- Dispatch to typedef based on context? HOT 3
- ios write blob to file HOT 3
- Type Caching and Memory Use HOT 3
- Question: How to convert the "uint16" value to half-precision float (i.e. "float16")? HOT 1
- Dynamic Types? HOT 2
- jBinary::readAll does not mutate the internal pointer HOT 3
- writeAll() method documentation and functionality HOT 3
- corsproxy.com redirects to dubious web sites HOT 1
- SaveAsSync? HOT 2
- BROWSER vs NODE differences HOT 3
- Change Endianness (question) HOT 4
- "index out of range" with dynamic array size HOT 5
- Just don't work, maybe it caused by electron? :-{ HOT 1
- Local build is failing HOT 3
- Remove old polyfills
- Switch to ESM+Rollup
- How to read a string HOT 8
- reproducable out-of-memory errors using 'array'
- Strings ignores length on writing
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jbinary.