找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1923|回复: 0
打印 上一主题 下一主题
收起左侧

Android串口显示加+MVAndroidchart图表

[复制链接]
跳转到指定楼层
楼主


1.串口使用   

  1.     private fun openSerialPort(){
  2.         serialPortFinder = SerialPortFinder()
  3.         serialHelper = object : SerialHelper() {
  4.             override fun onDataReceived(comBean: ComBean) {
  5.                 runOnUiThread {
  6. //                    Toast.makeText(
  7. //                        baseContext,
  8. //                        FuncUtil.ByteArrToHex(comBean.bRec),
  9. //                        Toast.LENGTH_SHORT
  10. //                    ).show()
  11.                     /*
  12.                     * TODO 将获取的数据进行列表适配
  13.                     * */
  14.                     map = HashMap()
  15.                     (map as HashMap<String, Any>)["Value"] =comBean.sRecTime + ":   " +
  16.                             ByteUtil.hexStringToString(ByteUtil.bytesToHexString(comBean.bRec))
  17.                     list.add(map as HashMap<String, Any>)
  18.                     Handler(Looper.getMainLooper()).postDelayed({
  19.                         /**
  20.                          * 列表数据的溢处理已经适配
  21.                          * */
  22.                         if (list.size>12){
  23.                             map = list[0]
  24.                             list.remove(map)

  25.                         }
  26.                         initAdapter()
  27.                     }, 300)

  28.                 }
  29.             }
  30.         }

  31.         //获取串口地址
  32.         val ports = serialPortFinder!!.allDevicesPath
  33.         //设置波特率地址
  34.         val botes = arrayOf(
  35.             "0",
  36.             "50",
  37.             "75",
  38.             "110",
  39.             "134",
  40.             "150",
  41.             "200",
  42.             "300",
  43.             "600",
  44.             "1200",
  45.             "1800",
  46.             "2400",
  47.             "4800",
  48.             "9600",
  49.             "19200",
  50.             "38400",
  51.             "57600",
  52.             "115200",
  53.             "230400",
  54.             "460800",
  55.             "500000",
  56.             "576000",
  57.             "921600",
  58.             "1000000",
  59.             "1152000",
  60.             "1500000",
  61.             "2000000",
  62.             "2500000",
  63.             "3000000",
  64.             "3500000",
  65.             "4000000"
  66.         )
  67.         val spAdapter = SpAdapter(this)
  68.         /*
  69.          * TODO 适配下拉列表(串口地址)
  70.         * */
  71.         spAdapter.setDatas(ports)
  72.         binding.spSerial.adapter = spAdapter
  73.         binding.spSerial.onItemSelectedListener = object : OnItemSelectedListener {
  74.             override fun onItemSelected(
  75.                 parent: AdapterView<*>?,
  76.                 view: View,
  77.                 position: Int,
  78.                 id: Long
  79.             ) {
  80.                 serialHelper?.close()

  81.                 serialHelper?.port = ports[position]

  82.                 binding.EditText1.setText(ports[position])
  83.                 binding.open.text = "关闭串口"

  84.             }

  85.             override fun onNothingSelected(parent: AdapterView<*>?) {}
  86.         }
  87.         /*
  88.          * TODO 适配下拉列表(波特率地址)
  89.         * */
  90.         val spAdapter2 = SpAdapter(this)
  91.         spAdapter2.setDatas(botes)
  92.         binding.spBote.adapter = spAdapter2
  93.         binding.spBote.onItemSelectedListener = object : OnItemSelectedListener {
  94.             override fun onItemSelected(
  95.                 parent: AdapterView<*>?,
  96.                 view: View,
  97.                 position: Int,
  98.                 id: Long
  99.             ) {
  100.                 serialHelper?.close()
  101.                 serialHelper?.setBaudRate(botes[position])
  102.                 binding.EditText2.setText(botes[position])
  103.                 binding.open.text = "关闭串口"

  104.             }

  105.             override fun onNothingSelected(parent: AdapterView<*>?) {}
  106.         }
  107.     }
  108.     override fun initListener() {
  109.         //打开串口
  110.         binding.open.setOnClickListener{
  111.             try {
  112.                 serialHelper?.open()
  113.                 binding.open.text = "打开成功"
  114.                 showToast("打开成功")


  115.             } catch (e: IOException) {
  116.                 e.printStackTrace()
  117.             }
  118.         }
  119.         //发送消息
  120.         binding.send.setOnClickListener{

  121.             if (binding.radioGroup.getCheckedRadioButtonId() == R.id.radioButton1) {
  122.                 if (binding.SendText.text.toString().isNotEmpty()) {
  123.                     if (serialHelper!!.isOpen) {
  124.                         serialHelper!!.sendTxt(binding.SendText.text.toString())
  125.                     } else {
  126.                         showToast("搞毛啊,串口都没打开")
  127.                     }
  128.                 } else {
  129.                     showToast("填数据吧")
  130.                 }
  131.             } else {
  132.                 if (binding.SendText.text.toString().length > 0) {
  133.                     if (serialHelper!!.isOpen) {
  134.                         serialHelper!!.sendHex(binding.SendText.text.toString())
  135.                     } else {
  136.                         showToast("搞毛啊,串口都没打开")
  137.                     }
  138.                 } else {
  139.                     showToast("填数据吧")
  140.                 }
  141.             }
  142.         }

  143.         //toolbar返回按钮监听
  144.         binding.toolbar.setNavigationOnClickListener { finish() }
  145.     }
复制代码

项目地址:HTTPS ://gitee.com/herui12/教MVAndroidchart

                






评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表