Android的Java的JNI和C字符数组无法识别的价值 [英] Android Java JNI and C Char array Can't recognize the value

查看:150
本文介绍了Android的Java的JNI和C字符数组无法识别的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图把字符数组从Java传递到c。我用Google搜索来实现它的方式。但是,当我试图访问数组的值,该值是不正确的。

其实,我试着给数组的字符。就像缓冲[I] ='Z',结果是正确的机器人。

顺便说一句,我想要的值由RS232发送的值。从RS232的价值是确定的。

有人告诉我使用的ByteArray。将它的工作?我害怕从RS232值不能存储到ByteArray ....

的Java

 公共本土INT OpenPort(INT端口编号,诠释贝特类);
公共本土INT ClosePort(INT端口编号);
公共本土INT READ(的char []的D​​ataBuffer);公众的char []缓冲区=新的char [40];INT I;对于(i = 0; I< 40;我++)
    缓冲区[i] ='S'; //初始化OpenPort(16,9600); //一个函数打开RS232端口而(ⅰ&下; 1000)
{
    READ(缓冲液);
    我++;
}

JNI.c

  JNIEXPORT jint JNICALL Java_com_example_ndk_Vehicles_READ(JNIEnv的* ENV,JCLASS
CLS,jcharArray的DataBuffer)
{    TMP的char [40];
    jchar *缓冲区=(* ENV) - GT; GetCharArrayElements(ENV,DataBuffer中,0);
    memset的(TMP,0,sizeof的(TMP));
    PollComport(16,TMP,40); //将这里的问题?此功能需要一个unsigned char型[]
                            //,但在C程序我写的,的char []是确定这一点。
    的memcpy(缓冲器,TMP,40);    (* ENV) - > ReleaseCharArrayElements(ENV,DataBuffer中,缓冲液,0);    返回0;
}


解决方案

由于GetCharArrayElements()允许使Java数组的副本,交给你一个指针,而不是复制。在您的code,你传入空的指针到布尔参数,将告诉你,如果一个副本制作:

  jchar *缓冲区=(* ENV) -  GT; GetCharArrayElements(ENV,DataBuffer中,0);

所以你很可能在当您完成这被丢弃一份工作。相反GetCharArrayElements(),操作用C本地阵列++和使用SetCharArrayRegion()上复制它们。的

I tried to pass the char array from java to c. I have googled the way to implement it. But when I tried to access the value of the array, the value was not correct.

In fact, I tried to give the array a character. Just Like buffer[i]='z', and the result is correct in android.

Btw, the value I wanted was the value sent by the rs232. The value from the rs232 was ok.

Someone told me to use the bytearray. Would it work? I was afraid of that the value from rs232 could not be store into the bytearray....

Java

public native int OpenPort(int portnum,int brates);
public native int ClosePort(int portnum);
public native int READ(char[] databuffer);

public char[] buffer=new char[40];

int i;

for(i=0;i<40;i++)
    buffer[i]='s';  //initialization

OpenPort(16,9600); // A function to open the rs232 port

while (i<1000)
{
    READ(buffer);
    i++;
}

JNI.c

JNIEXPORT jint JNICALL Java_com_example_ndk_Vehicles_READ(JNIEnv *env, jclass
cls,jcharArray databuffer)
{

    char tmp[40];
    jchar *buffer=(*env)->GetCharArrayElements(env,databuffer,0);
    memset(tmp,0,sizeof(tmp));
    PollComport(16,tmp,40); //will the problem here ? This function need a unsigned char[]
                            //,but in the c program I wrote , char[] was ok for this.
    memcpy(buffer,tmp,40);

    (*env)->ReleaseCharArrayElements(env, databuffer, buffer, 0);

    return 0;
}

解决方案

Because GetCharArrayElements() is allowed to make a copy of the Java array and hand you a pointer to the copy instead. In your code, you are passing in null for the pointer-to-boolean argument that would tell you if a copy was made:

jchar *buffer=(*env)->GetCharArrayElements(env,databuffer,0);

So you are likely operating on a copy which is discarded when you are done. Instead of GetCharArrayElements(), operate on your local array in C++ and use SetCharArrayRegion() to copy them.

这篇关于Android的Java的JNI和C字符数组无法识别的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