nodejs调用ffi模块时,返回中文乱码
发布于 9 年前 作者 leese7en 4477 次浏览 最后一次编辑是 8 年前 来自 问答

代码如下: var openPlant = op2.op2_init(0, ‘127.0.0.1’, 8200, 30, ‘sis’, ‘openplant’, null, 0); var sqlPoint = ‘select pn from node’; var result = getResult(openPlant, sqlPoint); console.log(‘result:’ + JSON.stringify(result)); 输出结果:[{“PN”:“UNIT01”},{“PN”:“BV”},{“PN”:“����”},{“PN”:“����”}] 应该输出结果:[{“PN”:“UNIT01”},{“PN”:“BV”},{“PN”:“测试”},{“PN”:“描述”}]

方法加载: var IntArray = ArrayType(int); var string = ref.types.CString; var StringArray = ArrayType(string); var op2 = ffi.Library(’…/lib/opapi2’, { ‘op2_init’: [‘pointer’, [‘int’, ‘string’, ‘int’, ‘int’, ‘string’, ‘string’, ‘string’, ‘int’]], ‘op2_status’: [‘int’, [‘pointer’]], ‘op2_get_system_time’: [‘int’, [‘pointer’, ‘pointer’]], ‘op2_encode_time’: [‘int’, [‘int’, ‘int’, ‘int’, ‘int’, ‘int’, ‘int’]], ‘op2_new_table’: [‘pointer’, [‘string’]], ‘op2_add_column’: [‘int’, [‘pointer’, ‘string’, ‘int’, ‘int’, ‘int’]], ‘op2_new_request’: [‘pointer’, []], ‘op2_set_table’: [‘void’, [‘pointer’, ‘pointer’]], ‘op2_set_option’: [‘void’, [‘pointer’, ‘string’, ‘string’]], ‘op2_add_filter’: [‘void’, [‘pointer’, ‘string’, ‘int’, ‘string’, ‘int’]], ‘op2_get_option’: [‘string’, [‘pointer’, ‘string’, ‘string’, ‘int’]], ‘op2_get_table’: [‘pointer’, [‘pointer’]], ‘op2_set_id_list’: [‘void’, [‘pointer’, ‘int’, IntArray]], ‘op2_set_name_list’: [‘void’, [‘pointer’, ‘int’, StringArray]], ‘op2_get_stream’: [‘pointer’, [‘pointer’]], ‘op2_set_compress’: [‘void’, [‘pointer’, ‘int’]], ‘op2_write_request’: [‘int’, [‘pointer’, ‘pointer’]], ‘op2_flush’: [‘int’, [‘pointer’]], ‘op2_write_content’: [‘int’, [‘pointer’, ‘pointer’]], ‘op2_flush_content’: [‘int’, [‘pointer’]], ‘op2_get_response’: [‘int’, [‘pointer’, ‘pointer’]], ‘op2_get_request’: [‘int’, [‘pointer’, ‘pointer’]], ‘op2_next_content’: [‘int’, [‘pointer’, ‘pointer’, ‘int’, ‘pointer’]], ‘op2_row_count’: [‘size_t’, [‘pointer’]], ‘op2_column_count’: [‘int’, [‘pointer’]], ‘op2_column_name’: [‘string’, [‘pointer’, ‘int’]], ‘op2_set_rowid’: [‘int’, [‘pointer’, ‘size_t’]], ‘op2_column_type’: [‘int’, [‘pointer’, ‘int’]], ‘op2_column_double’: [‘double’, [‘pointer’, ‘int’]], ‘op2_column_string’: [‘string’, [‘pointer’, ‘int’]], ‘op2_column_binary’: [‘void’, [‘pointer’, ‘int’]], ‘op2_column_value’: [‘pointer’, [‘pointer’, ‘int’]], ‘op2_free_value’: [‘void’, [‘pointer’]], ‘op2_free_request’: [‘void’, [‘pointer’]], ‘op2_free_response’: [‘void’, [‘pointer’]], ‘op2_lm_get_uid’: [‘int’, [‘int’, ‘string’, ‘int’]], ‘op2_lm_open’: [‘pointer’, [‘int’, ‘string’, ‘string’, ‘pointer’]], ‘op2_lm_get_type’: [‘int’, [‘pointer’]], ‘op2_lm_get_issue_date’: [‘void’, [‘pointer’, ‘string’, ‘int’]], ‘op2_lm_close’: [‘void’, [‘pointer’]] });

getResult函数: function getResult(openPlant, sql) { var objsArray = new Array(); //创建表 var opTable; var opRequest = op2.op2_new_request(); initRequest.initRequest(op2, opTable, opRequest, sql);

//op2.op2_set_name_list(opRequest, 4, names);
//获取action
var buf = ref.alloc('string');
if (opRequest) {
    var action = op2.op2_get_option(opRequest, 'Action', buf, buf.length - 1);
}
//重新加载table
var opTable2 = op2.op2_get_table(opRequest);
//获取流
var opioTemp = op2.op2_get_stream(openPlant);
op2.op2_set_compress(opioTemp, 1);
//执行request
var rv = op2.op2_write_request(opioTemp, opRequest);

if (rv == 0) {
    if (action == 'SELECT') {
        rv = op2.op2_flush(opioTemp);
    }
    else {
        rv = op2.op2_write_content(opioTemp, opTable2);
        rv = op2.op2_flush_content(opioTemp);
    }
}
if (rv != 0) {
    console.log('write request error');
    return rv;
}
//获取response
var opResponse = ref.alloc(sqlite3PtrPtr);
rv = op2.op2_get_response(opioTemp, opResponse);
if (rv != 0) {
    console.log('get response error');
    return rv;
}
//根据response获取 加载的table
var result = op2.op2_get_table(opResponse.deref());
//遍历table 构造对象
var dval, i, j, count, ncols;
var val;
if (result) {
    var rowObj;
    var eof = ref.alloc('int');
    eof.writeInt32LE(0, 0);
    while ((rv = op2.op2_next_content(opioTemp, result, 0, eof)) == 0 && !(eof.deref())) {
    }
    count = op2.op2_row_count(result);
    ncols = op2.op2_column_count(result);
    for (i = 0; i < count; i++) {
        op2.op2_set_rowid(result, i);
        rowObj = new Object();
        for (j = 0; j < ncols; j++) {
            var name = op2.op2_column_name(result, j);
            var type = op2.op2_column_type(result, j);
            if (type == opType.vtFloat || type == opType.vtDouble || type == opType.vtObject) {
                dval = op2.op2_column_double(result, j);
                rowObj[name] = dval;
            }
            else {
                val = op2.op2_column_string(result, j);
                rowObj[name] = val;
            }
        }
        objsArray.push(rowObj);
    }
}
if (rv != 0) {
    console.log('get content error:' + rv);
}
else {
    console.log('total %d record', op2.op2_row_count(result));
}
op2.op2_free_request(opRequest);
op2.op2_free_response(opResponse.deref());
return objsArray;

}

代码要实现的功能:连接实时数据库,查询所有的节点获取点名,暂时测出只有中文才有乱码,调用的是c的dll。

1 回复

格式一下代码

回到顶部