Android 串口数据的发送包括数据和取低8位校验位

网上搜索了很多有关安卓串口数据带校验位的发送,发现很多问题。所以将此方法分享出来,给大家借鉴参考。里面设计的包需要全部导进去,特别注意mMBContext 要赋值,不然程序会报错。该方法仅限于android源码的调用,android studio无法使用,因为其中串口写数据的方法是调用framework层的。

import java.nio.ByteBuffer;
import android.hardware.SerialManager;
import android.hardware.SerialPort;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;


private ByteBuffer mInputBuffer;
private ByteBuffer mOutputBuffer;
private SerialManager mSerialManager;
private SerialPort mSerialPort;
private byte[] bytes;
private static Context mMBContext;//这个要特别注意,在用的整个类里面搜下context并把context赋 
 //给mMBContext,否则程序会报错,如果整个类没有context,就在要调用的地方加上这句:mMBContext = 
   // ***.this,***就是当前的类名

//用onCreate方法演示串口数据发送,仅做演示,不是串口串口数据发送的方法
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    serialPortWrite(CommunicationData("00","01","0a","0f"));//在要发送数据的地方使用这个语    
        //句,将要发送的四个数据填写进去就好    
}

private void serialPortInit() {
    mInputBuffer = ByteBuffer.allocateDirect(1024);
    mOutputBuffer = ByteBuffer.allocateDirect(1024);

	mSerialManager = 
        (SerialManager)mMBContext.getSystemService(mMBContext.SERIAL_SERVICE);
        String[] ports = mSerialManager.getSerialPorts();
	}

            try {
                mSerialPort = mSerialManager.openSerialPort("/dev/tty", 115200);//根据自己的需要填对应要发送数据的串口节点和波特率


            } catch (IOException e) {

            }

        
	}

	private void serialPortStop()
	{
        if (mSerialPort != null) {
            try {
                mSerialPort.close();
            } catch (IOException e) {

            }
            mSerialPort = null;
        }	
	}

	private void serialPortWrite(byte[] bytes)
	{
        if ( mSerialPort != null) {
            try {
                mOutputBuffer.clear();
                mOutputBuffer.put(bytes);
                mSerialPort.write(mOutputBuffer, bytes.length);
            } catch (IOException e) {
                
            }
        }
	}

    //输入四个数据,返回输入的四个数据和取低八位的校验位
    public byte[] CommunicationData(String Head,String DATA0,String DATA1,String DATA2) {
        String data = Head + DATA0 + DATA1 + DATA2 + CheckDigit(Head,DATA0,DATA1,DATA2);
		byte [] by = hex2byte(data);
        return by;
    }

    //校验位计算,取低8位为校验位
    public String CheckDigit(String Head,String DATA0,String DATA1,String DATA2) {
        int sum = Integer.parseInt(Head,16) + Integer.parseInt(DATA0,16) +         
                     Integer.parseInt(DATA1,16) + Integer.parseInt(DATA2,16);	  
        String CheckSumBinary = Integer.toBinaryString(sum);
        String CheckSum = "";
        String CheckSum_hex = "";
        if (CheckSumBinary.length() > 8) {
            CheckSum = 
             CheckSumBinary.substring(CheckSumBinary.length()-8,CheckSumBinary.length());
            sum = Integer.parseInt(CheckSum,2);
            CheckSum_hex = Integer.toHexString(sum);
        } else {
            sum = Integer.parseInt(CheckSumBinary,2);
            CheckSum_hex = Integer.toHexString(sum);
        }

        return CheckSum_hex;
    }

   //16进制转2进制
    public static byte[] hex2byte(String hex) throws IllegalArgumentException {
        if (hex.length() % 2 != 0) {
            throw new IllegalArgumentException();
        }
        char[] arr = hex.toCharArray();
        byte[] b = new byte[hex.length() / 2];
        for (int i = 0, j = 0, l = hex.length(); i < l; i++, j++) {
            String swap = "" + arr[i++] + arr[i];
            int byteint = Integer.parseInt(swap, 16) & 0xFF;
            b[j] = new Integer(byteint).byteValue();
        }
        return b;
    }

 

你可能感兴趣的:(MTK,安卓,安卓系统,java,android,串口通信,安卓)