修改苏净zk-r322a的底层驱动逻辑

This commit is contained in:
2021-01-25 10:59:06 +08:00
parent 96b16608fd
commit 73a8d2ffbd
12 changed files with 94 additions and 219 deletions

6
.idea/encodings.xml generated
View File

@ -1,6 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="Encoding"> <component name="Encoding">
<file url="file://$PROJECT_DIR$/app/jni/Android.mk" charset="GBK" />
<file url="file://$PROJECT_DIR$/app/jni/Application.mk" charset="GBK" />
<file url="file://$PROJECT_DIR$/app/jni/SerialPort.c" charset="GBK" />
<file url="file://$PROJECT_DIR$/app/jni/SerialPort.h" charset="GBK" />
<file url="file://$PROJECT_DIR$/app/jni/gen_SerialPort_h.sh" charset="GBK" />
<file url="file://$PROJECT_DIR$/app/libs/armeabi/libserial_port.so" charset="GBK" />
<file url="file://$PROJECT_DIR$/app/src/main/java/com/jiebao/h518/scan/SqliteConnect.java" charset="GBK" /> <file url="file://$PROJECT_DIR$/app/src/main/java/com/jiebao/h518/scan/SqliteConnect.java" charset="GBK" />
<file url="file://$PROJECT_DIR$/app/vguang-jni/Android.mk" charset="GBK" /> <file url="file://$PROJECT_DIR$/app/vguang-jni/Android.mk" charset="GBK" />
<file url="file://$PROJECT_DIR$/app/vguang-jni/Application.mk" charset="GBK" /> <file url="file://$PROJECT_DIR$/app/vguang-jni/Application.mk" charset="GBK" />

View File

@ -42,5 +42,5 @@ android {
dependencies { dependencies {
//加载jar包 //加载jar包
compile fileTree(dir: 'libs', include: ['*.jar']) implementation fileTree(dir: 'libs', include: ['*.jar'])
} }

View File

@ -18,7 +18,7 @@ LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS) include $(CLEAR_VARS)
TARGET_PLATFORM := android-8 TARGET_PLATFORM := android-3
LOCAL_MODULE := serial_port LOCAL_MODULE := serial_port
LOCAL_SRC_FILES := SerialPort.c LOCAL_SRC_FILES := SerialPort.c
LOCAL_LDLIBS := -llog LOCAL_LDLIBS := -llog

1
app/jni/Application.mk Normal file
View File

