主题 : jni使用i2c总线访问设备的问题 复制链接 | 浏览器收藏 | 打印
天下古今之庸人,皆因一惰字致败;天下古今之人才,皆因一傲字致败。
级别: 新手上路
UID: 52874
精华: 0
发帖: 15
金钱: 75 两
威望: 15 点
综合积分: 30 分
注册时间: 2011-07-27
最后登录: 2011-11-25
楼主  发表于: 2011-11-07 16:25

 jni使用i2c总线访问设备的问题

诸位,有问题如下:要在android中通过jni调用i2c总线驱动,并通过i2c对设备进行简单的读写操作,主要代码如下:
i2cActivity.java:
//主要代码,一些声明什么的我都省略了。
package ....;
import ......;
//openB是我设置的一个button,点击它可以成功打开i2c总线
openB.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
int i = i2c.openI2C();
......
}
});

writeB.segOnClickListener(new View.OnClickListener(){
public void onClick(View v){
int buf[] = new int [4];
buf[0]=0x03;
buf[1]=0x13;
buf[2]=0x03;
buf[3]=0x13;
int len = 4;
int i = i2c.write(buf,len);//这里的i2c变量是我在前面声明的i2cCtl类型的变量
if(i != 1){openT.setText("writeToI2c Failed.");}
}
})

i2cCtl.java:
    package com.i2cCtl;
    import .......;

    public native int openI2C();
    public native int write(int[] buf,int len);
    public native int close();

    static{System.loadLibrary("i2c-jni");}


i2c-jni.c
    #include <string.h>
    #include ......//这里包含了包括jni.h, fcntl.h, stdio.h, stdlib.h , unistd.h , linux/i2c.h的头文件

  int fd;
   jint Java_com_i2cCtl_i2cCtl_openI2C(JNIEnv* env,jobject thiz)
{
fd = open("/dev/i2c-1",O_RDWR);
}

jint Java_com_i2cCtl_i2cCtl_write(JNIEnv* env,jobject thiz,jint buf[],jint len)
{
jint *bufInt;
char *bufByte;
(*env)->GetIntArrayRegion(env,buf,0,len,bufInt);
for(int i = 0;i<len;i++)
{
bufByte = bufInt;
}
ioctl(fd,I2_TENBIT,0);
ioctl(fd,I2C_SLAVE,0x58);

write(fd,bufByte,len);
return 1;
}

.........


执行应用程序的时候,可以打开i2c设备节点,open是可以执行的,但执行write指令的时候,就会提示“应用程序I2CTest意外终止,请重试”的错误,请各位做过的大侠给看看,代码本身是不是有什么硬伤。我是全手打的代码,可能会有些手误,但在eclipse里自动生成的代码,不会有低级错误。。。。
*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
1楼  发表于: 2011-11-07 16:55
用logcat看看?如果是Native进程出现crash的话,debuggerd应该会输出相应的信息的。
"If you have an apple and I have an apple and we exchange apples, then you and I will
still each have one apple. But if you have an idea and I have an idea and we exchange
these ideas, then each of us will have two ideas."
天下古今之庸人,皆因一惰字致败;天下古今之人才,皆因一傲字致败。
级别: 新手上路
UID: 52874
精华: 0
发帖: 15
金钱: 75 两
威望: 15 点
综合积分: 30 分
注册时间: 2011-07-27
最后登录: 2011-11-25
2楼  发表于: 2011-11-07 20:06

 回 1楼(kasim) 的帖子

图片:
恩,刚才找了一下log里的信息,只要执行write指令,就会弹出那个图中的信息。
*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
3楼  发表于: 2011-11-07 22:31
我对JNI不熟,不过我想你可以google一下"No implementation found for native [com/i2cCtl/I2cCtl;.write ..."这种错误
"If you have an apple and I have an apple and we exchange apples, then you and I will
still each have one apple. But if you have an idea and I have an idea and we exchange
these ideas, then each of us will have two ideas."
天下古今之庸人,皆因一惰字致败;天下古今之人才,皆因一傲字致败。
级别: 新手上路
UID: 52874
精华: 0
发帖: 15
金钱: 75 两
威望: 15 点
综合积分: 30 分
注册时间: 2011-07-27
最后登录: 2011-11-25
4楼  发表于: 2011-11-08 09:48

 回 3楼(kasim) 的帖子

恩,好的。谢谢你啊,我再去google一下。
天下古今之庸人,皆因一惰字致败;天下古今之人才,皆因一傲字致败。
级别: 新手上路
UID: 52874
精华: 0
发帖: 15
金钱: 75 两
威望: 15 点
综合积分: 30 分
注册时间: 2011-07-27
最后登录: 2011-11-25
5楼  发表于: 2011-11-20 15:32
恩,已经解决了。是自己对于JNIEnv不了解。其实类型转换没有那么麻烦的。呵呵。
级别: 新手上路
UID: 79428
精华: 0
发帖: 13
金钱: 70 两
威望: 14 点
综合积分: 26 分
注册时间: 2012-10-08
最后登录: 2013-05-02
6楼  发表于: 2012-12-26 17:05
read.......