建人所聞
人生如夢 一尊還酹江月
2022年4月24日 星期日
2018年8月3日 星期五
2014年6月13日 星期五
用C#開發 多執行緒的陽春Web Server
2014年4月8日 星期二
JNI 呼叫C, 由C控制 DHT11讀取溫濕度 (完成版)
其實上次的錯誤有兩個原因, 那天躺到床上時, 忽然就想到了 XD
1. 權限問題, 要用 sudo java -Djava.library.path=. JavaDHT 來執行
2. 程式碼問題, 我忘記要先呼叫 bcm2835_init()
其他不多說, 看Code就行了....
1. 權限問題, 要用 sudo java -Djava.library.path=. JavaDHT 來執行
2. 程式碼問題, 我忘記要先呼叫 bcm2835_init()
其他不多說, 看Code就行了....
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pi@raspberrypi ~/jni $ javac JavaDHT.java | |
pi@raspberrypi ~/jni $ javah -jni JavaDHT | |
pi@raspberrypi ~/jni $ gcc -I /opt/java/jdk1.7.0_21/include/ -I /opt/java/jdk1.7.0_21/include/linux/ JavaDHT.c -l bcm2835 -std=gnu99 -o JavaDHT | |
pi@raspberrypi ~/jni $ sudo ./JavaDHT 11 15 | |
Using pin #15 | |
Data (40): 0x23 0x0 0x1a 0x0 0x3d | |
Temp = 26 *C, Hum = 35 % | |
Result Str: Temp = 26 *C, Hum = 35 % | |
pi@raspberrypi ~/jni $ gcc -shared -I /opt/java/jdk1.7.0_21/include/ -I /opt/java/jdk1.7.0_21/include/linux/ JavaDHT.c -l bcm2835 -std=gnu99 -o libJavaDHT.so | |
pi@raspberrypi ~/jni $ sudo java -Djava.library.path=. JavaDHT | |
Data (40): 0x23 0x0 0x1a 0x0 0x3d | |
Temp = 26 *C, Hum = 35 % | |
result=Temp = 26 *C, Hum = 35 % | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// How to access GPIO registers from C-code on the Raspberry-Pi | |
// Example program | |
// 15-January-2012 | |
// Dom and Gert | |
// | |
// Access from ARM Running Linux | |
#define BCM2708_PERI_BASE 0x20000000 | |
#define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) /* GPIO controller */ | |
#include <jni.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include <dirent.h> | |
#include <fcntl.h> | |
#include <assert.h> | |
#include <unistd.h> | |
#include <sys/mman.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <sys/time.h> | |
#include <bcm2835.h> | |
#include <unistd.h> | |
#include "JavaDHT.h" | |
#define MAXTIMINGS 100 | |
//#define DEBUG | |
#define DHT11 11 | |
#define DHT22 22 | |
#define AM2302 22 | |
int readDHT(int type, int pin, char* result); | |
int main(int argc, char **argv) | |
{ | |
if (!bcm2835_init()) | |
return 1; | |
if (argc != 3) { | |
printf("usage: %s [11|22|2302] GPIOpin#\n", argv[0]); | |
printf("example: %s 2302 4 - Read from an AM2302 connected to GPIO #4\n", argv[0]); | |
return 2; | |
} | |
int type = 0; | |
if (strcmp(argv[1], "11") == 0) type = DHT11; | |
if (strcmp(argv[1], "22") == 0) type = DHT22; | |
if (strcmp(argv[1], "2302") == 0) type = AM2302; | |
if (type == 0) { | |
printf("Select 11, 22, 2302 as type!\n"); | |
return 3; | |
} | |
int dhtpin = atoi(argv[2]); | |
if (dhtpin <= 0) { | |
printf("Please select a valid GPIO pin #\n"); | |
return 3; | |
} | |
printf("Using pin #%d\n", dhtpin); | |
char theStr[] = ""; | |
readDHT(type, dhtpin, theStr); | |
printf("Result Str: %s \n",theStr); | |
return 0; | |
} // main | |
int bits[250], data[100]; | |
int bitidx = 0; | |
JNIEXPORT jstring JNICALL | |
Java_JavaDHT_readDHTfromC (JNIEnv *env, jobject obj, jint theType, jint thePin){ | |
char theStr[]="Call by Java_JavaDHT_readDHTfromC"; | |
bcm2835_init(); | |
readDHT(theType, thePin, theStr); | |
jstring rtstr = (*env)->NewStringUTF(env, theStr); | |
return rtstr; | |
} | |
int readDHT(int type, int pin, char* result) { | |
int counter = 0; | |
int laststate = HIGH; | |
int j=0; | |
// Set GPIO pin to output | |
bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_OUTP); | |
bcm2835_gpio_write(pin, HIGH); | |
usleep(500000); // 500 ms | |
bcm2835_gpio_write(pin, LOW); | |
usleep(20000); | |
bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_INPT); | |
data[0] = data[1] = data[2] = data[3] = data[4] = 0; | |
// wait for pin to drop? | |
while (bcm2835_gpio_lev(pin) == 1) { | |
usleep(1); | |
} | |
// read data! | |
for (int i=0; i< MAXTIMINGS; i++) { | |
counter = 0; | |
while ( bcm2835_gpio_lev(pin) == laststate) { | |
counter++; | |
//nanosleep(1); // overclocking might change this? | |
if (counter == 1000) | |
break; | |
} | |
laststate = bcm2835_gpio_lev(pin); | |
if (counter == 1000) break; | |
bits[bitidx++] = counter; | |
if ((i>3) && (i%2 == 0)) { | |
// shove each bit into the storage bytes | |
data[j/8] <<= 1; | |
if (counter > 200) | |
data[j/8] |= 1; | |
j++; | |
} | |
} | |
#ifdef DEBUG | |
for (int i=3; i<bitidx; i+=2) { | |
printf("bit %d: %d\n", i-3, bits[i]); | |
printf("bit %d: %d (%d)\n", i-2, bits[i+1], bits[i+1] > 200); | |
} | |
#endif | |
printf("Data (%d): 0x%x 0x%x 0x%x 0x%x 0x%x\n", j, data[0], data[1], data[2], data[3], data[4]); | |
if ((j >= 39) && | |
(data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) ) { | |
// yay! | |
if (type == DHT11) | |
printf("Temp = %d *C, Hum = %d \%\n", data[2], data[0]); | |
sprintf(result, "Temp = %d *C, Hum = %d \%\n", data[2], data[0]); | |
if (type == DHT22) { | |
float f, h; | |
h = data[0] * 256 + data[1]; | |
h /= 10; | |
f = (data[2] & 0x7F)* 256 + data[3]; | |
f /= 10.0; | |
if (data[2] & 0x80) f *= -1; | |
printf("Temp = %.1f *C, Hum = %.1f \%\n", f, h); | |
sprintf(result, "Temp = %.1f *C, Hum = %.1f \%\n", f, h); | |
} | |
return 1; | |
} | |
//result = "Error"; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* DO NOT EDIT THIS FILE - it is machine generated */ | |
#include <jni.h> | |
/* Header for class JavaDHT */ | |
#ifndef _Included_JavaDHT | |
#define _Included_JavaDHT | |
#ifdef __cplusplus | |
extern "C" { | |
#endif | |
/* | |
* Class: JavaDHT | |
* Method: readDHTfromC | |
* Signature: (II)Ljava/lang/String; | |
*/ | |
JNIEXPORT jstring JNICALL Java_JavaDHT_readDHTfromC | |
(JNIEnv *, jobject, jint, jint); | |
#ifdef __cplusplus | |
} | |
#endif | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.*; | |
public class JavaDHT { | |
public JavaDHT (){ | |
System.loadLibrary("JavaDHT"); | |
} | |
public native String readDHTfromC (int theType, int thePin); | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
String str = new JavaDHT().readDHTfromC(11,15); | |
System.out.println("result=" + str); | |
} | |
} |
2014年4月4日 星期五
JNI 呼叫C, 由C控制 DHT11讀取溫濕度 (未完成版)
今天嘗試使用JNI 呼叫C, 由C控制 DHT11讀取溫濕度的值.
不過遇到一個奇怪的錯誤.
pi@raspberrypi ~/jni $ sudo java -Djava.library.path=. JavaDHT
DEBUG theStr--> Java_JavaDHT_readDHTfromC
#
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0xa8cb239c, pid=2663, tid=3057022064
#
# JRE version: 7.0_21-b11
# Java VM: Java HotSpot(TM) Client VM (23.21-b01 mixed mode linux-arm )
# Problematic frame:
# C [libJavaDHT.so+0x239c] bcm2835_peri_read+0x18
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /home/pi/jni/hs_err_pid2663.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.sun.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
這裡有個日本人也有類似問題, 不過夜深了, 明天再來研究好了
http://broadbeans.blog.so-net.ne.jp/2013-08-27
不過遇到一個奇怪的錯誤.
pi@raspberrypi ~/jni $ sudo java -Djava.library.path=. JavaDHT
DEBUG theStr--> Java_JavaDHT_readDHTfromC
#
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0xa8cb239c, pid=2663, tid=3057022064
#
# JRE version: 7.0_21-b11
# Java VM: Java HotSpot(TM) Client VM (23.21-b01 mixed mode linux-arm )
# Problematic frame:
# C [libJavaDHT.so+0x239c] bcm2835_peri_read+0x18
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /home/pi/jni/hs_err_pid2663.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.sun.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
這裡有個日本人也有類似問題, 不過夜深了, 明天再來研究好了
http://broadbeans.blog.so-net.ne.jp/2013-08-27
2014年4月3日 星期四
JNI Example
由於許多控制電子元件適合用Native Code來做, 於是關注了JNI 的一些功能. 不可免俗的也做了JNI 版本的 HelloWorld.
1. 首先建立要使用JNI的 JavaCallC.java 檔 以及 要被JNI呼叫的 JavaCallC.c 檔
2. 利用建立好的 JavaCallC.java 產生 JavaCallC.h
javac JavaCallC.java
javah -jni JavaCallC
3. 產生 libJavaCallC.so
gcc -shared -I /opt/java/jdk1.7.0_21/include/ -I /opt/java/jdk1.7.0_21/include/linux/ JavaCallC.c -o libJavaCallC.so
4. 執行
java -Djava.library.path=. JavaCallC
5.結果輸出
Hello world from C Language JavaCallC.c csayhello!
1. 首先建立要使用JNI的 JavaCallC.java 檔 以及 要被JNI呼叫的 JavaCallC.c 檔
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <jni.h> | |
#include "JavaCallC.h" | |
#include <stdio.h> | |
JNIEXPORT void JNICALL | |
Java_JavaCallC_csayhello(JNIEnv *env, jobject obj) | |
{ | |
printf("Hello world from C Language JavaCallC.c csayhello!\n"); | |
return; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.*; | |
public class JavaCallC { | |
public JavaCallC (){ | |
System.loadLibrary("JavaCallC"); | |
} | |
public native void csayhello(); | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
new JavaCallC().csayhello(); | |
} | |
} |
javac JavaCallC.java
javah -jni JavaCallC
3. 產生 libJavaCallC.so
gcc -shared -I /opt/java/jdk1.7.0_21/include/ -I /opt/java/jdk1.7.0_21/include/linux/ JavaCallC.c -o libJavaCallC.so
4. 執行
java -Djava.library.path=. JavaCallC
5.結果輸出
Hello world from C Language JavaCallC.c csayhello!
參考資料
訂閱:
文章 (Atom)