打开串口JNI问题,打开老是失败,跪求高手指点。

cheerry 2011-04-25 04:20:53
从网上下载的示例程序中,能够正确的打开串口通讯。

但我的程序却不能正确的打开串口
我比较了一下,当能打开串口时,LOG中没有09-02 05:34:39.385: DEBUG/dalvikvm(13423): +++ not scanning '/system/lib/libsrec_jni.so' for 'open' (wrong CL)



09-02 05:34:39.205: DEBUG/SerialPort(13423): Found new driver: /dev/rfcomm
09-02 05:34:39.205: DEBUG/SerialPort(13423): Found new driver: /dev/tcc-uart
09-02 05:34:39.365: DEBUG/SerialPort(13423): Found new device: /dev/tcc-uart5
09-02 05:34:39.365: DEBUG/SerialPort(13423): Found new device: /dev/tcc-uart4
09-02 05:34:39.365: DEBUG/SerialPort(13423): Found new device: /dev/tcc-uart3
09-02 05:34:39.365: DEBUG/SerialPort(13423): Found new device: /dev/tcc-uart2
09-02 05:34:39.365: DEBUG/SerialPort(13423): Found new device: /dev/tcc-uart1
09-02 05:34:39.365: DEBUG/SerialPort(13423): Found new device: /dev/tcc-uart0
09-02 05:34:39.385: DEBUG/dalvikvm(13423): Trying to load lib /data/data/phone.line/lib/libserial_port.so 0x43c7c8d8
09-02 05:34:39.385: DEBUG/dalvikvm(13423): Added shared lib /data/data/phone.line/lib/libserial_port.so 0x43c7c8d8
09-02 05:34:39.385: DEBUG/dalvikvm(13423): No JNI_OnLoad found in /data/data/phone.line/lib/libserial_port.so 0x43c7c8d8
09-02 05:34:39.385: DEBUG/dalvikvm(13423): +++ not scanning '/system/lib/libwebcore.so' for 'open' (wrong CL)
09-02 05:34:39.385: DEBUG/dalvikvm(13423): +++ not scanning '/system/lib/libmedia_jni.so' for 'open' (wrong CL)
09-02 05:34:39.385: DEBUG/dalvikvm(13423): +++ not scanning '/system/lib/libexif.so' for 'open' (wrong CL)
09-02 05:34:39.385: DEBUG/dalvikvm(13423): +++ not scanning '/system/lib/libsrec_jni.so' for 'open' (wrong CL)09-02 05:34:39.385: WARN/dalvikvm(13423): No implementation found for native Lphone/line/SerialPort;.open (Ljava/lang/String;I)Ljava/io/FileDescriptor;
09-02 05:34:39.395: DEBUG/AndroidRuntime(13423): Shutting down VM
09-02 05:34:39.395: WARN/dalvikvm(13423): threadid=3: thread exiting with uncaught exception (group=0x4001b160)
09-02 05:34:39.395: ERROR/AndroidRuntime(13423): Uncaught handler: thread main exiting due to uncaught exception
09-02 05:34:39.405: ERROR/AndroidRuntime(13423): java.lang.UnsatisfiedLinkError: open
09-02 05:34:39.405: ERROR/AndroidRuntime(13423): at phone.line.SerialPort.open(Native Method)
09-02 05:34:39.405: ERROR/AndroidRuntime(13423): at phone.line.SerialPort.<init>(SerialPort.java:79)
09-02 05:34:39.405: ERROR/AndroidRuntime(13423): at phone.line.openSerialPort.getSerialPort(openSerialPort.java:84)
09-02 05:34:39.405: ERROR/AndroidRuntime(13423): at phone.line.phone.initSetialPort(phone.java:58)
09-02 05:34:39.405: ERROR/AndroidRuntime(13423): at phone.line.phone.onCreate(phone.java:70)
09-02 05:34:39.405: ERROR/AndroidRuntime(13423): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-02 05:34:39.405: ERROR/AndroidRuntime(13423): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
09-02 05:34:39.405: ERROR/AndroidRuntime(13423): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
09-02 05:34:39.405: ERROR/AndroidRuntime(13423): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
09-02 05:34:39.405: ERROR/AndroidRuntime(13423): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
09-02 05:34:39.405: ERROR/AndroidRuntime(13423): at android.os.Handler.dispatchMessage(Handler.java:99)
09-02 05:34:39.405: ERROR/AndroidRuntime(13423): at android.os.Looper.loop(Looper.java:123)
09-02 05:34:39.405: ERROR/AndroidRuntime(13423): at android.app.ActivityThread.main(ActivityThread.java:4363)
09-02 05:34:39.405: ERROR/AndroidRuntime(13423): at java.lang.reflect.Method.invokeNative(Native Method)
09-02 05:34:39.405: ERROR/AndroidRuntime(13423): at java.lang.reflect.Method.invoke(Method.java:521)
09-02 05:34:39.405: ERROR/AndroidRuntime(13423): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
09-02 05:34:39.405: ERROR/AndroidRuntime(13423): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
09-02 05:34:39.405: ERROR/AndroidRuntime(13423): at dalvik.system.NativeStart.main(Native Method)
09-02 05:34:39.405: INFO/Process(882): Sending signal. PID: 13423 SIG: 3
09-02 05:34:39.415: INFO/dalvikvm(13423): threadid=7: reacting to signal 3
09-02 05:34:39.415: INFO/dalvikvm(13423): Wrote stack trace to '/data/anr/traces.txt'
09-02 05:34:48.645: WARN/ActivityManager(882): Launch timeout has expired, giving up wake lock!
09-02 05:34:48.845: WARN/ActivityManager(882): Activity idle timeout for HistoryRecord{43e29b10 phone.line/.phone}
09-02 05:34:54.245: DEBUG/dalvikvm(940): GC freed 1842 objects / 105160 bytes in 390ms