@ -0,0 +1 @@
APP_ABI := armeabi armeabi-v7a x86

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2009 Cedric Priscal * Copyright 2009-2011 Cedric Priscal
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -22,52 +22,14 @@
#include <string.h> #include <string.h>
#include <jni.h> #include <jni.h>
#include "SerialPort.h"
#include "android/log.h" #include "android/log.h"
static const char *TAG="serial_port"; static const char *TAG="serial_port";
#define LOGI(fmt, args...) __android_log_print(ANDROID_LOG_INFO, TAG, fmt, ##args) #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 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) #define LOGE(fmt, args...) __android_log_print(ANDROID_LOG_ERROR, TAG, fmt, ##args)
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_android_barcode_SerialPort
* Method: open
* Signature: (Ljava/lang/String;I)I
*/
JNIEXPORT jint JNICALL Java_com_android_barcode_SerialPort_open
(JNIEnv *, jclass, jstring, jint);
/*
* Class: com_android_barcode_SerialPort
* Method: write
* Signature: (ILjava/lang/String;I)I
*/
JNIEXPORT jint JNICALL Java_com_android_barcode_SerialPort_write
(JNIEnv *, jobject, jint, jstring, jint);
/*
* Class: com_android_barcode_SerialPort
* Method: read
* Signature: (II)[B
*/
JNIEXPORT jbyteArray JNICALL Java_com_android_barcode_SerialPort_read
(JNIEnv *, jobject, jint, jint);
/*
* Class: com_android_barcode_SerialPort
* Method: close
* Signature: (I)V
*/
JNIEXPORT void JNICALL Java_com_android_barcode_SerialPort_close
(JNIEnv *, jobject, jint);
#ifdef __cplusplus
}
#endif
static speed_t getBaudrate(jint baudrate) static speed_t getBaudrate(jint baudrate)
{ {
switch(baudrate) { switch(baudrate) {
@ -107,15 +69,16 @@ static speed_t getBaudrate(jint baudrate)
} }
/* /*
* Class: cedric_serial_SerialPort * Class: android_serialport_SerialPort
* Method: open * Method: open
* Signature: (Ljava/lang/String;)V * Signature: (Ljava/lang/String;II)Ljava/io/FileDescriptor;
*/ */
JNIEXPORT jint JNICALL Java_com_android_barcode_SerialPort_open JNIEXPORT jobject JNICALL Java_android_1serialport_1api_SerialPort_open
(JNIEnv *env, jobject thiz, jstring path, jint baudrate) (JNIEnv *env, jclass thiz, jstring path, jint baudrate, jint flags)
{ {
int fd; int fd;
speed_t speed; speed_t speed;
jobject mFileDescriptor;
/* Check arguments */ /* Check arguments */
{ {
@ -123,6 +86,7 @@ JNIEXPORT jint JNICALL Java_com_android_barcode_SerialPort_open
if (speed == -1) { if (speed == -1) {
/* TODO: throw an exception */ /* TODO: throw an exception */
LOGE("Invalid baudrate"); LOGE("Invalid baudrate");
return NULL;
} }
} }
@ -130,8 +94,8 @@ JNIEXPORT jint JNICALL Java_com_android_barcode_SerialPort_open
{ {
jboolean iscopy; jboolean iscopy;
const char *path_utf = (*env)->GetStringUTFChars(env, path, &iscopy); const char *path_utf = (*env)->GetStringUTFChars(env, path, &iscopy);
LOGD("Opening serial port %s", path_utf); LOGD("Opening serial port %s with flags 0x%x", path_utf, O_RDWR | flags);
fd = open(path_utf, O_RDWR | O_SYNC); fd = open(path_utf, O_RDWR | flags);
LOGD("open() fd = %d", fd); LOGD("open() fd = %d", fd);
(*env)->ReleaseStringUTFChars(env, path, path_utf); (*env)->ReleaseStringUTFChars(env, path, path_utf);
if (fd == -1) if (fd == -1)
@ -139,6 +103,7 @@ JNIEXPORT jint JNICALL Java_com_android_barcode_SerialPort_open
/* Throw an exception */ /* Throw an exception */
LOGE("Cannot open port"); LOGE("Cannot open port");
/* TODO: throw an exception */ /* TODO: throw an exception */
return NULL;
} }
} }
@ -151,136 +116,52 @@ JNIEXPORT jint JNICALL Java_com_android_barcode_SerialPort_open
LOGE("tcgetattr() failed"); LOGE("tcgetattr() failed");
close(fd); close(fd);
/* TODO: throw an exception */ /* TODO: throw an exception */
return NULL;
} }
cfmakeraw(&cfg); cfmakeraw(&cfg);
cfsetispeed(&cfg, speed); cfsetispeed(&cfg, speed);
cfsetospeed(&cfg, speed); cfsetospeed(&cfg, speed);
/*******************************/
cfg.c_cflag &= ~CSIZE;
cfg.c_lflag &= ~(ICANON|ECHO|ECHOE|ISIG);
cfg.c_oflag &= ~OPOST;
//'8' bit
cfg.c_cflag |= CS8;
//'N' PARENB
cfg.c_cflag &= ~PARENB;
cfg.c_iflag &= ~INPCK;
//'1' STOP
cfg.c_cflag &= ~CSTOPB;
cfg.c_cc[VTIME] = 15;
cfg.c_cc[VMIN] = 0;
tcflush(fd, TCIFLUSH);
/*********************************/
if (tcsetattr(fd, TCSANOW, &cfg)) if (tcsetattr(fd, TCSANOW, &cfg))
{ {
LOGE("tcsetattr() failed"); LOGE("tcsetattr() failed");
close(fd); close(fd);
/* TODO: throw an exception */ /* TODO: throw an exception */
}
}
return fd;
}
/*
* Class: cedric_serial_SerialPort
* Method: write
* Signature: ()V
*/
JNIEXPORT jint JNICALL Java_com_android_barcode_SerialPort_write
(JNIEnv *env, jobject obj, jint fd, jstring str, jint len)
{
jboolean iscopy;
const char *buff_utf = (*env)->GetStringUTFChars(env, str, &iscopy);
int wlen = 0;
wlen = write(fd, buff_utf, len);
if(wlen > 0)
{
LOGD("Write serial port %s\n", buff_utf);
}
else
{
LOGE("Write failed\n");
}
(*env)->ReleaseStringUTFChars(env, str, buff_utf);
LOGD("Write finish!\n");
return wlen;
}
/*
* Class: cedric_serial_SerialPort
* Method: read
* Signature: ()V
*/
JNIEXPORT jbyteArray JNICALL Java_com_android_barcode_SerialPort_read
(JNIEnv *env, jobject obj, jint fd, jint len)
{
int reval;
int nread = 0;
char buff[len];
char result[len];
struct timeval tv;
jbyteArray jba;
fd_set rfds;
while(1)
{
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
tv.tv_sec = 0;
tv.tv_usec = 100000;
if ((reval = select(1 + fd, &rfds, NULL, NULL, &tv)) > 0)
{
if(FD_ISSET(fd, &rfds))
{
int temp = read(fd, buff + nread, len);
nread += temp;
LOGD("%i us is used", 100000 - tv.tv_usec);
LOGD("wo have data %d\n", temp);
}
}
else if(reval == 0 && nread != 0) //timeout means
{
// int ffd;
buff[nread]=0;
// ffd = open("/data/ndk", O_WRONLY | O_TRUNC);
// write(ffd, buff, nread);
// close(ffd);
int i, j;
for(i = 0, j = 0; i < nread; i++)
{
if(buff[i] != 0)
{
result[j] = buff[i];
j++;
}
// LOGD("0x%2x ", buff[i]);
}
//LOGD("\n");
//result[j] = 0;
jba = (*env)->NewByteArray(env, j);
(*env)->SetByteArrayRegion(env, jba, 0, j, result);
/* jba = (*env)->NewByteArray(env, nread);
(*env)->SetByteArrayRegion(env, jba, 0, nread, buff);*/
LOGD("readlength=%d\n, reallength=%d\n, all data received!\n", nread, j);
return jba;
}
else
{
return NULL; 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 * Class: cedric_serial_SerialPort
* Method: close * Method: close
* Signature: ()V * Signature: ()V
*/ */
JNIEXPORT void JNICALL Java_com_android_barcode_SerialPort_close JNIEXPORT void JNICALL Java_android_1serialport_1api_SerialPort_close
(JNIEnv *env, jobject obj, jint fd) (JNIEnv *env, jobject thiz)
{ {
close(fd); 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);
} }

29
app/jni/SerialPort.h Normal file
View File

@ -0,0 +1,29 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class android_serialport_api_SerialPort */
#ifndef _Included_android_serialport_api_SerialPort
#define _Included_android_serialport_api_SerialPort
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: android_serialport_api_SerialPort
* Method: open
* Signature: (Ljava/lang/String;II)Ljava/io/FileDescriptor;
*/
JNIEXPORT jobject JNICALL Java_android_1serialport_1api_SerialPort_open
(JNIEnv *, jclass, jstring, jint, jint);
/*
* Class: android_serialport_api_SerialPort
* Method: close
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_android_1serialport_1api_SerialPort_close
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,3 @@
#!/bin/sh
javah -o SerialPort.h -jni -classpath ../src android_serialport_api.SerialPort

Binary file not shown.

View File

@ -1,21 +1,19 @@
package android_serialport_api; package android_serialport_api;
import android.app.Application;
import android.util.Log; import android.util.Log;
import com.util.DialogUtil; import com.util.DialogUtil;
import com.util.GlobalApplication;
import map.baidu.com.BMapManagerUtil; import map.baidu.com.BMapManagerUtil;
import java.io.*; import java.io.*;
public class SerialPort { public class SerialPort{
private static final String TAG = "SerialPort"; private static final String TAG = "SerialPort";
private FileDescriptor mFd; private FileDescriptor mFd;
private FileInputStream mFileInputStream; private FileInputStream mFileInputStream;
private FileOutputStream mFileOutputStream; private FileOutputStream mFileOutputStream;
public SerialPort(File device, int baudrate, int bits, char event, int stop, int flags) throws SecurityException, public SerialPort(File device, int baudrate, int flags) throws SecurityException,
IOException { IOException {
if (!device.canRead() || !device.canWrite()) { if (!device.canRead() || !device.canWrite()) {
@ -35,7 +33,7 @@ public class SerialPort {
throw new SecurityException(); throw new SecurityException();
} }
} }
mFd = open(device.getAbsolutePath(), baudrate, bits, event, stop, flags); mFd = open(device.getAbsolutePath(), baudrate, flags);
Log.i("info", "open device!!"); Log.i("info", "open device!!");
if (mFd == null) { if (mFd == null) {
Log.e(TAG, "native open returns null"); Log.e(TAG, "native open returns null");
@ -57,12 +55,12 @@ public class SerialPort {
return mFileOutputStream; return mFileOutputStream;
} }
private native static FileDescriptor open(String path, int baudrate, int bits, char event, int stop, int flags); private native static FileDescriptor open(String path, int baudrate, int flags);
public native void close(); public native void close();
static { static {
System.loadLibrary("serialport"); System.loadLibrary("serial_port");
} }
} }

View File

@ -1,24 +1,18 @@
package com.device.zk_r322a; package com.device.zk_r322a;
import android_serialport_api.SerialPort;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import android.gpio.GpioJNI;
import android.util.Log;
import android_serialport_api.SerialPort;
public class ZKR322AControl { public class ZKR322AControl {
private SerialPort mSerialPort = null; private SerialPort mSerialPort = null;
private OutputStream mOutputStream = null;
private InputStream mInputStream = null; private InputStream mInputStream = null;
private boolean readVal = false;
public ZKR322AControl() throws SecurityException, IOException { public ZKR322AControl() throws SecurityException, IOException {
mSerialPort = new SerialPort(new File("/dev/ttyS4"), 115200, 8, 'N', 1,0); mSerialPort = new SerialPort(new File("/dev/ttyS1"), 115200,0);
mOutputStream = mSerialPort.getOutputStream(); mInputStream = mSerialPort.getInputStream();
mInputStream = mSerialPort.getInputStream();
} }
public String getData() { public String getData() {
@ -37,17 +31,11 @@ public class ZKR322AControl {
} }
public void start() { public void start() {
readVal = true; // GpioJNI.gpio_switch_scan_trig(1);
GpioJNI.gpio_switch_scan_trig(1);
} }
public void stop() { public void stop() {
readVal = false; // GpioJNI.gpio_switch_scan_trig(0);
GpioJNI.gpio_switch_scan_trig(0);
}
public boolean isRead() {
return readVal;
} }
public void close() { public void close() {
@ -58,7 +46,7 @@ public class ZKR322AControl {
public void initScan() { public void initScan() {
GpioJNI.gpio_switch_scan_rf_ired(0); // GpioJNI.gpio_switch_scan_rf_ired(0);
GpioJNI.gpio_switch_scan_power(1); // GpioJNI.gpio_switch_scan_power(1);
} }
} }

View File

@ -15,7 +15,7 @@ public class NewScanControler {
private InputStream mInputStream; private InputStream mInputStream;
public NewScanControler() throws SecurityException, IOException { public NewScanControler() throws SecurityException, IOException {
mSerialPort = new SerialPort(new File("/dev/ttySAC1"), 9600, 8, 'N', 1, mSerialPort = new SerialPort(new File("/dev/ttySAC1"), 9600,
0); 0);
mOutputStream = mSerialPort.getOutputStream(); mOutputStream = mSerialPort.getOutputStream();
mInputStream = mSerialPort.getInputStream(); mInputStream = mSerialPort.getInputStream();

View File

@ -1,31 +0,0 @@
package com.util;
/*
**********************************************
* DATE PERSON REASON
* 2021-01-15 FXY Created
**********************************************
*/
import android.app.Application;
import android.content.Context;
/**
* 全局上下文,可以用来弹出无依赖的对话框等
*/
public class GlobalApplication extends Application {
private static Context context;//全局上下文
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
}
//获取全局的上下文
public static Context getContext() {
return context;
}
}