标题:
Android串口显示加+MVAndroidchart图表
[打印本页]
作者:
herui123456
时间:
2021-11-17 10:47
标题:
Android串口显示加+MVAndroidchart图表
IMG_20211110_193229.jpg
(2.62 MB, 下载次数: 90)
下载附件
2021-11-17 10:47 上传
1.串口使用
private fun openSerialPort(){
serialPortFinder = SerialPortFinder()
serialHelper = object : SerialHelper() {
override fun onDataReceived(comBean: ComBean) {
runOnUiThread {
// Toast.makeText(
// baseContext,
// FuncUtil.ByteArrToHex(comBean.bRec),
// Toast.LENGTH_SHORT
// ).show()
/*
* TODO 将获取的数据进行列表适配
* */
map = HashMap()
(map as HashMap<String, Any>)["Value"] =comBean.sRecTime + ": " +
ByteUtil.hexStringToString(ByteUtil.bytesToHexString(comBean.bRec))
list.add(map as HashMap<String, Any>)
Handler(Looper.getMainLooper()).postDelayed({
/**
* 列表数据的溢处理已经适配
* */
if (list.size>12){
map = list[0]
list.remove(map)
}
initAdapter()
}, 300)
}
}
}
//获取串口地址
val ports = serialPortFinder!!.allDevicesPath
//设置波特率地址
val botes = arrayOf(
"0",
"50",
"75",
"110",
"134",
"150",
"200",
"300",
"600",
"1200",
"1800",
"2400",
"4800",
"9600",
"19200",
"38400",
"57600",
"115200",
"230400",
"460800",
"500000",
"576000",
"921600",
"1000000",
"1152000",
"1500000",
"2000000",
"2500000",
"3000000",
"3500000",
"4000000"
)
val spAdapter = SpAdapter(this)
/*
* TODO 适配下拉列表(串口地址)
* */
spAdapter.setDatas(ports)
binding.spSerial.adapter = spAdapter
binding.spSerial.onItemSelectedListener = object : OnItemSelectedListener {
override fun onItemSelected(
parent: AdapterView<*>?,
view: View,
position: Int,
id: Long
) {
serialHelper?.close()
serialHelper?.port = ports[position]
binding.EditText1.setText(ports[position])
binding.open.text = "关闭串口"
}
override fun onNothingSelected(parent: AdapterView<*>?) {}
}
/*
* TODO 适配下拉列表(波特率地址)
* */
val spAdapter2 = SpAdapter(this)
spAdapter2.setDatas(botes)
binding.spBote.adapter = spAdapter2
binding.spBote.onItemSelectedListener = object : OnItemSelectedListener {
override fun onItemSelected(
parent: AdapterView<*>?,
view: View,
position: Int,
id: Long
) {
serialHelper?.close()
serialHelper?.setBaudRate(botes[position])
binding.EditText2.setText(botes[position])
binding.open.text = "关闭串口"
}
override fun onNothingSelected(parent: AdapterView<*>?) {}
}
}
override fun initListener() {
//打开串口
binding.open.setOnClickListener{
try {
serialHelper?.open()
binding.open.text = "打开成功"
showToast("打开成功")
} catch (e: IOException) {
e.printStackTrace()
}
}
//发送消息
binding.send.setOnClickListener{
if (binding.radioGroup.getCheckedRadioButtonId() == R.id.radioButton1) {
if (binding.SendText.text.toString().isNotEmpty()) {
if (serialHelper!!.isOpen) {
serialHelper!!.sendTxt(binding.SendText.text.toString())
} else {
showToast("搞毛啊,串口都没打开")
}
} else {
showToast("填数据吧")
}
} else {
if (binding.SendText.text.toString().length > 0) {
if (serialHelper!!.isOpen) {
serialHelper!!.sendHex(binding.SendText.text.toString())
} else {
showToast("搞毛啊,串口都没打开")
}
} else {
showToast("填数据吧")
}
}
}
//toolbar返回按钮监听
binding.toolbar.setNavigationOnClickListener { finish() }
}
复制代码
项目地址:HTTPS ://gitee.com/herui12/教MVAndroidchart
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1