Commit 9bc26031 by patpat

新增优卡特p60s机型(未完善二维码读取)

parent 71e3ea8d
......@@ -9,7 +9,7 @@ android {
defaultConfig {
applicationId "com.bgycc.smartcanteen"
minSdkVersion 21
targetSdkVersion 28
targetSdkVersion 22
versionCode 1
versionName "1.0.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
......@@ -33,7 +33,7 @@ dependencies {
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation "org.java-websocket:Java-WebSocket:1.4.0"
implementation 'org.greenrobot:eventbus:3.1.1'
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
implementation 'com.squareup.okhttp3:okhttp:3.12.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
......
package android_serialport_api;
import android.util.Log;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Google官方代码
* 此类的作用为,JNI的调用,用来加载.so文件的
* 获取串口输入输出流
*/
public class SerialPort {
private static final String TAG = "SerialPort";
public static SerialPort build(File device, int baudrate, int flags) {
/* Check access permission */
if (!device.canRead() || !device.canWrite()) {
try {
/* Missing read/write permission, trying to chmod the file */
Process su;
su = Runtime.getRuntime().exec("/system/bin/su");
String cmd = "chmod 666 " + device.getAbsolutePath() + "\n" + "exit\n";
su.getOutputStream().write(cmd.getBytes());
if ((su.waitFor() != 0) || !device.canRead() || !device.canWrite()) {
throw new SecurityException();
}
} catch (Exception e) {
e.printStackTrace();
throw new SecurityException();
}
}
System.out.println(device.getAbsolutePath() + "==============================");
FileDescriptor fd = open(device.getAbsolutePath(), baudrate, flags);
if (fd == null) return null;
return new SerialPort(fd);
}
/*
* Do not remove or rename the field mFd: it is used by native method
* close();
*/
private FileDescriptor mFd;
private FileInputStream mFileInputStream;
private FileOutputStream mFileOutputStream;
private SerialPort(FileDescriptor fd) {
mFd = fd;
mFileInputStream = new FileInputStream(mFd);
mFileOutputStream = new FileOutputStream(mFd);
}
// Getters and setters
public InputStream getInputStream() {
return mFileInputStream;
}
public OutputStream getOutputStream() {
return mFileOutputStream;
}
// JNI
private native static FileDescriptor open(String path, int baudrate, int flags);
public native void close();
static {
System.out.println("==============================");
System.loadLibrary("serial_port");
System.out.println("********************************");
}
}
package android_serialport_api;
import android.util.Log;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.util.Iterator;
import java.util.Vector;
/**
* Google官方代码
* 此类的作用为,寻找得到有效的串口的物理地址。
* 如果你本身就知道串口的地址如:ttyS1、ttyS2,那么这个类就可以不用了。
*
*/
public class SerialPortFinder {
public class Driver {
public Driver(String name, String root) {
mDriverName = name;
mDeviceRoot = root;
}
private String mDriverName;
private String mDeviceRoot;
Vector<File> mDevices = null;
public Vector<File> getDevices() {
if (mDevices == null) {
mDevices = new Vector<File>();
File dev = new File("/dev");
File[] files = dev.listFiles();
int i;
for (i=0; i<files.length; i++) {
if (files[i].getAbsolutePath().startsWith(mDeviceRoot)) {
Log.d(TAG, "Found new device: " + files[i]);
mDevices.add(files[i]);
}
}
}
return mDevices;
}
public String getName() {
return mDriverName;
}
}
private static final String TAG = "SerialPort";
private Vector<Driver> mDrivers = null;
Vector<Driver> getDrivers() throws IOException {
if (mDrivers == null) {
mDrivers = new Vector<Driver>();
LineNumberReader r = new LineNumberReader(new FileReader("/proc/tty/drivers"));
String l;
while((l = r.readLine()) != null) {
// Issue 3:
// Since driver name may contain spaces, we do not extract driver name with split()
String drivername = l.substring(0, 0x15).trim();
String[] w = l.split(" +");
if ((w.length >= 5) && (w[w.length-1].equals("serial"))) {
Log.d(TAG, "Found new driver " + drivername + " on " + w[w.length-4]);
mDrivers.add(new Driver(drivername, w[w.length-4]));
}
}
r.close();
}
return mDrivers;
}
public String[] getAllDevices() {
Vector<String> devices = new Vector<String>();
// Parse each driver
Iterator<Driver> itdriv;
try {
itdriv = getDrivers().iterator();
while(itdriv.hasNext()) {
Driver driver = itdriv.next();
Iterator<File> itdev = driver.getDevices().iterator();
while(itdev.hasNext()) {
String device = itdev.next().getName();
String value = String.format("%s (%s)", device, driver.getName());
devices.add(value);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return devices.toArray(new String[devices.size()]);
}
public String[] getAllDevicesPath() {
Vector<String> devices = new Vector<String>();
// Parse each driver
Iterator<Driver> itdriv;
try {
itdriv = getDrivers().iterator();
while(itdriv.hasNext()) {
Driver driver = itdriv.next();
Iterator<File> itdev = driver.getDevices().iterator();
while(itdev.hasNext()) {
String device = itdev.next().getAbsolutePath();
devices.add(device);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return devices.toArray(new String[devices.size()]);
}
}
package com.bgycc.smartcanteen.task;
import android_serialport_api.SerialPort;
import com.bgycc.smartcanteen.event.QRCodeEvent;
import com.bgycc.smartcanteen.action.*;
import com.bgycc.smartcanteen.event.LogEvent;
......@@ -12,6 +13,7 @@ import kotlin.jvm.functions.Function2;
import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;
import java.io.File;
import java.io.InputStream;
import java.util.Arrays;
import java.util.regex.Pattern;
......@@ -66,10 +68,12 @@ public class QRCodeTask {
int lastLen = -1;
byte[] lastBuf;
Serial tb580cSerial = null;
SerialPort p60sSerial = null;
InputStream tb580cSerialIS = null;
byte[] tb580cSerialBuf = null;
ScanAsyncTask() {
p60sSerial = SerialPort.build(new File("/dev/ttyS6"), 9600, 0);
if (Serial.isSupport()) {
try {
tb580cSerial = new Serial("/dev/ttyS0", 115200, 0);
......@@ -98,6 +102,23 @@ public class QRCodeTask {
delay(1000);
str = new String(buf, 0, len);
} else if (p60sSerial != null) {
byte[] buf = null;
int len = 0;
try {
while (true) {
byte[] b = new byte[64];
int l = p60sSerial.getInputStream().read(b);
if (l == 0) break;
buf = ByteUtil.merge(buf, b);
len += l;
}
delay(1000);
str = new String(buf, 0, len);
} catch (Exception e) {
e.printStackTrace();
}
} else if (tb580cSerial != null && tb580cSerialIS != null) { // 天波580C
byte[] buffer = new byte[64];
int size = tb580cSerialIS.read(buffer);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment