Android 串口开发(一) 串口读写操作

jniLibs下面放的就是so库。

注意:因为用的谷歌原生so库,所以SerialPort类的包名一定要是android_serialport_api,如果想修改这个包名,就需要重新生成对应的so库

public class SerialPortUtil {

public static String TAG = “SerialPortUtil”;

/**

  • 标记当前串口状态(true:打开,false:关闭)

**/

public static boolean isFlagSerial = false;

public static SerialPort serialPort = null;

public static InputStream inputStream = null;

public static OutputStream outputStream = null;

public static Thread receiveThread = null;

public static String strData = “”;

public static Handler mHandler;

/**

  • 打开串口

*/

public static boolean open() {

boolean isopen = false;

if(isFlagSerial){

LogUtils.e(TAG,“串口已经打开,打开失败”);

return false;

}

try {

serialPort = new SerialPort(new File(“/dev/ttyS3”), 115200, 0);

inputStream = serialPort.getInputStream();

outputStream = serialPort.getOutputStream();

receive();

isopen = true;

isFlagSerial = true;

} catch (IOException e) {

e.printStackTrace();

isopen = false;

}

return isopen;

}

/**

  • 关闭串口

*/

public static boolean close() {

if(isFlagSerial){

LogUtils.e(TAG,“串口关闭失败”);

return false;

}

boolean isClose = false;

LogUtils.e(TAG, “关闭串口”);

try {

if (inputStream != null) {

inputStream.close();

}

if (outputStream != null) {

outputStream.close();

}

isClose = true;

isFlagSerial = false;//关闭串口时,连接状态标记为false

} catch (IOException e) {

e.printStackTrace();

isClose = false;

}

return isClose;

}

/**

  • 发送串口指令

*/

public static void sendString(String data, Handler handler) {

mHandler = handler;

if (!isFlagSerial) {

LogUtils.e(TAG, “串口未打开,发送失败” + data);

return;

}

try {

outputStream.write(ByteUtil.hex2byte(data));

outputStream.flush();

LogUtils.e(TAG, “sendSerialData:” + data);

} catch (IOException e) {

e.printStackTrace();

LogUtils.e(TAG, “发送指令出现异常”);

}

}

/**

  • 接收串口数据的方法

*/

public static void receive() {

if (receiveThread != null && !isFlagSerial) {

return;

}

receiveThread = new Thread() {

@Override

public void run() {

while (isFlagSerial) {

try {

byte[] readData = new byte[32];

if (inputStream == null) {

return;

}

int size = inputStream.read(readData);

if (size > 0 && isFlagSerial) {

strData = ByteUtil.byteToStr(readData, size);

LogUtils.e(TAG, “readSerialData:” + strData);

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

};

receiveThread.start();

}

}

这个类就比较重要了,打开串口、关闭串口、读写操作,都在这个类里面写了详细的注释,另外下面在贴一个工具类出来

package com.sqy.scancode.util;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.util.Base64;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

import Decoder.BASE64Decoder;

import Decoder.BASE64Encoder;

/**

  • Created by Administrator on 2018/6/15.

*/

public class ByteUtil {

/**

  • 字符串转化成为16进制字符串

  • @param s

  • @return

*/

public static String strTo16(String s) {

String str = “”;

for (int i = 0; i < s.length(); i++) {

int ch = (int) s.charAt(i);

String s4 = Integer.toHexString(ch);

str = str + s4;

}

return str;

}

/**

  • 16进制转换成为string类型字符串

  • @param s

  • @return

*/

public static String hexStringToString(String s) {

if (s == null || s.equals(“”)) {

return null;

}

s = s.replace(" ", “”);

byte[] baKeyword = new byte[s.length() / 2];

for (int i = 0; i < baKeyword.length; i++) {

try {

baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));

} catch (Exception e) {

e.printStackTrace();

}

}

try {

s = new String(baKeyword, “UTF-8”);

new String();

} catch (Exception e1) {

e1.printStackTrace();

}

return s;

}

/**

  • 向串口发送数据转为字节数组

*/

public static byte[] hex2byte(String hex) {

String digital = “0123456789ABCDEF”;

String hex1 = hex.replace(" ", “”);

char[] hex2char = hex1.toCharArray();

byte[] bytes = new byte[hex1.length() / 2];

byte temp;

for (int p = 0; p < bytes.length; p++) {

temp = (byte) (digital.indexOf(hex2char[2 * p]) * 16);

temp += digital.indexOf(hex2char[2 * p + 1]);

bytes[p] = (byte) (temp & 0xff);

}

return bytes;

}

/**

  • 接收到的字节数组转换16进制字符串

*/

public static String bytes2HexString(byte[] b, int size) {

String ret = “”;

for (int i = 0; i < size; i++) {

String hex = Integer.toHexString(b[i] & 0xFF);

if (hex.length() == 1) {

hex = ‘0’ + hex;

}

ret += hex.toUpperCase();

}

return ret;

}

public static String bytesToHexString(byte[] src) {

StringBuilder stringBuilder = new StringBuilder(“”);

if (src == null || src.length <= 0) {

return null;

}

for (int i = 0; i < src.length; i++) {

int v = src[i] & 0xFF;

String hv = Integer.toHexString(v);

if (hv.length() < 2) {

stringBuilder.append(0);

}

stringBuilder.append(hv);

}

return stringBuilder.toString();

}

/**

  • 接收到的字节数组转换16进制字符串

*/

public static String byteToStr(byte[] b, int size) {

String ret = “”;

for (int i = 0; i < size; i++) {

String hex = Integer.toHexString(b[i] & 0xFF);

if (hex.length() == 1) {

hex = ‘0’ + hex;

}

ret += hex.toUpperCase();

}

return ret;

}

/**

  • BASE64码解密成图片

*/

public static Bitmap Base64ToImage(String imgStr) { // 对字节数组字符串进行Base64解码并生成图片

BASE64Decoder decoder = new BASE64Decoder();

Bitmap bitmap = null;

try {

// Base64解码

byte[] b = decoder.decodeBuffer(imgStr);

for (int i = 0; i < b.length; ++i) {

if (b[i] < 0) {// 调整异常数据

b[i] += 256;

}

}

bitmap = BitmapFactory.decodeByteArray(b,0,b.length);

return bitmap;

} catch (Exception e) {

LogUtils.e(“TAG”,“解析异常”);

return bitmap;

}

}

/**

  • 将图片转换为base64加密数据

*/

public static String ImageToBase64(String imgFile) {

InputStream in = null;

byte[] data = null;

try {

in = new FileInputStream(imgFile);

data = new byte[in.available()];

in.read(data);

in.close();

} catch (IOException e) {

LogUtils.e(“TAG”,“加密异常”);

e.printStackTrace();

}

BASE64Encoder encoder = new BASE64Encoder();

return encoder.encode(data);

}

/**

  • 计算CRC16校验码

  • 逐个求和

  • @param bytes 字节数组

  • @return {@link String} 校验码

  • @since 1.0

*/

public static String getCRC_16(byte[] bytes) {

int CRC = 0x0000ffff;

int POLYNOMIAL = 0x0000a001;

int i, j;

for (i = 0; i < bytes.length; i++) {

CRC ^= ((int) bytes[i] & 0x000000ff);

for (j = 0; j < 8; j++) {

if ((CRC & 0x00000001) != 0) {

CRC >>= 1;

CRC ^= POLYNOMIAL;

} else {

CRC >>= 1;

}

}

}

if (Integer.toHexString(CRC).toUpperCase().length() == 2) {

return byteToStr(bytes, bytes.length) + “00” + Integer.toHexString(CRC).toUpperCase();

} else if (Integer.toHexString(CRC).toUpperCase().length() == 3) {

return byteToStr(bytes, bytes.length) + “0” + Integer.toHexString(CRC).toUpperCase();

}

return byteToStr(bytes, bytes.length) + Integer.toHexString(CRC).toUpperCase();

}

/**

  • 指令校验和,并取出后两位字节

  • */

public static String getSum16(byte[] msg, int length) {

long mSum = 0;

byte[] mByte = new byte[length];

/** 逐Byte添加位数和 */

for (byte byteMsg : msg) {

long mNum = ((long) byteMsg >= 0) ? (long) byteMsg : ((long) byteMsg + 256);

mSum += mNum;

} /** end of for (byte byteMsg : msg) */

/** 位数和转化为Byte数组 */

for (int liv_Count = 0; liv_Count < length; liv_Count++) {

mByte[length - liv_Count - 1] = (byte) (mSum >> (liv_Count * 8) & 0xff);

} /** end of for (int liv_Count = 0; liv_Count < length; liv_Count++) */

return byteToStr(msg, length) + byteToStr(mByte, mByte.length).substring(byteToStr(mByte, mByte.length).length() - 4, byteToStr(mByte, mByte.length).length());

}

}

最后

有任何问题,欢迎广大网友一起来交流,分享高阶Android学习视频资料和面试资料包~

偷偷说一句:群里高手如云,欢迎大家加群和大佬们一起交流讨论啊!

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

gth - liv_Count - 1] = (byte) (mSum >> (liv_Count * 8) & 0xff);

} /** end of for (int liv_Count = 0; liv_Count < length; liv_Count++) */

return byteToStr(msg, length) + byteToStr(mByte, mByte.length).substring(byteToStr(mByte, mByte.length).length() - 4, byteToStr(mByte, mByte.length).length());

}

}

最后

有任何问题,欢迎广大网友一起来交流,分享高阶Android学习视频资料和面试资料包~

偷偷说一句:群里高手如云,欢迎大家加群和大佬们一起交流讨论啊!

[外链图片转存中…(img-CFjU1Wge-1714130266505)]

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 30
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值