`
ai_longyu
  • 浏览: 477854 次
社区版块
存档分类
最新评论

can 总线移植--------代码

 
阅读更多

JNI:

#include <jni.h>
#include <stdio.h>
#define LOG_TAG "FLEXCAN"
#include <android/log.h>




JNIEXPORT jobject JNICALL
Java_com_liujun_can_dump(JNIEnv *env,jobject obj,
jobject frame)
{
/*
struct frame_struct {
long can_id;
int dlc;
jchar buf[8];
};


frame_struct *mFrame ;


mFrame = (frame_struct *)malloc(sizeof(frame_struct));
*/
jint data[] = {11,22,33,44,55};
jint can_id = 10;
jint dlc = 5;
// mFrame->buf = data;

jclass frame_cls = (*env)->FindClass(env,
"com/liujun/frame");
if(frame_cls == NULL)
return NULL;
jmethodID setID = (*env)->GetMethodID(env,frame_cls,
"setID","(I)V");
if(setID == NULL)
return NULL;
jmethodID setBuf = (*env)->GetMethodID(env,frame_cls,
"setBuf","([I)V");
if(setBuf == NULL)
return NULL;


jobject myFrame = frame;
if(myFrame == NULL)
return NULL;

(*env)->CallVoidMethod(env,myFrame,setID,can_id);
jintArray arr;
arr = (*env)->NewIntArray(env,5);
if(arr == NULL)
return NULL;
(*env)->SetIntArrayRegion(env,arr,0,5,data);
__android_log_print(ANDROID_LOG_INFO,"JNI","11111");
(*env)->CallVoidMethod(env,myFrame,setBuf,arr);
__android_log_print(ANDROID_LOG_INFO,"JNI","22222");
(*env)->DeleteLocalRef(env,arr);
return myFrame;




}




JNIEXPORT jint JNICALL
Java_com_liujun_can_send(JNIEnv *env,jobject obj,jint dlc, jintArray arr)
{
int BUF_SIZE = (int)dlc;
jint i;
jint buf[BUF_SIZE];
__android_log_print(ANDROID_LOG_INFO,"JNI","buf size : %d\n",BUF_SIZE);
(*env)->GetIntArrayRegion(env, arr, 0, BUF_SIZE, buf);
for(i=0;i<BUF_SIZE;i++)
__android_log_print(ANDROID_LOG_INFO,"JNI","buf[%d] = %d",(int)i,(int)(buf[i]));
return 0;
}


jint JNI_OnLoad(JavaVM* vm,void* reserved)
{
void *venv;
// LOGI("JNI_OnLoad!");


if((*vm)->GetEnv(vm, (void**)&venv, JNI_VERSION_1_4) != JNI_OK) {
// LOGE("ERROR: GetEnv failed");
return -1;
}


return JNI_VERSION_1_4;
}

java

package com.liujun;


import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


public class can extends Activity {
private static final String TAG = "can";
private Button bt_send = null;
private Button bt_dump = null;
private TextView tv = null;
private Context mContext = null;
private frame mFrame ;
static {
System.loadLibrary("can");
}

private native int send(int dlc, int buf[]);
private native frame dump(frame mFrame);



/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mContext = this;

bt_send = (Button)findViewById(R.id.send);
bt_dump = (Button)findViewById(R.id.dump);
tv = (TextView)findViewById(R.id.textView);

bt_send.setOnClickListener(new MyButtonListener());
bt_dump.setOnClickListener(new MyButtonListener());
}

class MyButtonListener implements OnClickListener{


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==R.id.send){
Toast.makeText(mContext, "sned!!", Toast.LENGTH_SHORT).show();
int buf[] = {1,2,3,4,5};
send(buf.length,buf);

}
if(v.getId()==R.id.dump){
Toast.makeText(mContext, "dump!!", Toast.LENGTH_SHORT).show();
mFrame = new frame();
Log.d(TAG, "3");
mFrame = dump(mFrame);
// int data[] = {11,22,33,4,55};
// mFrame.setBuf(data);
if(mFrame == null)
Log.d(TAG, "4");
Log.d(TAG, "1");
int _data[] = mFrame.getBuf();
Log.d(TAG, "2");


tv.setText("id: "+mFrame.getID()+
"\n1."+_data[0]+"\n2."+_data[1]+
"\n3."+_data[2]+"\n4."+_data[3]+
"\n5."+_data[4]+"\n6."+_data[5]+
"\n7."+_data[6]+"\n8."+_data[7]);
}
}
}
}


下载:

http://download.csdn.net/detail/zhangjie201412/4170702

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics