Last week, using the underlying principle of Linux, successfully realized GPIO control on pcDuino .If use the same approach on PWM, ADC serial port , we found that it is very difficult.This week we begin to using jna, call dynamic link library, to complete the pcDuino control.
There are 5 steps for Java call dynamic link library-jna (2):
1. Create test.c file
#include"stdio.h"
int add(int a,int b);
int add(int a,int b){
return a+b;
}
2. Compiler to generate dynamic
gcc test.c -fPIC -shared -o libtest.so
3. Create the project test on the eclipse of pcDuino , import the download jna.jar package, create a class JNA_test, write code
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;
public class JNA_test
{
//\u5b9a\u4e49\u63a5\u53e3CLibrary\uff0c\u7ee7\u627f\u81eacom.sun.jna.Library
public interface CLibrary extends Library
{
//\u5b9a\u4e49\u5e76\u521d\u59cb\u5316\u63a5\u53e3\u7684\u9759\u6001\u53d8\u91cf
CLibrary Instance=(CLibrary)Native.loadLibrary("test",CLibrary.class);
//printf\u51fd\u6570\u58f0\u660e
//void printf(String str,Object... args);
int add(int a,int b);
}
public static void main(String[] args)
{
int sum= CLibrary.Instance.add(1,2);
System.out.println("1+2="+sum);
}
}
4. Just copy the generated files to the project libtest, there was an error.
5. Run the project, there was an erroe, search information, and then download the latest Jna https://java.net/projects/jna/downloads/, run again, remain unusual, but abnormal information information has changed
Exception in thread "main" java.lang.UnsatisfiedLinkError: Can't obtain updateLastError method for class com.sun.jna.Native
at com.sun.jna.Native.initIDs(Native Method)
at com.sun.jna.Native.<clinit>(Native.java:139)
at JNA_test$CLibrary.<clinit>(JNA_test.java:13)
at JNA_test.main(JNA_test.java:23)

Leave a Reply
You must be logged in to post a comment.