TooTallNate / ref-struct

Create ABI-compliant "struct" instances on top of Buffers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

C# FixedBuffer struct into NodeJS

roosht3 opened this issue · comments

C# Struct

public struct DB_TRAIN_HEDAER{
        [FixedBuffer(typeof(sbyte), 3)]
        public DB_TRAIN_HEDAER.<dateStart>e__FixedBuffer19 dateStart;

        [FixedBuffer(typeof(sbyte), 3)]
        public DB_TRAIN_HEDAER.<timeStart>e__FixedBuffer1a timeStart;


        [StructLayout(LayoutKind.Sequential, Size = 3)]
        public struct <timeStart>e__FixedBuffer1a{
            public sbyte FixedElementField;
        }

        [StructLayout(LayoutKind.Sequential, Size = 3)]
        public struct <timeStart>e__FixedBuffer1a
        {
            public sbyte FixedElementField;
        }
}

NodeJS Struct

var dateStart =  StructType({
    FixedElementField: ref.types.char
});


var timeStart =  StructType({
    FixedElementField: ref.types.char
});



var TRAIN_HEADER =  StructType({
    dateStart: dateStart,
    timeStart: timeStart,
});

I am pretty new to byte data structures. I am trying to read a device using serial port communication. so far I was able to convert the entire structure into ref-struct. I have a Fixed Buffer field which sets 3 char bytes in sequential order. When I access access by buffer casted to TRAIN_HEADER struct, I encounter a minor problem.

Buffer Reference

b[0] = 120,
b[1] =1, 
b[2] = 80, 
b[3] = 16, //Year
b[4] = 5, //Month
b[5] = 30,  //Date
b[6] = 10, //Hour
b[7] = 16,  //Minute
b[8] = 17,  //Seconds

Accessing Buffer

let add = 3;
let currentActivityPtr =  ref.reinterpret(ptr, structSize, add);
currentActivityPtr.type = TRAIN_HEADER;
let currentActivity = currentActivityPtr.deref();

let year = 2000 + currentActivity.dateStart.FixedElementField; // correct value
let month_date =  currentActivity.dateStart.ref()[1] + "-" + currentActivity.dateStart.ref()[1]; // correct values
console.log(year + " - " +month_date); //prints - 2016-5-30 //Totally Correct 
let time = currentActivity.timeStart.FixedElementField // incorrect value b[4] is stored here instead of b[6]

how do I correct this issue @TooTallNate

Issue has been resolved. C# compiles sbyte to a nested struct.

var ArrayType = require("ref-array");
var sbyte = ArrayType(ref.types.char, 3);

PS: Explicit struct form C# is equal to ref-union.