使用无符号字符缓冲器淋巴结FFI(Using unsigned char buffer with n

2019-10-21 07:10发布

我使用的是缓冲与节点FFI调用下面的库函数有问题:

int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size) { ... }

http://www.intra2net.com/en/developer/libftdi/documentation/group__libftdi.html#ga72d87e30015c98bd0be22e7c8c873345

我已经看到使用通过使“无符号字符的buf [大小]”到作为参数buf ..

因此,与FFI我试试这个:

ffi = require 'ffi'
ref = require 'ref'

ftdiContext = ref.types.void
ftdiContextPtr = ref.refType ftdiContext

ftdi = ffi.Library 'libftdi',
  'ftdi_init': [ 'int', [ ftdiContextPtr ] ]
  'ftdi_usb_open': [ 'int', [ ftdiContextPtr, 'int', 'int' ] ]
  'ftdi_usb_open_desc': [ 'int', [ ftdiContextPtr, 'int', 'int', 'string', 'string'] ]
  'ftdi_get_error_string': [ 'string', [ ftdiContextPtr ] ]
  'ftdi_set_bitmode': [ 'int', [ ftdiContextPtr, 'uchar', 'uchar' ] ]
  'ftdi_set_baudrate': [ 'int', [ ftdiContextPtr, 'int' ] ]
  'ftdi_read_data': [ 'int', [ ftdiContextPtr, ref.refType(ref.types.uchar), 'int' ] ]

BITMODE_RESET  = 0x00
BITMODE_BITBANG= 0x01
BITMODE_MPSSE  = 0x02
BITMODE_SYNCBB = 0x04
BITMODE_MCU    = 0x08
BITMODE_OPTO   = 0x10
BITMODE_CBUS   = 0x20
BITMODE_SYNCFF = 0x40

inputPin = 1
outputPin = 2

rxBaudRate = 9600
txBaudRate = 4500

rxBufSize = 10

buf = new Buffer rxBufSize

ctx = ref.alloc ftdiContextPtr

console.log ftdi.ftdi_init ctx
console.log ftdi.ftdi_usb_open_desc ctx, 0x0403, 0x6001, null, 'FTG4GJL1'
console.log ftdi.ftdi_set_bitmode ctx, inputPin << outputPin , BITMODE_BITBANG
console.log ftdi.ftdi_set_baudrate ctx, rxBaudRate
console.log ftdi.ftdi_read_data ctx, buf, buf.length
console.log ftdi.ftdi_get_error_string ctx

但结果我得到的是:

-22
usb bulk read failed

该ftdi_read_data C函数进行调用以usb_bulk_read(从libusb的)这需要“字符*”,因为它的缓冲区的说法 - 我不知道这是否是影响任何东西..

http://www.intra2net.com/en/developer/libftdi/documentation/ftdi_8c_source.html#l01749

Answer 1:

好像它失败,因为你不能只用无效的情况下给它:

ret = libusb_bulk_transfer (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, &actual_length, ftdi->usb_read_timeout);
if (ret < 0)
  ftdi_error_return(ret, "usb bulk read failed");

ftdi应该是结构http://www.intra2net.com/en/developer/libftdi/documentation/ftdi_8h_source.html#l00219

尝试使用ref-struct库这一点。



文章来源: Using unsigned char buffer with node-ffi