xmaihh / android-serialport Goto Github PK
View Code? Open in Web Editor NEW移植谷歌官方串口库,仅支持串口名称及波特率,该项目添加支持校验位、数据位、停止位、流控配置项
License: MIT License
移植谷歌官方串口库,仅支持串口名称及波特率,该项目添加支持校验位、数据位、停止位、流控配置项
License: MIT License
Hi,
First of all thanks for this library reference to understand the Android UART. Actually facing an issue with this is whenever I run the app then it wasn't able to read the drivers.
Exception:- java.io.FileNotFoundException: /proc/tty/drivers: open failed: EACCES (Permission denied)
Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
I am using this in the rooted device. Can you please help with some guidance for this? Thanks @xmaihh
在demo中用rs485开启偶校验发送数据,接收方收到的数据与发送数据不一样。
demo中发的01 04 00 01 00 15 60 05 ,串口调试助手收到的是01 10 01 15 15,用rs232测试不会出现这个现象,只有485会,请问是什么问题呢?
Hi,我第一次搞这个,出现标题的情况,这个可以如何排查呀?
2、之前测试有能接收到过数据,但是第一次Open之后不能直接收到数据,再次调用Open之后 才能收到数据(前面发送的数据拼接在一起)
3、厂商协议里有个响应的要求,在接收到message的方法里,直接写 sent方法 是不是不行?
(因为板子没开ADB,我无法直接调试看Log...只能打包安装到板子上看效果...)
RT
留个qq或者微信吧,想请教下串口
报错信息:
Execution failed for task ':app:desugarDebugFileDependencies'.
> Could not resolve all files for configuration ':app:debugRuntimeClasspath'.
> Could not find :unspecified:.
Required by:
project :app > io.github.xmaihh:serialport:2.1
发送一次指令要发3次-4次才有数据返回,而且会有重复数据,最后还有一大串0000000000,为什么呀大佬?
比如:发送 FAFB0C10000055AB,
第一次
第二次
第三次
响应数据: FC0C1000016132ADFAFC0C100001DA89ADFAFC0C100001EFBCADFAFC0C10000100000000000000000000000000000000000000000000000000000000000000
按理来说应该是: FAFC0C100001DA89AD
是不是第一次发送阻塞了,还是怎么回事,求大佬指教呀
如题求救啊大佬
type=1400 audit(0.0:2416): avc: denied { read } for name="ttyS5" dev="tmpfs" ino=10395 scontext=u:r:untrusted_app:s0:c120,c256,c512,c768 tcontext=u:object_r:serial_device:s0 tclass=chr_file permissive=1 app=com.ex.serialport
2023-04-13 17:19:50.217 m.ex....port I type=1400 audit(0.0:2417): avc: denied { write } for name="ttyS5" dev="tmpfs" ino=10395 scontext=u:r:untrusted_app:s0:c120,c256,c512,c768 tcontext=u:object_r:serial_device:s0 tclass=chr_file permissive=1 app=com.ex.serialport
2023-04-13 17:19:50.220 Thread-6 I type=1400 audit(0.0:2420): avc: denied { getattr } for path="/dev/ttyS5" dev="tmpfs" ino=10395 scontext=u:r:untrusted_app:s0:c120,c256,c512,c768 tcontext=u:object_r:serial_device:s0 tclass=chr_file permissive=1 app=com.ex.serialport
请教一下,接收不到数据,是上面的错误引起的吗?安卓端能解决吗
请问5.0以后如何申请权限
RT
https://stackoverflow.com/questions/5826198/inputstream-available-is-0-always ,这里有说明,应该改为 read()
才行。而且我现在出现了并发传过来的多个信号,只有一个信号读取到,其他的信号都被忽略了。不知这个问题,你解决的了吗?并发数据传过来的读取问题
1.可以同时支持多个串口读取数据吗?
2.一次读取数据有限长度吗?比如一次写2M以上的数据?
InputStream.available()
这个方法可以在读写操作前先得知数据流里有多少个字节可以读取,
使用时遇到一些麻烦,比如,通讯时,对方明明发来了1000个字节,但是自己的程序调用available()方法却只得到900,或者100,甚至是0, 这是因为串口通讯是间断性的,和波特率有关,一串字节往往分几批进行发送。本地程序调用available()方法有时得到0,这可能是对方还没有响应,也可能是对方已经响应了,但是数据还没有送达本地。对方发送了1000个字节给你,也许分成3批到达,这你就要调用3次available()方法才能将数据总数全部得到。
安卓端使用sendHex发送,内容长度大概是3000,接收方没有收到这内容。请问改如何处理发送大数据内容,谢谢
软件长时间运行,一连运行一个星期以上的时候,就读取不到信息,是发生串口宕机吗?
during some tests I noticed that if I call the 'serialHelper.open()' method twice in a row even calling 'serialHelper.close()' in 'onDestroy()', it creates another FileDescriptor , and with that I can no longer read the serial data.
public void close() {
if (this.mReadThread != null) {
this.mReadThread.interrupt();
}
if (this.mSerialPort != null) {
this.mSerialPort.close();
this.mSerialPort = null;
}
this._isOpen = false;
}
after running the opening I get in logcat:
Now, if I destroy the activity, and run it again, look what happens
Android 接收串口数据,串口发送太快的时候,接收到的数据会合并在一起。
目前2种类型的数据,都是AA开头,第2位是对应长度,最后一位是合校验(2个类型的数据长度不一致)。
对应该用 StaticLenStickPackageHelper 这个么?还是SpecifiedStickPackageHelper?
SpecifiedStickPackageHelper 中的一些参数有点看不懂
求助一些示例代码
偶尔会遇到这个NPE问题。。。不知道怎么解决
protected lateinit var mTTYS1SerialPort: SerialPortHelper
protected lateinit var mTTYS4SerialPort: SerialPortHelper
companion object {
private const val TAG = "ATOneDevice"
private const val BIT_RATE = 115200
private const val STOP_BIT = 1
private const val DATA_BIT = 8
private const val PARITY = 0 // 不使用校验
private const val FLOW_CON = 0 // 不使用流控制
private const val FLAGS = 0 // 正常模式
}
@Throws(SecurityException::class, IOException::class, InvalidParameterException::class)
private fun tryOpenSerialPort(): Boolean {
Log.i(TAG, "打开LORA串口 START")
// 第一次连接需要同时打开S1和S4串口
mTTYS1SerialPort = SerialPortHelper(loraS1SerialPort, BIT_RATE)
mTTYS4SerialPort = SerialPortHelper(loraS4SerialPort, BIT_RATE)
mTTYS1SerialPort.stickPackageHelper = SpecifiedStickPackageHelper("AT".toByteArray(), "\r\n".toByteArray())
mTTYS1SerialPort.dataBits = DATA_BIT
mTTYS1SerialPort.stopBits = STOP_BIT
mTTYS1SerialPort.parity = PARITY
mTTYS1SerialPort.flowCon = FLOW_CON
mTTYS1SerialPort.setDataReceiveCallback(object : IDataReceive {
override fun onReceived(cmdStr: String, serialPort: String) {
mCurrentSerialPortIsS1 = true
onReceived(cmdStr)
}
})
mTTYS1SerialPort.open()
mTTYS1IsConnected = true
Log.i(TAG, "打开LORA S1串口")
mTTYS4SerialPort.stickPackageHelper = SpecifiedStickPackageHelper("AT".toByteArray(), "\r\n".toByteArray())
mTTYS4SerialPort.dataBits = DATA_BIT
mTTYS4SerialPort.stopBits = STOP_BIT
mTTYS4SerialPort.parity = PARITY
mTTYS4SerialPort.flowCon = FLOW_CON
mTTYS4SerialPort.setDataReceiveCallback(object : IDataReceive {
override fun onReceived(cmdStr: String, serialPort: String) {
mCurrentSerialPortIsS1 = false
onReceived(cmdStr)
}
})
mTTYS4SerialPort.open()
mTTYS4IsConnected = true
Log.i(TAG, "打开LORA S4串口")
Log.i(TAG, "打开LORA串口 END")
return true
}
jclass cFileDescriptor = env->FindClass("java/io/FileDescriptor");
jmethodID iFileDescriptor = env->GetMethodID(cFileDescriptor,"","()V");
jfieldID descriptorID = env->GetFieldID( cFileDescriptor, "descriptor", "I");
mFileDescription = env->NewObject(cFileDescriptor,iFileDescriptor);
env->SetIntField(mFileDescription,descriptorID,(jint)fd);
Android Studio 4.1.1 says "Class 'FileDescriptor' has no field named 'descriptor'. "
I am sending 55 from dev/ttyCOM3 with 203400 as baudrate and at the other end i am recieving B5 with the same baudrate at dev/ttyS1.
But If I do the same above with 115200 baudrate both ends data is same not changing.
I want to recieve similiar data with higher baudrate.
Where is the problem?
我在oncreate里 open,在onDestroy close 这样我来回进入这个页面几次就会导致内存泄漏,这个怎么解决
我百度了一下,需要把/system/bin/su修改为/system/bin/sh
https://blog.csdn.net/weixin_43266090/article/details/108245698
希望能修正一下
已知:
发送hex "010203" 返回hex "040506"
粘包处理用SpecifiedStickPackageHelper
问题:
打开串口,此时mReadThread为Thread-10。
发送 "010203"
接收"040506",处理完毕后,停留在SpecifiedStickPackageHelper$execute方法的 while ((len = is.read()) != -1)
is.read() 阻塞线程
关闭串口,调用了mReadThread.interrupt(),但此时停留在is.read,Thread-10线程无法立即结束。
此时再打开串口,mReadThread为Thread-11。
发送"010203"
返回值 “040506”并不是预期中的全部被Thread-11给read到了,一部分被Thread-10read后,Thread-10从is.read中走了出来,然后结束了线程,导致Thread-11接收的数据不全。
原因:
mReadThread在空闲时是停留在InputStream.read导致线程阻塞,所以此时close方法调了mReadThread.interrupt()也无法立即结束线程。
解决方案:
mReadThread在空闲不要停留在is.read
while ((len = is.read()) != -1) => while (is.available()>0&&(len = is.read()) != -1)
A word of thanks for this project, I've been able to perform UART communication via my Android app. Just one issue, however, is that I need it to work at 128000 baud rate. As of now, I could only make it work at either 115200 or 230400.
Are there any ways to enable it at 128000?
波特率,数据位,停止位,这些设置都没有问题,发送数据也没问题,就是收不到数据。
开启混淆后运行报错,得到错误信息
A/art: art/runtime/check_jni.cc:65] JNI DETECTED ERROR IN APPLICATION: JNI GetFieldID called with pending exception 'java.lang.NoSuchFieldError' thrown in void android_serialport_api.SerialPort.close():-2
A/art: art/runtime/check_jni.cc:65] in call to GetFieldID
A/art: art/runtime/check_jni.cc:65] from void android_serialport_api.SerialPort.close()
A/art: art/runtime/check_jni.cc:65] "IntentService[KitchenAid]" prio=5 tid=22 Runnable
A/art: art/runtime/check_jni.cc:65] | group="main" sCount=0 dsCount=0 obj=0x12f477b0 self=0xb4846b00
A/art: art/runtime/check_jni.cc:65] | sysTid=6585 nice=0 cgrp=default sched=0/0 handle=0xb4928e00
A/art: art/runtime/check_jni.cc:65] | state=R schedstat=( 1392412644 43181446 156 ) utm=137 stm=2 core=3 HZ=100
A/art: art/runtime/check_jni.cc:65] | stack=0xa3a11000-0xa3a13000 stackSize=1036KB
A/art: art/runtime/check_jni.cc:65] | held mutexes= "mutator lock"(shared held)
A/art: art/runtime/check_jni.cc:65] native: #00 pc 00004e64 /system/lib/libbacktrace_libc++.so (UnwindCurrent::Unwind(unsigned int, ucontext*)+23)
A/art: art/runtime/check_jni.cc:65] native: #01 pc 00003665 /system/lib/libbacktrace_libc++.so (Backtrace::Unwind(unsigned int, ucontext*)+8)
A/art: art/runtime/check_jni.cc:65] native: #02 pc 002423d9 /system/lib/libart.so (art::DumpNativeStack(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, int, char const*, art::mirror::ArtMethod*)+68)
A/art: art/runtime/check_jni.cc:65] native: #03 pc 00226013 /system/lib/libart.so (art::Thread::Dump(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) const+146)
A/art: art/runtime/check_jni.cc:65] native: #04 pc 000afdcf /system/lib/libart.so (art::JniAbort(char const*, char const*)+582)
A/art: art/runtime/check_jni.cc:65] native: #05 pc 000b050f /system/lib/libart.so (art::JniAbortF(char const*, char const*, ...)+58)
A/art: art/runtime/check_jni.cc:65] native: #06 pc 000b35c7 /system/lib/libart.so (art::ScopedCheck::ScopedCheck(_JNIEnv*, int, char const*)+1286)
A/art: art/runtime/check_jni.cc:65] native: #07 pc 000b57f7 /system/lib/libart.so (art::CheckJNI::GetFieldID(_JNIEnv*, _jclass*, char const*, char const*)+26)
A/art: art/runtime/check_jni.cc:65] native: #08 pc 00000bc1 /data/app/com.project.kitchen-1/lib/arm/libserialport.so (Java_android_1serialport_1api_SerialPort_close+76)
A/art: art/runtime/check_jni.cc:65] native: #09 pc 0000014f /data/dalvik-cache/arm/data@[email protected]@[email protected] (Java_android_1serialport_1api_SerialPort_close__+82)
A/art: art/runtime/check_jni.cc:65] at android_serialport_api.SerialPort.close(Native method)
A/art: art/runtime/check_jni.cc:65] at e.a.a.a.a(SerialHelper.java:60)
A/art: art/runtime/check_jni.cc:65] at b.d.a.b.a.a(SerialManager.java:80)
解决办法:
在proguard-rules.pro
添加
-keep class tp.xmaihh.serialport.**{*;}
-keep class android_serialport_api.**{*;}
SerialHelper
类open方法打开了一个SendThread
close方法关闭的时候没有关闭SendThread,这不会出问题嘛
抱歉,打扰到您了,我想请问再导入工程之后,工程可以运行,但是termios.h文件出现
'sys/cdefs.h' file not found
'sys/ioctl.h' file not found
'sys/types.h' file not found
'stdint.h' file not found
'linux/termios.h' file not found
Unknown type name '__BEGIN_DECLS'
Use of undeclared identifier 'ioctl'
Use of undeclared identifier 'TCGETS'
Use of undeclared identifier 'ioctl'
Use of undeclared identifier 'TCXONC'
请问怎样解决?
拔了usb 串口的警告日志 2.1.1 版本
java.io.IOException: I/O error
at java.io.FileInputStream.available0(Native Method)
at java.io.FileInputStream.available(FileInputStream.java:323)
at tp.xmaihh.serialport.stick.BaseStickPackageHelper.execute(BaseStickPackageHelper.java:18)
at tp.xmaihh.serialport.SerialHelper$ReadThread.run(SerialHelper.java:95)
接收串口数据的时候数据被分成了很多段,请问下这怎么解决呐
导入包然后复制demo里面的代码,发送后收不到回应,没有任何回应,请问这是什么情况
如题
目前在SerialHelper的读取回调onDataReceived中,不管发送的数据包多大,总是拿到63字节的缓冲区。无法确定收到数据的大小。
ANR in com.ex.serialport (com.ex.serialport/.MainActivity)
PID: 13718
Reason: Input dispatching timed out (Waiting to send non-key event because the touched window has not finished processing certain input events that were delivered to it over 500.0ms ago. Wait queue length: 2. Wait queue head age: 6434.5ms.)
Load: 6.27 / 6.12 / 6.02
CPU usage from 0ms to 10785ms later (2024-01-07 12:57:42.865 to 2024-01-07 12:57:53.651):
1.4% 1578/system_server: 0.9% user + 0.5% kernel / faults: 2837 minor 11 major
1.2% 2633/adbd: 0% user + 1.1% kernel / faults: 42947 minor
0.4% 1730/com.android.systemui: 0.3% user + 0% kernel / faults: 1958 minor 2 major
0.2% 1349/logd: 0% user + 0.2% kernel / faults: 2 minor
0.2% 1424/[email protected]: 0.1% user + 0% kernel
0.2% 1895/com.android.phone: 0.1% user + 0% kernel / faults: 702 minor 3 major
0.2% 13718/com.ex.serialport: 0.1% user + 0% kernel / faults: 1815 minor 5 major
0% 1463/media.codec: 0% user + 0% kernel / faults: 1210 minor 6 major
0% 2144/com.android.se: 0% user + 0% kernel / faults: 745 minor 1 major
0% 2154/com.android.coreservice: 0% user + 0% kernel / faults: 678 minor 1 major
0% 7/rcu_preempt: 0% user + 0% kernel
0% 36/kworker/u8:2: 0% user + 0% kernel
0% 1414/[email protected]: 0% user + 0% kernel
0% 1430/[email protected]: 0% user + 0% kernel
0% 1434/ldinit: 0% user + 0% kernel
0% 1437/surfaceflinger: 0% user + 0% kernel / faults: 32 minor
0% 5327/com.youkong.medicalrobot: 0% user + 0% kernel / faults: 16 minor
+0% 14133/crash_dump64: 0% user + 0% kernel
+0% 14135/droid.bluetooth: 0% user + 0% kernel
2.3% TOTAL: 1% user + 1% kernel + 0.1% iowait + 0% softirq
CPU usage from 7ms to 231ms later (2024-01-07 12:57:42.872 to 2024-01-07 12:57:43.096):
9.3% 1578/system_server: 4.6% user + 4.6% kernel / faults: 180 minor
4.6% 1593/ActivityManager: 0% user + 4.6% kernel
+0% 14056/ps: 0% user + 0% kernel
2.2% TOTAL: 1.1% user + 1.1% kernel
抱歉,再次打扰您了,我的情况是这样的:我已经成功实现了使用您的代码完成了单个串口的读写操作,通过读线程来接收数据,通过写线程来发送数据,并且使用SerialHelper类创建了三个实例对象分别管理三个串口的读写操作,希望能够实现三个串口的同时发送和接收数据,但是我在开启了一个串口的发送和接收之后,第二个串口就不可以实现正常的发送和接收了,请问应该按照怎样的思路才可以实现三个串口的同时接收和发送数据?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.