Node FFi accessing CTAPI

161 Views Asked by At

I have the CT-API header that looks like this:

char LINKAGE CT_init (
      CTAPI_USHORT Ctn,                  /* Terminal Number */
      CTAPI_USHORT pn                    /* Port Number */
      );

char LINKAGE CT_close(
      CTAPI_USHORT Ctn                  /* Terminal Number */
      );                 

char LINKAGE CT_data( 
       CTAPI_USHORT ctn,                /* Terminal Number */
       unsigned char  *dad,               /* Destination */
       unsigned char  *sad,               /* Source */
       CTAPI_USHORT lc,                 /* Length of command */
       unsigned char  *cmd,               /* Command/Data Buffer */
       unsigned short *lr,                /* Length of Response */
       unsigned char  *rsp                /* Response */
       );

When I call CT_init from my js code it works fine, but, when trying to call CT_data, I get an invalid data error.

This is my NodeJS:

var ffi = require('ffi');
var ref = require('ref');

var libm = ffi.Library("C:\\Windows\\System32\\CTPCSC31x64.dll", {
  'CT_init' :  [ ref.types.char, [ ref.types.uint, ref.types.uint ] ],
  'CT_close': [ ref.types.char, [ ref.types.ushort ] ],
  'CT_data' : [ref.types.char, [
      ref.types.ushort, 
      ref.refType(ref.types.uchar), 
      ref.refType(ref.types.uchar), 
      ref.types.ushort, 
      ref.refType(ref.types.uchar), 
      ref.refType(ref.types.ushort), 
      ref.refType(ref.types.uchar)
  ]]
});


const command = new Buffer([0x20, 0x10, 0x00, 0x00, 0x00]);
const response = ref.alloc(ref.types.uchar)
const dest =  new Buffer([0x02]);
const src =  new Buffer([0x01]);
const response_l= new Buffer([0xFF]);

try {
    console.log(JSON.stringify(command), JSON.stringify(dest), JSON.stringify(src), JSON.stringify(response));
    console.log(libm.CT_init(0, 0));
    console.log(libm.CT_data(0, dest, src, command.length, command , response_l, response));
    console.log(libm.CT_close(0));
} catch(error) {
    console.log(error);
}
0

There are 0 best solutions below