程序源码是:

public SerialPort getSerialPort() throws SecurityException, IOException //, InvalidParameterException
{
Log.d("chw", "getSerialPort: -------" );

Log.d("chw", "getSerialPort: 1-------" );
String[] entries = mSerialPortFinder.getAllDevices(); // device name
String[] path = mSerialPortFinder.getAllDevicesPath(); //device path.
int nDevice = path.length; int i;
for(i = 0; i <nDevice; i++)
{
Log.d("chw", "Found new driver: " + path[i]+ "\ngetAllDevices :" + entries[i] );
}

int baudrate = 115200;//4800;
mSerialPort = new SerialPort(new File("/dev/tcc-uart0"), baudrate);

}
Log.d("chw", "getSerialPort: ++++++++" );
return mSerialPort;
}



public class SerialPort {

private static final String TAG = "SerialPort";

/*
* Do not remove or rename the field mFd: it is used by native method close();
*/
private FileDescriptor mFd;
private FileInputStream mFileInputStream;
private FileOutputStream mFileOutputStream;

public SerialPort(File device, int baudrate) throws SecurityException, IOException
{
Log.i("chw","SerialPort --------!!!!");

/* Check access permission */
if (!device.canRead() || !device.canWrite()) {
try {
String command = "chmod 777 " + device.getAbsolutePath();
Log.i("chw", "command = " + command + "\ndevice.getAbsolutePath = " + device.getAbsolutePath());
Runtime runtime = Runtime.getRuntime();

Process proc = runtime.exec(command);
} catch (IOException e) {
Log.i("chw","chmod fail!!!!");
e.printStackTrace();
throw new SecurityException();
}
Log.i("chw", "Get serial port read/write permission");
}

Log.i("chw","open SerialPort ----------getAbsolutePath:" + device.getAbsolutePath() );
mFd = open(device.getAbsolutePath(), baudrate);
Log.i("chw","open SerialPort ++++++++++!!!!");
if (mFd == null) {
Log.e("chw", "native open returns null");
throw new IOException();
}
mFileInputStream = new FileInputStream(mFd);
mFileOutputStream = new FileOutputStream(mFd);
Log.i("chw","SerialPort ++++++++!!!!");
}


// Getters and setters
public InputStream getInputStream() {
return mFileInputStream;
}

public OutputStream getOutputStream() {
return mFileOutputStream;
}

// JNI
private native static FileDescriptor open(String path, int baudrate);
public native void close();
static {
System.loadLibrary("serial_port");
}
}










