Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
huangzhicong
/
SmartCanteen
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
01294ba9
authored
Mar 07, 2020
by
pye52
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加兼容不同机型设备的方案
parent
428558e1
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
287 additions
and
10 deletions
+287
-10
app/src/main/java/android_serialport_api/SerialPort.java
+57
-0
app/src/main/java/android_serialport_api/SerialPortFinder.java
+113
-0
app/src/main/java/com/bgycc/smartcanteen/qrcode/IQRCodeScan.java
+21
-0
app/src/main/java/com/bgycc/smartcanteen/qrcode/QRCodeScanFactory.java
+20
-0
app/src/main/java/com/bgycc/smartcanteen/qrcode/T3.java
+31
-0
app/src/main/java/com/bgycc/smartcanteen/qrcode/TPS.java
+34
-0
app/src/main/java/com/bgycc/smartcanteen/task/QRCodeTask.java
+11
-10
No files found.
app/src/main/java/android_serialport_api/SerialPort.java
0 → 100644
View file @
01294ba9
package
android_serialport_api
;
import
java.io.File
;
import
java.io.FileDescriptor
;
import
java.io.FileInputStream
;
import
java.io.FileOutputStream
;
import
java.io.InputStream
;
import
java.io.OutputStream
;
/**
* Google官方代码
* 此类的作用为,JNI的调用,用来加载.so文件的
* 获取串口输入输出流
*/
public
class
SerialPort
{
private
FileDescriptor
mFd
;
private
FileInputStream
mFileInputStream
;
private
FileOutputStream
mFileOutputStream
;
static
{
System
.
loadLibrary
(
"serial_port"
);
}
public
SerialPort
(
String
devicePath
,
int
baudRate
,
int
flags
)
throws
SecurityException
{
this
(
new
File
(
devicePath
),
baudRate
,
flags
);
}
private
SerialPort
(
File
device
,
int
baudRate
,
int
flags
)
throws
SecurityException
{
if
(!
device
.
canRead
()
||
!
device
.
canWrite
())
{
try
{
Process
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
RuntimeException
(
"没有串口权限"
);
}
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
"提权失败"
+
e
.
getMessage
(),
e
);
}
}
mFd
=
open
(
device
.
getAbsolutePath
(),
baudRate
,
flags
);
mFileInputStream
=
new
FileInputStream
(
mFd
);
mFileOutputStream
=
new
FileOutputStream
(
mFd
);
}
public
InputStream
getInputStream
()
{
return
mFileInputStream
;
}
public
OutputStream
getOutputStream
()
{
return
mFileOutputStream
;
}
private
native
static
FileDescriptor
open
(
String
path
,
int
baudrate
,
int
flags
);
public
native
void
close
();
}
app/src/main/java/android_serialport_api/SerialPortFinder.java
0 → 100644
View file @
01294ba9
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
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
<>();
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
()]);
}
}
app/src/main/java/com/bgycc/smartcanteen/qrcode/IQRCodeScan.java
0 → 100644
View file @
01294ba9
package
com
.
bgycc
.
smartcanteen
.
qrcode
;
import
java.io.InputStream
;
import
java.io.OutputStream
;
public
interface
IQRCodeScan
{
/**
* 获取输入流
*/
InputStream
getInputStream
();
/**
* 获取输出流
*/
OutputStream
getOutputStream
();
/**
* 关闭串口
*/
void
close
();
}
app/src/main/java/com/bgycc/smartcanteen/qrcode/QRCodeScanFactory.java
0 → 100644
View file @
01294ba9
package
com
.
bgycc
.
smartcanteen
.
qrcode
;
import
android.os.Build
;
public
class
QRCodeScanFactory
{
private
static
final
String
TPS
=
"TPS"
;
private
static
final
String
T3
=
"56iqDS"
;
private
static
final
String
TPS_PORT
=
"/dev/ttyS0"
;
private
static
final
String
T3_PORT
=
"/dev/ttyS6"
;
public
static
IQRCodeScan
create
()
throws
Exception
{
if
(
Build
.
DEVICE
.
contains
(
TPS
))
{
return
new
TPS
(
TPS_PORT
);
}
else
if
(
Build
.
DEVICE
.
contains
(
T3
))
{
return
new
T3
(
T3_PORT
);
}
else
{
throw
new
RuntimeException
(
"不明设备型号: "
+
Build
.
DEVICE
);
}
}
}
app/src/main/java/com/bgycc/smartcanteen/qrcode/T3.java
0 → 100644
View file @
01294ba9
package
com
.
bgycc
.
smartcanteen
.
qrcode
;
import
java.io.InputStream
;
import
java.io.OutputStream
;
import
android_serialport_api.SerialPort
;
public
class
T3
implements
IQRCodeScan
{
private
SerialPort
serial
;
public
T3
(
String
filePath
)
throws
Exception
{
serial
=
new
SerialPort
(
filePath
,
115200
,
0
);
}
@Override
public
InputStream
getInputStream
()
{
return
serial
.
getInputStream
();
}
@Override
public
OutputStream
getOutputStream
()
{
return
serial
.
getOutputStream
();
}
@Override
public
void
close
()
{
if
(
serial
!=
null
)
{
serial
.
close
();
}
}
}
app/src/main/java/com/bgycc/smartcanteen/qrcode/TPS.java
0 → 100644
View file @
01294ba9
package
com
.
bgycc
.
smartcanteen
.
qrcode
;
import
com.telpo.tps550.api.serial.Serial
;
import
java.io.InputStream
;
import
java.io.OutputStream
;
/**
* 天波580C串口读取
*/
public
class
TPS
implements
IQRCodeScan
{
private
Serial
serial
;
public
TPS
(
String
filePath
)
throws
Exception
{
serial
=
new
Serial
(
filePath
,
115200
,
0
);
}
@Override
public
InputStream
getInputStream
()
{
return
serial
.
getInputStream
();
}
@Override
public
OutputStream
getOutputStream
()
{
return
serial
.
getOutputStream
();
}
@Override
public
void
close
()
{
if
(
serial
!=
null
)
{
serial
.
close
();
}
}
}
app/src/main/java/com/bgycc/smartcanteen/task/QRCodeTask.java
View file @
01294ba9
...
...
@@ -7,10 +7,11 @@ import com.bgycc.smartcanteen.action.*;
import
com.bgycc.smartcanteen.event.LogEvent
;
import
com.bgycc.smartcanteen.event.QRCodeInvalidEvent
;
import
com.bgycc.smartcanteen.helper.TimerHelper
;
import
com.bgycc.smartcanteen.qrcode.IQRCodeScan
;
import
com.bgycc.smartcanteen.qrcode.QRCodeScanFactory
;
import
com.bgycc.smartcanteen.server.websocket.MainWebSocket
;
import
com.bgycc.smartcanteen.util.ByteUtil
;
import
com.blankj.utilcode.util.LogUtils
;
import
com.telpo.tps550.api.serial.Serial
;
import
org.greenrobot.eventbus.EventBus
;
import
org.json.JSONObject
;
...
...
@@ -36,7 +37,7 @@ public class QRCodeTask {
}
private
ScanAsyncTask
mScanAsyncTask
;
private
Serial
mS
erial
;
private
IQRCodeScan
s
erial
;
private
long
mTimerId
=
-
1
;
private
long
mOfflineTimerId
=
-
1
;
private
long
mOfflineUpdateDelay
=
0
;
...
...
@@ -52,7 +53,7 @@ public class QRCodeTask {
if
(
mScanAsyncTask
==
null
)
{
try
{
mSerial
=
new
Serial
(
"/dev/ttyS0"
,
115200
,
0
);
serial
=
QRCodeScanFactory
.
create
(
);
}
catch
(
Exception
e
)
{
LogUtils
.
file
(
"串口初始化失败"
);
}
...
...
@@ -85,13 +86,13 @@ public class QRCodeTask {
TimerHelper
.
INSTANCE
.
cancel
(
mTimerId
);
TimerHelper
.
INSTANCE
.
cancel
(
mOfflineTimerId
);
mScanAsyncTask
=
null
;
if
(
mS
erial
!=
null
)
{
if
(
s
erial
!=
null
)
{
try
{
mS
erial
.
getInputStream
().
close
();
mS
erial
.
getOutputStream
().
close
();
s
erial
.
getInputStream
().
close
();
s
erial
.
getOutputStream
().
close
();
}
catch
(
Exception
e
)
{}
mS
erial
.
close
();
mS
erial
=
null
;
s
erial
.
close
();
s
erial
=
null
;
}
}
...
...
@@ -107,13 +108,13 @@ public class QRCodeTask {
case
0
:
LogUtils
.
file
(
TAG
,
"QRCode wait"
);
String
str
=
null
;
if
(
mS
erial
!=
null
)
{
if
(
s
erial
!=
null
)
{
byte
[]
buf
=
null
;
int
len
=
0
;
while
(
true
)
{
try
{
byte
[]
b
=
new
byte
[
64
];
int
l
=
mS
erial
.
getInputStream
().
read
(
b
);
int
l
=
s
erial
.
getInputStream
().
read
(
b
);
if
(
l
==
0
)
break
;
b
=
Arrays
.
copyOfRange
(
b
,
0
,
l
);
buf
=
ByteUtil
.
merge
(
buf
,
b
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment