基于微嵌安卓屏的串口测试程序开发

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/WQFAE01/article/details/86597386

在工业平板中,串口是经常可以见到的,也是在工业控制中用的比较多的接口,通常就是一个工业平板通过串口连接设备进行数据的传输,所以今天我们就来学习如何在微嵌的安卓平板上实现串口的使用

 

 串口的通信笔尖简单,一般设置好对应的参数即可,数据的接收是通过开启一个接收线程来实现,因为是编写一个测试程序,所以发送数据可选择单次发送和循环发送,同时也可以对接受和发送的数据进行统计,最后就是一个计时功能

布局和布局的代码如下,作为测试程序,页面布局比较简单,注重实用性,计时功能是在按下循环发送时即可自动计时

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

  <RelativeLayout
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="1" >

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="8">

      <TextView
          android:id="@+id/comNameText1"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="1"
          android:layout_marginStart="10dp"
          android:text="串    口:"
          android:gravity="center_vertical"
          android:textSize="30sp" />

      <TextView
          android:id="@+id/baudText"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="1"
          android:layout_marginStart="10dp"
          android:text="波特率:"
          android:gravity="center_vertical"
          android:textSize="30sp" />

      <TextView
          android:id="@+id/dataText"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="1"
          android:layout_marginStart="10dp"
          android:text="数据位:"
          android:gravity="center_vertical"
          android:textSize="30sp" />

      <TextView
          android:id="@+id/parityText"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="1"
          android:layout_marginStart="10dp"
          android:text="校验位:"
          android:gravity="center_vertical"
          android:textSize="30sp" />

      <TextView
          android:id="@+id/stopText"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="1"
          android:layout_marginStart="10dp"
          android:text="停止位:"
          android:gravity="center_vertical"
          android:textSize="30sp" />

      <Button
          android:id="@+id/openButton"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="1"
          android:layout_marginStart="10dp"
          android:text="打   开"
          android:textSize="30sp" />

      <TextView
          android:id="@+id/interval"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="1"
          android:layout_marginStart="10dp"
          android:text="间隔 (ms)"
          android:gravity="center_vertical"
          android:textSize="30sp" />

    </LinearLayout>

  </RelativeLayout>

  <RelativeLayout
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="1" >

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="8">

      <Spinner
          android:id="@+id/nameSpinner"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="1"
          android:entries="@array/portname" />

      <Spinner
          android:id="@+id/baudSpinner"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="1"
          android:entries="@array/baud" />

      <Spinner
          android:id="@+id/dataSpinner"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="1"
          android:entries="@array/databits" />

      <Spinner
          android:id="@+id/paritySpinner"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="1"
          android:entries="@array/parity" />


      <Spinner
          android:id="@+id/stopSpinner"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="1"
          android:entries="@array/stopbits" />

      <Button
          android:id="@+id/closeButton"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="1"
          android:text="关   闭"
          android:textSize="30sp" />

      <EditText
          android:id="@+id/timeEdit"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="1"
          android:text="1000"
          android:textSize="25sp" />

    </LinearLayout>

  </RelativeLayout>

  <RelativeLayout
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="4" >

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="8">

      <EditText
          android:id="@+id/receiveText"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="4"
          android:editable="false"
          android:scrollbars="vertical"
          android:textSize="25sp" />

      <EditText
          android:id="@+id/sendText"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="1"
          android:editable="false"
          android:scrollbars="vertical"
          android:textSize="25sp" />

      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="1"
          android:orientation="horizontal" >

        <Button
            android:id="@+id/startButton"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="循环发送"
            android:textSize="28sp" />

        <Button
            android:id="@+id/stopButton"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="停止发送"
            android:textSize="28sp" />

        <Button
            android:id="@+id/sendButton"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="单次发送"
            android:textSize="28sp" />

        <Button
            android:id="@+id/clearButton"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="清   除"
            android:textSize="28sp" />

      </LinearLayout>

      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="1"
          android:orientation="horizontal">

        <TextView
            android:id="@+id/reText"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="S:0"
            android:gravity="center_vertical"
            android:textSize="30sp" />

        <TextView
            android:id="@+id/seText"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="R:0"
            android:gravity="center_vertical"
            android:textSize="30sp" />

      </LinearLayout>

      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="1"
          android:orientation="horizontal">

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="计时:"
            android:textSize="30sp"
            android:gravity="center_vertical" />

        <TextView
            android:id="@+id/time_text"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="0时0分0秒"
            android:textSize="30sp"
            android:gravity="center_vertical" />

      </LinearLayout>

    </LinearLayout>

  </RelativeLayout>

</LinearLayout>

SerialPort类的定义

public class SerialPort {
    
    private int mFd;
    
    public SerialPort() {
        mFd = -1;
    }
    
    public void open(String path, int baud, int databits, String parity, int stopbits) {
        mFd = HardwareControl.OpenSerialPort(path, baud, databits, parity, stopbits);
    }
    
    public void close() {
        HardwareControl.CloseSerialPort(mFd);
    }
    
    public int read(byte[] buff, int count) {
        return HardwareControl.ReadSerialPort(mFd, buff, count);
    }
    
    public int write(byte[] buff, int count) {
        return HardwareControl.WriteSerialPort(mFd, buff, count);
    }
}

串口打开操作

case R.id.openButton:
{
      mReadThread = new ReadThread();
      mReadThread.start();
      readflag = true;
      openBut.setEnabled(false);
      closeBut.setEnabled(true);
      sendBut.setEnabled(true);
      startBut.setEnabled(true);

      int baud = Integer.parseInt(baudSpinner.getSelectedItem().toString());
      int databits = Integer.parseInt(dataSpinner.getSelectedItem().toString());
      int stopbits = Integer.parseInt(stopSpinner.getSelectedItem().toString());
      String parity = paritySpinner.getSelectedItem().toString();
      serialPort.open(nameSpinner.getSelectedItem().toString(), baud, databits, parity, stopbits);
}

单次发送数据

case R.id.sendButton:
{
      int n = sendString(sendEdit.getText().toString());
      if(n > 0) {
            seN += n;
            onDataSent();
      }
}

循环发送

case R.id.startButton:
{
      stopBut.setEnabled(true);
      mSendThread = new SendThread();
      flag = true;
      timeflag = true;
      startBut.setEnabled(false);
      mTimeThread = new TimeThread();
      mSendThread.start();
      mTimeThread.start();
}

接收数据线程

private class ReadThread extends Thread {
        @Override
        public void run() {
            super.run();
            byte[] buff = new byte[64];
            while(readflag){
                int n = serialPort.read(buff, buff.length);
                if (n > 0) {
                    reN += n;
                    onDataReceived(buff);
                }
            }
        }
    }

循环发送数据线程

private class SendThread extends Thread {
        @Override
        public void run() {
            super.run();
            while(flag)
            {
                int n = sendString(sendEdit.getText().toString());
                if(n > 0) {
                    seN += n;
                    onDataSent();
                }
                try {
                    SendThread.sleep(Integer.parseInt(timeEdit.getText().toString()));
                } catch (NumberFormatException e) {
                    // TODO 自动生成的 catch 块
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    // TODO 自动生成的 catch 块
                    e.printStackTrace();
                }
            }
        }
    }

界面的数据不能直接在线程改

protected void onDataReceived(byte[] buffer) {
        final String nbuffer = new String(buffer);
        runOnUiThread(new Runnable() {
            public void run() {
                if (reText != null) {
                    reText.setText("R:" + reN);
                    receiveEdit.append(nbuffer);
                    receiveEdit.append("\r\n");
                }
                if (receiveEdit.getLineCount() == 50) {
                    receiveEdit.setText("");
                }
            }
        });
    }
protected void onDataSent() {
        runOnUiThread(new Runnable() {
            public void run() {
                if (seText != null) {
                    seText.setText("S:" + seN);
                }
            }
        });
    }

串口关闭

case R.id.closeButton:
{
       receiveEdit.setText("");
       openBut.setEnabled(true);
       closeBut.setEnabled(false);
       sendBut.setEnabled(false);
       stopBut.setEnabled(false);
       startBut.setEnabled(false);
       timeflag = false;
       readflag = false;
       timeM = 0;
       timeH = 0;
       timeS = 0;
       serialPort.close();

       if (mReadThread != null)
             mReadThread.interrupt();
}

至此在安卓系统上是使用串口通信的最基本的功能就以实现

猜你喜欢

转载自blog.csdn.net/WQFAE01/article/details/86597386