...全文
2500 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
dopic 2011-08-31
  • 打赏
  • 举报
回复
fd!=-1即打开成功
scholarlovepotato 2011-07-17
  • 打赏
  • 举报
回复
楼上,我对你第一项不是很明白,能否再清楚一点。。。呵呵
hifoxking 2011-05-27
  • 打赏
  • 举报
回复
1.检查项目属性,设置builders 选项;确保对.c更改自动生成.so文件;具体详见NDK环境搭建
2.删除项目libs/libserial_port.so文件,如果没有此文件完成第三步会自动生成,否则继续第一步;
3.修改serialport.c文件,移除fd = open(path_utf, O_RDWR | O_DIRECT | O_SYNC);中的 O_DIRECT选项;保存;
附成功logcat:
05-27 15:32:19.130: DEBUG/dalvikvm(3471): Trying to load lib /data/data/android.serialport.sample/lib/libserial_port.so 0x46133798
05-27 15:32:19.130: DEBUG/dalvikvm(3471): Added shared lib /data/data/android.serialport.sample/lib/libserial_port.so 0x46133798
05-27 15:32:19.130: DEBUG/dalvikvm(3471): No JNI_OnLoad found in /data/data/android.serialport.sample/lib/libserial_port.so 0x46133798, skipping init
05-27 15:32:19.130: DEBUG/serial_port(3471): Opening serial port /dev/ttyUSB0
05-27 15:32:19.141: DEBUG/serial_port(3471): open() fd = 32
打开成功;
android129 2011-05-12
  • 打赏
  • 举报
回复
能打开串口吗?
fd = open(path_utf, O_RDWR | O_DIRECT | O_SYNC);
LOGD("open() fd = %d", fd);

在这里 fd 返回的为 -1, 不能打开。

有哪位能够正常打开串口的?
Smile__LV 2011-05-12
  • 打赏
  • 举报
回复
错误很明显 没找到 .so库文件 原因可能 你自己代码没有写正确 或者 你直接使用的别人的.so导入自己工程里面,这种最常见,这样做是错误的。。。。.so的搜寻是要和你的 工程目录有直接关系的,就是说你使用别人的 .so那么就得和别人的工程目录是一摸一样的 否则 程序找不到 .so
freshui 2011-04-26
  • 打赏
  • 举报
回复
jni没找到。
先try下看 System.loadLibrary("serial_port");
有没有成功吧
xqhrs232 2011-04-25
  • 打赏
  • 举报
回复
自己写一个好了.
cheerry 2011-04-25
  • 打赏
  • 举报
回复
请高手帮忙啊。
cheerry 2011-04-25
  • 打赏
  • 举报
回复
//-----------------------------JNI:---------------------


#include <termios.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <jni.h>

#include "android/log.h"
static const char *TAG="serial_port";
#define LOGI(fmt, args...) __android_log_print(ANDROID_LOG_INFO, TAG, fmt, ##args)
#define LOGD(fmt, args...) __android_log_print(ANDROID_LOG_DEBUG, TAG, fmt, ##args)
#define LOGE(fmt, args...) __android_log_print(ANDROID_LOG_ERROR, TAG, fmt, ##args)


