基于ndk jni技术实现串口收发的方法

6 篇文章 0 订阅
步骤:
1、写java jni类;

2、javah生成.h,实现对应.c文件;

3、写java activity类测试。

参考代码:

//*******************java jni类*************************//
package com.example.hellojni;


public class JniInterface {

	static {
       System.loadLibrary("hello-jni");//可装载java.library.path包含路径下的so
       // System.load("/sdcard/mylib/libhello-jni.so");//可装载任何绝对路径的so
    }

    //串口初始化
    public static native void CommInit();  
   
    //串口收发
    public static native int ComSendRecv(byte[] inBuf,int nInLen,byte[] outBuf);
    
    public static native void nativeInit();

    //下面两个是测试jni里面调用java方法
    public static native void nativeExecution();

    private  static void DispInfo(int value)
   {
    	System.out.println("this is called by c value:"+value);
		//Toast.makeText(mainContext, "this is from c:"+value,Toast.LENGTH_LONG).show();
    } 
}
//*********************javah生成.h****************************//
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_hellojni_JniInterface */

#ifndef _Included_com_example_hellojni_JniInterface
#define _Included_com_example_hellojni_JniInterface
#ifdef __cplusplus
extern "C" {
#endif

/*
 * Class:     com_example_hellojni_JniInterface
 * Method:    CommInit
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_example_hellojni_JniInterface_CommInit
  (JNIEnv *, jobject);

/*
 * Class:     com_example_hellojni_JniInterface
 * Method:    ComSendRecv
 * Signature: ([BI[B)I
 */
JNIEXPORT jint JNICALL Java_com_example_hellojni_JniInterface_ComSendRecv
  (JNIEnv *, jobject, jbyteArray, jint, jbyteArray);

/*
 * Class:     com_example_hellojni_JniInterface
 * Method:    nativeInit
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_example_hellojni_JniInterface_nativeInit
  (JNIEnv *, jobject);

/*
 * Class:     com_example_hellojni_JniInterface
 * Method:    nativeExecution
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_example_hellojni_JniInterface_nativeExecution
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif
//*********************实现对应.c****************************//
#include"com_example_hellojni_JniInterface.h"

void Java_com_example_hellojni_JniInterface_CommInit(JNIEnv* env,jobject thiz)
{
	Comm_Init();//这里面就是linux c串口初始化代码,如打开串口,设置波特率,阻塞等
}

int _ComSendRecvPack(unsigned char *psInBuf,unsigned int nInLen,unsigned char *psOutBuf)
{
	unsigned char ucmRetCmdNo[2];
	int i=0;
	int nTagCount=0;//tag个数
		
	//发送
	m_SendBufLen = nInLen;
	memset(m_SendBuf,0,sizeof(m_SendBuf));
	memcpy(m_SendBuf,psInBuf,nInLen);

	ComSend();//里面是串口write

	//接收	
	ComRecv();//里面是串口read
	memcpy(psOutBuf,m_RecvBuf,m_RecvBufLen);
	
	return m_RecvBufLen;

}
//串口封装给JNI
int Java_com_example_hellojni_JniInterface_ComSendRecv(JNIEnv* env,jobject thiz,jbyteArray psInBuf,int nInLen,jbyteArray psOutBuf)
{	
	jbyte native_Inbuf[1024];
	jbyte native_Outbuf[1024];
	jint nlen = 0;

	//类型转换java byte -> c buf
	(*env)->GetByteArrayRegion(env,psInBuf,0,nInLen,native_Inbuf);
	
	nlen = _ComSendRecvPack(native_Inbuf,nInLen,native_Outbuf);

	//c buf -> java byte	
	(*env)->SetByteArrayRegion(env,psOutBuf,0,nlen,native_Outbuf);
	
	return nlen;
}
jmethodID gOnNativeID;
JNIEnv* genv;
jobject mObject;
jclass mClass;
JavaVM* gs_jvm;

//native初始化函数
void Java_com_example_hellojni_JniInterface_nativeInit(JNIEnv* env,jobject thiz)
{

	printf("CalledForJni_nativeInit................");
	
	//获取类
	jclass clazz = (*env)->FindClass(env,"com/example/hellojni/JniInterface");
	if(clazz==NULL)
	{
		printf("clazz IS NULL................");
		return;
	}
	//mClass = (jclass)(*env)->NewGlobalRef(env,clazz);//永久保存mClass

	mObject = (jobject)(*env)->NewGlobalRef(env,thiz);
	//获取方法ID
	gOnNativeID = (*env)->GetStaticMethodID(env,clazz,"DispInfo","(I)V");	
	if(gOnNativeID==NULL)
	{
		printf("gOnNativeID IS NULL................");
		return;
	}
	//得到JAVA VM,为了在其他没有传env参数的函数获取env调用java方法
	(*env)->GetJavaVM(env,&gs_jvm); 
	printf("CalledForJni_nativeInit end................");

}
void Java_com_example_hellojni_JniInterface_nativeExecution(JNIEnv* env,jobject thiz)
{
	printf("nativeExecution................");
	(*env)->CallStaticVoidMethod(env,mObject,gOnNativeID);
	(*env)->DeleteGlobalRef(env,mObject);
	return;
}

//*********************activity部分****************************//
JniInterface.CommInit();

serialtestbutton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {				
				//Log.i(HelloJni.ACTIVITY_TAG,"serialtestbutton clicked================");
				
				
				Log.i(HelloJni.ACTIVITY_TAG,"inBuf:"+nInLen+":"+ByteTool.getHexString(inBuf));
				
				Log.i(HelloJni.ACTIVITY_TAG,"before send");				
				nOutLen = JniInterface.ComSendRecv(inBuf, nInLen, outBuf);
				Log.i(HelloJni.ACTIVITY_TAG,"after recv");
				
				byte[] temp = new byte[nOutLen];				
				System.arraycopy(outBuf, 0, temp, 0, nOutLen);
				Log.i(HelloJni.ACTIVITY_TAG,"outBuf:"+nOutLen+":"+ByteTool.getHexString(temp));
			}
			});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值