import cn.yumakeji.lib_serialportapi.utils.ByteUtil;
import cn.yumakeji.lib_serialportapi.utils.LogUtil;
/**
- 数据操作封装
*/
public class SerialPortUtil {
public static String TAG = “serial_port”;
/**
- 标记当前串口状态(true:打开,false:关闭)
**/
public boolean isFlagSerial = false;
public SerialPort serialPort = null;
public InputStream inputStream = null;
public OutputStream outputStream = null;
public Thread receiveThread = null;
private SerialPortUtil() {
}
public static SerialPortUtil getInstance() {
return SingletonHolder.sInstance;
}
//静态内部类
private static class SingletonHolder {
private static final SerialPortUtil sInstance = new SerialPortUtil();
}
/**
-
打开串口
-
-
串口有五个重要的参数:串口设备名,波特率,检验位,数据位,停止位
-
其中检验位一般默认位NONE,数据位一般默认为8,停止位默认为1
-
@param device 串口设备的绝对路径
-
@param baudrate 波特率
-
@param flags 校验位
-
@return
*/
public boolean open(File device, int baudrate, int flags) {
boolean isopen = false;
if (isFlagSerial) {
LogUtil.e(TAG, “串口已经打开,打开失败”);
return false;
}
try {
serialPort = new SerialPort(device, baudrate, flags);
inputStream = serialPort.getInputStream();
outputStream = serialPort.getOutputStream();
receive();
isopen = true;
isFlagSerial = true;
} catch (Exception e) {
e.printStackTrace();
isopen = false;
}
return isopen;
}
/**
-
打开串口
-
@param device 串口设备文件
-
@param baudRate 波特率
-
@param parity 奇偶校验,0 None(默认); 1 Odd; 2 Even
-
@param dataBits 数据位,5 ~ 8 (默认8)
-
@param stopBit 停止位,1 或 2 (默认 1)
-
@param flags 标记 0(默认)
-
@return
*/
public boolean open(File device, int baudRate, int parity, int dataBits,
int stopBit, int flags) {
boolean isopen = false;
if (isFlagSerial) {
LogUtil.e(TAG, “串口已经打开,打开失败”);
return false;
}
try {
serialPort = new SerialPort(device, baudRate, parity, dataBits, stopBit, flags);
inputStream = serialPort.getInputStream();
outputStream = serialPort.getOutputStream();
receive();
isopen = true;
isFlagSerial = true;
} catch (Exception e) {
e.printStackTrace();
isopen = false;
}
return isopen;
}
/**
- 关闭串口
*/
public boolean close() {
if (!isFlagSerial) {
LogUtil.e(TAG, “串口关闭失败”);
return false;
}
boolean isClose = false;
LogUtil.e(TAG, “关闭串口”);
try {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
if (serialPort != null) {
serialPort.close();
}
isClose = true;
isFlagSerial = false;//关闭串口时,连接状态标记为false
} catch (IOException e) {
e.printStackTrace();
isClose = false;
}
return isClose;
}
/**
- 发送16进制,串口指令
*/
public void sendHexString(String data) {
if (!isFlagSerial) {
LogUtil.e(TAG, “串口未打开,发送失败” + data);
return;
}
try {
outputStream.write(ByteUtil.hex2byte(data));
outputStream.flush();
LogUtil.e(TAG, “sendSerialData:” + data);
} catch (IOException e) {
e.printStackTrace();
LogUtil.e(TAG, “发送指令出现异常”);
}
}
/**
- 发送ASCII,串口指令
*/
public void sendAsciiString(String data) {
if (!isFlagSerial) {
LogUtil.e(TAG, “串口未打开,发送失败” + data);
return;
}
try {
outputStream.write(data.getBytes(“gbk”));
outputStream.flush();
LogUtil.e(TAG, “sendSerialData:” + data);
} catch (IOException e) {
e.printStackTrace();
LogUtil.e(TAG, “发送指令出现异常”);
}
}
/**
- 接收串口数据的方法
*/
public void receive() {
if (receiveThread != null && !isFlagSerial) {
return;
}
receiveThread = new Thread() {
@Override
public void run() {
while (isFlagSerial) {
try {
byte[] readData = new byte[256];
if (inputStream == null) {
return;
}
int size = inputStream.read(readData);
if (size > 0 && isFlagSerial) {
// strData = ByteUtil.byteToStr(readData, size);
if (onDataReceiveListener != null) {
onDataReceiveListener.onSerialPortData(readData, size);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
};
receiveThread.start();
}
private SerialCallBack onDataReceiveListener;
public void setOnDataReceiveListener(SerialCallBack onDataReceiveListener) {
this.onDataReceiveListener = onDataReceiveListener;
}
}
- 具体调用如下:
package cn.yumakeji.hserialportapi;
import android.Manifest;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import cn.yumakeji.lib_serialportapi.SerialPortFinder;
import cn.yumakeji.lib_serialportapi.callback.SerialCallBack;
import cn.yumakeji.lib_serialportapi.port.SerialPortUtil;
import cn.yumakeji.lib_serialportapi.utils.AppGlobals;
import cn.yumakeji.lib_serialportapi.utils.ByteUtil;
import cn.yumakeji.lib_serialportapi.utils.LogUtil;
import tsou.cn.lib_primissions.HxgPermissionFail;
import tsou.cn.lib_primissions.HxgPermissionHelper;
import tsou.cn.lib_primissions.HxgPermissionSuccess;
import utils.AppUtils;
public class MainActivity extends AppCompatActivity implements SerialCallBack {
private static final int REQUESE_CODE = 0xFFA;
private CheckBox mCbHex;
private CheckBox mCbAscii;
@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
SerialPortUtil.getInstance().close();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
HxgPermissionHelper.requestPermissionsResult(this, requestCode, permissions);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCbHex = findViewById(R.id.cb_hex);
mCbAscii = findViewById(R.id.cb_ascii);
mCbHex.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
mCbAscii.setChecked(false);
} else {
mCbAscii.setChecked(true);
}
}
});
mCbAscii.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
mCbHex.setChecked(false);
} else {
mCbHex.setChecked(true);
}
}
});
}
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
HxgPermissionHelper.with(this)
.requestCode(REQUESE_CODE)
.requestPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE)
.request();
SerialPortUtil.getInstance().setOnDataReceiveListener(this);
}
@HxgPermissionSuccess(requestCode = REQUESE_CODE)
private void success() {
Toast.makeText(this, “权限获取成功”, Toast.LENGTH_SHORT).show();
LogUtil.d(“是否root:” + AppUtils.isRooted() + “”);
}
@HxgPermissionFail(requestCode = REQUESE_CODE)
private void fail() {
Toast.makeText(this, “权限获取失败”, Toast.LENGTH_SHORT).show();
}
/**
-
打开串口
-
@param view
*/
public void onOpenClick(View view) {
// boolean open = SerialPortUtil.getInstance().open(new File(“/dev/ttyS0”), 38400, 0);
boolean open = SerialPortUtil.getInstance().open(new File(“/dev/ttyS1”), 9600, 0);
if (open) {
LogUtil.d(“串口已经打开”);
} else {
LogUtil.d(“串口打开失败”);
}
}
/**
-
发送数据
-
@param view
*/
public void onSendClick(View view) {
String trim = ((EditText) findViewById(R.id.et_input)).getText().toString().trim();
if (TextUtils.isEmpty(trim)) {
Toast.makeText(AppGlobals.getApplication().getApplicationContext(), “请输入发送内容”, Toast.LENGTH_LONG).show();
return;
}
if (!mCbHex.isChecked() && !mCbAscii.isChecked()) {
Toast.makeText(AppGlobals.getApplication().getApplicationContext(), “请选择数据类型”, Toast.LENGTH_LONG).show();
return;
}
if (mCbHex.isChecked()) {
SerialPortUtil.getInstance().sendHexString(trim);
}
if (mCbAscii.isChecked()) {
SerialPortUtil.getInstance().sendAsciiString(trim);
}
}
/**
-
关闭串口
-
@param view
*/
public void onCloseClick(View view) {
boolean close = SerialPortUtil.getInstance().close();
if (close) {
LogUtil.d(“串口已经关闭”);
}
}
/**
-
获取串口数据
-
@param view
*/
public void onInfoClick(View view) {
SerialPortFinder spf = new SerialPortFinder();
if (spf.getAllDevices() != null && spf.getAllDevices().length > 0) {
LogUtil.e(String.valueOf(Arrays.asList(spf.getAllDevices())));
}
if (spf.getAllDevicesPath() != null && spf.getAllDevicesPath().length > 0) {
LogUtil.e(String.valueOf(Arrays.asList(spf.getAllDevicesPath())));
}
}
/**
-
接收数据
-
@param buffer
-
@param size
*/
@Override
public void onSerialPortData(final byte[] buffer, final int size) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (mCbHex.isChecked()) {
// LogUtil.d(ByteUtil.byteToStr(buffer, size));
readUartData(buffer, size);
}
if (mCbAscii.isChecked()) {
try {
LogUtil.d(new String(buffer, 0, size, “gbk”));
// ((TextView) findViewById(R.id.tv_read_message)).setText(new String(buffer, 0, size));
((TextView) findViewById(R.id.tv_read_message)).setText(new String(buffer, 0, size, “gbk”));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
});
}
private StringBuffer stringBuffer = new StringBuffer();
private HexReadBean hexReadBean = new HexReadBean();
/**
-
读取16进制串口数据
-
@param buffer
-
@param size
*/
private void readUartData(byte[] buffer, int size) {
String str = ByteUtil.byteToStr(buffer, size);
if (!hexReadBean.isStartRead()) {
//表示刚开始读
stringBuffer.delete(0, stringBuffer.length());
if (size > 1 && str.substring(0, 2).equals(“02”)) {
hexReadBean.setStartRead(true);
} else if (str.equals(“02”)) {
hexReadBean.setStartRead(true);
}
stringBuffer.append(str);
} else {
stringBuffer.append(str);
if (stringBuffer.length() >= 4 && hexReadBean.getLength() == 0) {
//表示可以读长度了
hexReadBean.setLength(Integer.valueOf(stringBuffer.substring(2, 4), 16));
}
if (str.contains(“03”) && stringBuffer.length() == hexReadBean.getLength() * 2) {
//表示数据读完了
LogUtil.e(stringBuffer.toString());
hexReadBean.setType(stringBuffer.substring(4, 6));
hexReadBean.setBcc(stringBuffer.substring(stringBuffer.length() - 4, stringBuffer.length() - 2));
hexReadBean.setStartRead(false);
LogUtil.i(“读取数据的原始内容为:” + stringBuffer.substring(6, stringBuffer.length() - 4));
//转化为数组进行处理
String leftNum = “”;
String rightNum = “”;
for (int i = 0; i < stringBuffer.length(); i++) {
if (i % 2 == 0) {
leftNum += stringBuffer.charAt(i);
} else {
rightNum += stringBuffer.charAt(i);
}
}
ArrayList strings = new ArrayList<>();
for (int i = 0; i < stringBuffer.length() / 2; i++) {
strings.add(String.valueOf(leftNum.charAt(i)) + rightNum.charAt(i));
}
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
} else {
rightNum += stringBuffer.charAt(i);
}
}
ArrayList strings = new ArrayList<>();
for (int i = 0; i < stringBuffer.length() / 2; i++) {
strings.add(String.valueOf(leftNum.charAt(i)) + rightNum.charAt(i));
}
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-PoUS2WHf-1715334431854)]
[外链图片转存中…(img-YkTtrqb8-1715334431857)]
[外链图片转存中…(img-RHb5tCFf-1715334431859)]
[外链图片转存中…(img-iVlxFvet-1715334431859)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!