Android 屏幕赢得';在串行数据停止输入之前,请不要更新。-科特林
我有一个kotlin应用程序,它有4个仪表和大约120个textView数值(制作一个数字破折号),还有一个串行适配器,接收数据流,然后解码并显示,然后获取更多数据并重复。问题是,在串行流停止之前,显示永远不会更新。我有流量控制,但不知道如何确定屏幕何时更新或插入延迟以使屏幕跟上。sleep()无法工作,因为它似乎可以停止一切,运行高达1亿或10亿的计数器也是如此。使用Mik3y串行库。使用felHR85,屏幕工作,但流量控制不起作用。我曾尝试使用SerialInputOutManager在新数据到达时获取中断,但屏幕似乎仍然并没有足够的时间进行更新。使用三星Tab a 10.1,它位于低端,但在Android 10(Sdk版本30)上应有足够的功率 有什么想法吗? 顺致敬意, 布莱克 这是read函数,我想你是对的,把它放在另一个线程上可能会有帮助,我不是100%知道如何做,如何在线程之间传递数组缓冲区(rsdash是一个8196字节数组)。我不知道如何设置线程也Android 屏幕赢得';在串行数据停止输入之前,请不要更新。-科特林,android,android-studio,kotlin,serial-port,Android,Android Studio,Kotlin,Serial Port,我有一个kotlin应用程序,它有4个仪表和大约120个textView数值(制作一个数字破折号),还有一个串行适配器,接收数据流,然后解码并显示,然后获取更多数据并重复。问题是,在串行流停止之前,显示永远不会更新。我有流量控制,但不知道如何确定屏幕何时更新或插入延迟以使屏幕跟上。sleep()无法工作,因为它似乎可以停止一切,运行高达1亿或10亿的计数器也是如此。使用Mik3y串行库。使用felHR85,屏幕工作,但流量控制不起作用。我曾尝试使用SerialInputOutManager在新数
fun receive(count: Int, timeout: Int): Int {
_serial.setRTS(true)
val startTime = System.currentTimeMillis()
val buffer = ByteArray(8196)
readBytes = 0
try {
while (readBytes <= count) {
val lend = _serial.read(buffer, timeout)
for (i in 0 until lend) {
rsdash[i + readBytes] = buffer[i]
}
readBytes = readBytes + lend
if (readBytes >= count) {
_serial.setRTS(false)
//if(count==387){display(rsdash)}
return readBytes
}
val duration = System.currentTimeMillis() - startTime
if (duration > timeout) {
_serial.setRTS(false)
break
}
}
_serial.setRTS(false)
return readBytes
} catch (e: IOException) {
_serial.setRTS(false)
return 0
}
}
// This code is not working, (no interupt, I assume) I am calling Receive() from the main program
val mListener: SerialInputOutputManager.Listener =
object : SerialInputOutputManager.Listener {
override fun onNewData(data: ByteArray) {
len = receive(387, 400)
_serial.write("0x52".toByteArray(), 2)
if (len == 387) {
chksum = 0
for (i in 0 until 384) chksum = chksum + (rsdash[i].toInt() and 0xFF)
chksum = (chksum and 0xFFFF)
val chktst =
(rsdash[385].toInt() and 0xFF shl 8 or (rsdash[384].toInt() and 0xFF)).toInt()
if (chksum == chktst) {
++cnt
display(rsdash)
Air2.setText(cnt.toString())
LED.setTextColor(Color.parseColor("#FF00FF"))
} else {
_serial.purgeHwBuffers(true, true)
// display(rsdash)
LED.setTextColor(Color.parseColor("#00FFFF"))
// _serial.purgeHwBuffers(true, true)
}
}
_serial.setRTS(true)
}
override fun onRunError(e: Exception) {
_serial.purgeHwBuffers(true, true)
_serial.setRTS(true)
}
}
//下面是“main”中正在工作的代码。for循环完成后,屏幕将立即更新。示波器上的一切看起来都很好,所有校验和都通过了
if(con==false){con=connect()}
if (con == true) {
if (_connected == true) {
for (i in 0 until 200) {
// _serial.setRTS(true)
len = receive(387, 400)
_serial.write("0x52".toByteArray(), 2)
if (len == 387) {
chksum = 0
for (i in 0 until 384) chksum = chksum + (rsdash[i].toInt() and 0xFF)
chksum = (chksum and 0xFFFF)
val chktst =
(rsdash[385].toInt() and 0xFF shl 8 or (rsdash[384].toInt() and 0xFF)).toInt()
if (chksum == chktst) {
++cnt
display(rsdash)
Air2.setText(cnt.toString())
LED.setTextColor(Color.parseColor("#FF00FF"))
} else {
_serial.purgeHwBuffers(true, true)
// display(rsdash)
LED.setTextColor(Color.parseColor("#00FFFF"))
// _serial.purgeHwBuffers(true, true)
}
}
//The display function is long here are all the values on the display if that helps
val EgtA = findViewById<ProgressBar>(R.id.EgtA)
val EgtB = findViewById<ProgressBar>(R.id.EgtB)
val EgtC = findViewById<ProgressBar>(R.id.EgtC)
val EgtD = findViewById<ProgressBar>(R.id.EgtD)
val EgtE = findViewById<ProgressBar>(R.id.EgtE)
val EgtF = findViewById<ProgressBar>(R.id.EgtF)
val EgtG = findViewById<ProgressBar>(R.id.EgtG)
val EgtH = findViewById<ProgressBar>(R.id.EgtH)
val EgtAN = findViewById<TextView>(R.id.EgtAN)
val EgtBN = findViewById<TextView>(R.id.EgtBN)
val EgtCN = findViewById<TextView>(R.id.EgtCN)
val EgtDN = findViewById<TextView>(R.id.EgtDN)
val EgtEN = findViewById<TextView>(R.id.EgtEN)
val EgtFN = findViewById<TextView>(R.id.EgtFN)
val EgtGN = findViewById<TextView>(R.id.EgtGN)
val EgtHN = findViewById<TextView>(R.id.EgtHN)
val O2A = findViewById<ProgressBar>(R.id.O2A)
val O2B = findViewById<ProgressBar>(R.id.O2B)
val O2C = findViewById<ProgressBar>(R.id.O2C)
val O2D = findViewById<ProgressBar>(R.id.O2D)
val O2E = findViewById<ProgressBar>(R.id.O2E)
val O2F = findViewById<ProgressBar>(R.id.O2F)
val O2G = findViewById<ProgressBar>(R.id.O2G)
val O2H = findViewById<ProgressBar>(R.id.O2H)
val O2AN = findViewById<TextView>(R.id.O2AN)
val O2BN = findViewById<TextView>(R.id.O2BN)
val O2CN = findViewById<TextView>(R.id.O2CN)
val O2DN = findViewById<TextView>(R.id.O2DN)
val O2EN = findViewById<TextView>(R.id.O2EN)
val O2FN = findViewById<TextView>(R.id.O2FN)
val O2GN = findViewById<TextView>(R.id.O2GN)
val O2HN = findViewById<TextView>(R.id.O2HN)
val CFA = findViewById<TextView>(R.id.CFA)
val CFB = findViewById<TextView>(R.id.CFB)
val CFC = findViewById<TextView>(R.id.CFC)
val CFD = findViewById<TextView>(R.id.CFD)
val CFE = findViewById<TextView>(R.id.CFE)
val CFF = findViewById<TextView>(R.id.CFF)
val CFG = findViewById<TextView>(R.id.CFG)
val CFH = findViewById<TextView>(R.id.CFH)
val Pr1 = findViewById<TextView>(R.id.Pr1)
val Pr2 = findViewById<TextView>(R.id.Pr2)
val Pr3 = findViewById<TextView>(R.id.Pr3)
val Pr4 = findViewById<TextView>(R.id.Pr4)
val Pr5 = findViewById<TextView>(R.id.Pr5)
val throt = findViewById<ProgressBar>(R.id.throt)
val Throt = findViewById<TextView>(R.id.Throt)
val Flbs = findViewById<TextView>(R.id.Flbs)
val Afl = findViewById<TextView>(R.id.Afl)
val Tim = findViewById<TextView>(R.id.Tim)
val tach = findViewById<Gauge>(R.id.tach)
val fuel = findViewById<Gauge>(R.id.fuel)
val Inj = findViewById<TextView>(R.id.Inj)
val oil = findViewById<Gauge>(R.id.oil)
val eng = findViewById<Gauge>(R.id.eng)
val Batt = findViewById<TextView>(R.id.Batt)
val O2v = findViewById<TextView>(R.id.O2v)
val Map = findViewById<TextView>(R.id.Map)
val Mat = findViewById<TextView>(R.id.Mat)
val Baro = findViewById<TextView>(R.id.Baro)
val Air = findViewById<TextView>(R.id.Air)
val Air2 = findViewById<TextView>(R.id.Air2)
val CF = findViewById<TextView>(R.id.CF)
val TO2 = findViewById<TextView>(R.id.TO2)
val Oilt = findViewById<TextView>(R.id.Oilt)
val Trnt = findViewById<TextView>(R.id.Trnt)
val TTL = findViewById<TextView>(R.id.TTL)
val TTR = findViewById<TextView>(R.id.TTR)
val Cjl = findViewById<TextView>(R.id.Cjl)
val Cjr = findViewById<TextView>(R.id.Cjr)
val Cjs = findViewById<TextView>(R.id.Cjs)
val Sp1 = findViewById<TextView>(R.id.Sp1)
val Sp2 = findViewById<TextView>(R.id.Sp2)
val Sp3 = findViewById<TextView>(R.id.Sp3)
val Sp4 = findViewById<TextView>(R.id.Sp4)
val Sp5 = findViewById<TextView>(R.id.Sp5)
val Xa = findViewById<TextView>(R.id.Xa)
val Ya = findViewById<TextView>(R.id.Ya)
val Za = findViewById<TextView>(R.id.Za)
if(con==false){con=connect()}
如果(con==true){
如果(_connected==true){
对于(i在0到200之间){
//_serial.setRTS(真)
len=接收(387400)
_serial.write(“0x52.toByteArray(),2)
如果(len==387){
chksum=0
对于(0中的i到384)chksum=chksum+(rsdash[i].toInt()和0xFF)
chksum=(chksum和0xFFFF)
瓦尔切克斯特=
(rsdash[385].toInt()和0xFF shl 8或(rsdash[384].toInt()和0xFF)).toInt()
如果(chksum==chktst){
++碳纳米管
显示(rsdash)
Air2.setText(cnt.toString())
LED.setTextColor(Color.parseColor(#FF00FF”))
}否则{
_串行.purgeHwBuffers(真,真)
//显示(rsdash)
LED.setTextColor(Color.parseColor(#00FFFF”))
//_serial.purgeHwBuffers(真,真)
}
}
//显示功能很长,如果有帮助,这里是显示器上的所有值
val EgtA=findviewbyd(R.id.EgtA)
val EgtB=findviewbyd(R.id.EgtB)
val EgtC=findviewbyd(R.id.EgtC)
val EgtD=findviewbyd(R.id.EgtD)
val EgtE=findviewbyd(R.id.EgtE)
val EgtF=findviewbyd(R.id.EgtF)
val EgtG=findViewById(R.id.EgtG)
val EgtH=findViewById(R.id.EgtH)
val EgtAN=findviewbyd(R.id.EgtAN)
val EgtBN=findviewbyd(R.id.EgtBN)
val EgtCN=findviewbyd(R.id.EgtCN)
val EgtDN=findviewbyd(R.id.EgtDN)
val EgtEN=findviewbyd(R.id.EgtEN)
val EgtFN=findviewbyd(R.id.EgtFN)
val EgtGN=findviewbyd(R.id.EgtGN)
val EgtHN=findviewbyd(R.id.EgtHN)
val O2A=findViewById(R.id.O2A)
val O2B=findViewById(R.id.O2B)
val O2C=findViewById(R.id.O2C)
val O2D=findViewById(R.id.O2D)
val O2E=findViewById(R.id.O2E)
val O2F=findViewById(R.id.O2F)
val O2G=findViewById(R.id.O2G)
val O2H=findViewById(R.id.O2H)
val O2AN=findViewById(R.id.O2AN)
val O2BN=findViewById(R.id.O2BN)
val O2CN=findViewById(R.id.O2CN)
val O2DN=findViewById(R.id.O2DN)
val O2EN=findViewById(R.id.O2EN)
val O2FN=findViewById(R.id.O2FN)
val O2GN=findViewById(R.id.O2GN)
val O2HN=findViewById(R.id.O2HN)
val CFA=findViewById(R.id.CFA)
val CFB=findViewById(R.id.CFB)
val CFC=findViewById(R.id.CFC)
val CFD=findViewById(R.id.CFD)
val CFE=findViewById(R.id.CFE)
val CFF=findViewById(R.id.CFF)
val CFG=findviewbyd(R.id.CFG)
val CFH=findviewbyd(R.id.CFH)
val Pr1=findViewById(R.id.Pr1)
val Pr2=findViewById(R.id.Pr2)
val Pr3=findViewById(R.id.Pr3)
val Pr4=findViewById(R.id.Pr4)
val Pr5=findViewById(R.id.Pr5)
val-throt=findviewbyd(R.id.throt)
val-Throt=findviewbyd(R.id.Throt)
val Flbs=findViewById(R.id.Flbs)
val Afl=findviewbyd(R.id.Afl)
val Tim=findViewById(R.id.Tim)
val tach=findViewById(R.id.tach)
val燃料=findViewById(R.id燃料)
val Inj=findViewById(R.id.Inj)
val oil=findViewById(R.id.oil)
val eng=findViewById(R.id.eng)
val Batt=findViewById(R.id.Batt)
val O2v=findViewById(R.id.O2v)
val Map=findViewById(R.id.Map)
val Mat=findViewById(R.id.Mat)
val Baro=findViewById(R.id.Baro)
val空气=findViewById(R.id.Air)
val Air2=findViewById(R.id.Air2)
val CF=findViewById(R.id.CF)
val TO2=findViewById(R.id.TO2)
val Oilt=findViewById(R.id.Oilt)
val Trnt=findViewById(R.id.Trnt)
val TTL=findviewbyd(R.id.TTL)
val TTR=findviewbyd(R.id.TTR)
val Cjl=findviewbyd(R.id.Cjl)
val Cjr=findviewbyd(R.id.Cjr)
val Cjs=findviewbyd(R.id.Cjs)
val Sp1=findviewbyd(R.id.Sp1)
val Sp2=findViewById(R.id.Sp2)
val Sp3=findViewById(R.id.Sp3)
val Sp4=findViewById(R.id.Sp4)
val Sp5=findViewById(R.id.Sp5)
val Xa=findViewById(R.id.Xa)
val Ya=findViewById(R.id.Ya)
v
if(con==false){con=connect()}
if (con == true) {
if (_connected == true) {
for (i in 0 until 200) {
// _serial.setRTS(true)
len = receive(387, 400)
_serial.write("0x52".toByteArray(), 2)
if (len == 387) {
chksum = 0
for (i in 0 until 384) chksum = chksum + (rsdash[i].toInt() and 0xFF)
chksum = (chksum and 0xFFFF)
val chktst =
(rsdash[385].toInt() and 0xFF shl 8 or (rsdash[384].toInt() and 0xFF)).toInt()
if (chksum == chktst) {
++cnt
display(rsdash)
Air2.setText(cnt.toString())
LED.setTextColor(Color.parseColor("#FF00FF"))
} else {
_serial.purgeHwBuffers(true, true)
// display(rsdash)
LED.setTextColor(Color.parseColor("#00FFFF"))
// _serial.purgeHwBuffers(true, true)
}
}
//The display function is long here are all the values on the display if that helps
val EgtA = findViewById<ProgressBar>(R.id.EgtA)
val EgtB = findViewById<ProgressBar>(R.id.EgtB)
val EgtC = findViewById<ProgressBar>(R.id.EgtC)
val EgtD = findViewById<ProgressBar>(R.id.EgtD)
val EgtE = findViewById<ProgressBar>(R.id.EgtE)
val EgtF = findViewById<ProgressBar>(R.id.EgtF)
val EgtG = findViewById<ProgressBar>(R.id.EgtG)
val EgtH = findViewById<ProgressBar>(R.id.EgtH)
val EgtAN = findViewById<TextView>(R.id.EgtAN)
val EgtBN = findViewById<TextView>(R.id.EgtBN)
val EgtCN = findViewById<TextView>(R.id.EgtCN)
val EgtDN = findViewById<TextView>(R.id.EgtDN)
val EgtEN = findViewById<TextView>(R.id.EgtEN)
val EgtFN = findViewById<TextView>(R.id.EgtFN)
val EgtGN = findViewById<TextView>(R.id.EgtGN)
val EgtHN = findViewById<TextView>(R.id.EgtHN)
val O2A = findViewById<ProgressBar>(R.id.O2A)
val O2B = findViewById<ProgressBar>(R.id.O2B)
val O2C = findViewById<ProgressBar>(R.id.O2C)
val O2D = findViewById<ProgressBar>(R.id.O2D)
val O2E = findViewById<ProgressBar>(R.id.O2E)
val O2F = findViewById<ProgressBar>(R.id.O2F)
val O2G = findViewById<ProgressBar>(R.id.O2G)
val O2H = findViewById<ProgressBar>(R.id.O2H)
val O2AN = findViewById<TextView>(R.id.O2AN)
val O2BN = findViewById<TextView>(R.id.O2BN)
val O2CN = findViewById<TextView>(R.id.O2CN)
val O2DN = findViewById<TextView>(R.id.O2DN)
val O2EN = findViewById<TextView>(R.id.O2EN)
val O2FN = findViewById<TextView>(R.id.O2FN)
val O2GN = findViewById<TextView>(R.id.O2GN)
val O2HN = findViewById<TextView>(R.id.O2HN)
val CFA = findViewById<TextView>(R.id.CFA)
val CFB = findViewById<TextView>(R.id.CFB)
val CFC = findViewById<TextView>(R.id.CFC)
val CFD = findViewById<TextView>(R.id.CFD)
val CFE = findViewById<TextView>(R.id.CFE)
val CFF = findViewById<TextView>(R.id.CFF)
val CFG = findViewById<TextView>(R.id.CFG)
val CFH = findViewById<TextView>(R.id.CFH)
val Pr1 = findViewById<TextView>(R.id.Pr1)
val Pr2 = findViewById<TextView>(R.id.Pr2)
val Pr3 = findViewById<TextView>(R.id.Pr3)
val Pr4 = findViewById<TextView>(R.id.Pr4)
val Pr5 = findViewById<TextView>(R.id.Pr5)
val throt = findViewById<ProgressBar>(R.id.throt)
val Throt = findViewById<TextView>(R.id.Throt)
val Flbs = findViewById<TextView>(R.id.Flbs)
val Afl = findViewById<TextView>(R.id.Afl)
val Tim = findViewById<TextView>(R.id.Tim)
val tach = findViewById<Gauge>(R.id.tach)
val fuel = findViewById<Gauge>(R.id.fuel)
val Inj = findViewById<TextView>(R.id.Inj)
val oil = findViewById<Gauge>(R.id.oil)
val eng = findViewById<Gauge>(R.id.eng)
val Batt = findViewById<TextView>(R.id.Batt)
val O2v = findViewById<TextView>(R.id.O2v)
val Map = findViewById<TextView>(R.id.Map)
val Mat = findViewById<TextView>(R.id.Mat)
val Baro = findViewById<TextView>(R.id.Baro)
val Air = findViewById<TextView>(R.id.Air)
val Air2 = findViewById<TextView>(R.id.Air2)
val CF = findViewById<TextView>(R.id.CF)
val TO2 = findViewById<TextView>(R.id.TO2)
val Oilt = findViewById<TextView>(R.id.Oilt)
val Trnt = findViewById<TextView>(R.id.Trnt)
val TTL = findViewById<TextView>(R.id.TTL)
val TTR = findViewById<TextView>(R.id.TTR)
val Cjl = findViewById<TextView>(R.id.Cjl)
val Cjr = findViewById<TextView>(R.id.Cjr)
val Cjs = findViewById<TextView>(R.id.Cjs)
val Sp1 = findViewById<TextView>(R.id.Sp1)
val Sp2 = findViewById<TextView>(R.id.Sp2)
val Sp3 = findViewById<TextView>(R.id.Sp3)
val Sp4 = findViewById<TextView>(R.id.Sp4)
val Sp5 = findViewById<TextView>(R.id.Sp5)
val Xa = findViewById<TextView>(R.id.Xa)
val Ya = findViewById<TextView>(R.id.Ya)
val Za = findViewById<TextView>(R.id.Za)
fun connect(): Boolean {
val drivers = UsbSerialProber.getDefaultProber().findAllDrivers(_usbManager)
if (drivers.isEmpty()) {
return false
}
val driver = drivers[0]
val _connection = _usbManager.openDevice(driver.device)
if (_connection == null) {
_connected = false
return false
}
val port = driver.ports[0]
if (port == null) {
_connected = false
return false
}
try {
port.open(_connection)
port.setParameters(
115200,
8,
UsbSerialPort.STOPBITS_1,
UsbSerialPort.PARITY_NONE
)
port.purgeHwBuffers(true, true)
_serial = port
_connected = true
// if (withIoManager) {
// usbIoManager = SerialInputOutputManager(usbSerialPort)
// Executors.newSingleThreadExecutor().submit(usbIoManager)
// }
//usbIoManager = SerialInputOutputManager(_serial,this)
usbIoManager = SerialInputOutputManager(_serial, mListener)
Executors.newSingleThreadExecutor().submit(usbIoManager)
// val submit = Executors.newSingleThreadExecutor().submit(usbIoManager)
// Executors.newSingleThreadExecutor().asCoroutineDispatcher(usbIoManager)
_serial.setRTS(true)
return true
} catch (e: IOException) {
//e.printStackTrace()
if(_connected==true){port.close()}
_connected = false
return false
}
}
// mainLooper = Handler(Looper.getMainLooper())
broadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == INTENT_ACTION_GRANT_USB) {
usbPermission = if (intent.getBooleanExtra(
UsbManager.EXTRA_PERMISSION_GRANTED,
false
)
) UsbPermission.Granted else UsbPermission.Denied
con=connect()
}
}
}
//mainLooper = Handler(Looper.getMainLooper())