static speed_t getBaudrate(jint baudrate)
{
switch(baudrate) {
case 0: return B0;
case 50: return B50;
case 75: return B75;
case 110: return B110;
case 134: return B134;
case 150: return B150;
case 200: return B200;
case 300: return B300;
case 600: return B600;
case 1200: return B1200;
case 1800: return B1800;
case 2400: return B2400;
case 4800: return B4800;
case 9600: return B9600;
case 19200: return B19200;
case 38400: return B38400;
case 57600: return B57600;
case 115200: return B115200;
case 230400: return B230400;
case 460800: return B460800;
case 500000: return B500000;
case 576000: return B576000;
case 921600: return B921600;
case 1000000: return B1000000;
case 1152000: return B1152000;
case 1500000: return B1500000;
case 2000000: return B2000000;
case 2500000: return B2500000;
case 3000000: return B3000000;
case 3500000: return B3500000;
case 4000000: return B4000000;
default: return -1;
}
}

/*
* Class: cedric_serial_SerialPort
* Method: open
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT jobject JNICALL Java_android_serialport_SerialPort_open
(JNIEnv *env, jobject thiz, jstring path, jint baudrate)
{
int fd;
speed_t speed;
jobject mFileDescriptor;

/* Check arguments */
{
speed = getBaudrate(baudrate);
if (speed == -1) {
/* TODO: throw an exception */
LOGE("Invalid baudrate");
return NULL;
}
}

/* Opening device */
{
jboolean iscopy;
const char *path_utf = (*env)->GetStringUTFChars(env, path, &iscopy);
LOGD("Opening serial port %s", path_utf);
fd = open(path_utf, O_RDWR | O_DIRECT | O_SYNC);
LOGD("open() fd = %d", fd);
(*env)->ReleaseStringUTFChars(env, path, path_utf);
if (fd == -1)
{
/* Throw an exception */
LOGE("Cannot open port");
/* TODO: throw an exception */
return NULL;
}
}

/* Configure device */
{
struct termios cfg;
LOGD("Configuring serial port");
if (tcgetattr(fd, &cfg))
{
LOGE("tcgetattr() failed");
close(fd);
/* TODO: throw an exception */
return NULL;
}

cfmakeraw(&cfg);
cfsetispeed(&cfg, speed);
cfsetospeed(&cfg, speed);

if (tcsetattr(fd, TCSANOW, &cfg))
{
LOGE("tcsetattr() failed");
close(fd);
/* TODO: throw an exception */
return NULL;
}
}

/* Create a corresponding file descriptor */
{
jclass cFileDescriptor = (*env)->FindClass(env, "java/io/FileDescriptor");
jmethodID iFileDescriptor = (*env)->GetMethodID(env, cFileDescriptor, "<init>", "()V");
jfieldID descriptorID = (*env)->GetFieldID(env, cFileDescriptor, "descriptor", "I");
mFileDescriptor = (*env)->NewObject(env, cFileDescriptor, iFileDescriptor);
(*env)->SetIntField(env, mFileDescriptor, descriptorID, (jint)fd);
}

return mFileDescriptor;
}

/*
* Class: cedric_serial_SerialPort
* Method: close
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_android_serialport_SerialPort_close
(JNIEnv *env, jobject thiz)
{
jclass SerialPortClass = (*env)->GetObjectClass(env, thiz);
jclass FileDescriptorClass = (*env)->FindClass(env, "java/io/FileDescriptor");

jfieldID mFdID = (*env)->GetFieldID(env, SerialPortClass, "mFd", "Ljava/io/FileDescriptor;");
jfieldID descriptorID = (*env)->GetFieldID(env, FileDescriptorClass, "descriptor", "I");

jobject mFd = (*env)->GetObjectField(env, thiz, mFdID);
jint descriptor = (*env)->GetIntField(env, mFd, descriptorID);

LOGD("close(fd = %d)", descriptor);
close(descriptor);
}



//================= ANDROID.MK =========================================
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
TARGET_PLATFORM := android-3
LOCAL_MODULE := serial_port
LOCAL_SRC_FILES := SerialPort.c
LOCAL_LDLIBS := -llog

include $(BUILD_SHARED_LIBRARY)

80,352

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