項(xiàng)目更新內(nèi)容:
v1.1:
(還未更新遠(yuǎn)程依賴,需要用的下載下來(lái)自用,抽時(shí)間我更新上去,不要忘了start)
- 升級(jí)至androidx
- 去除不必要的依賴
- 支持鏈?zhǔn)秸{(diào)用配置監(jiān)聽(tīng)事件、設(shè)置端口號(hào)、設(shè)置波特率
- 修改串口接收數(shù)據(jù)時(shí)格式化時(shí)間,現(xiàn)在改為時(shí)間戳
項(xiàng)目地址:https://github.com/maybesix/Android-EasySerialPort
(最新文檔看github上的項(xiàng)目文檔)
背景
之前在一個(gè)項(xiàng)目中,需要使用Android開發(fā)板來(lái)做項(xiàng)目,其中的一個(gè)需求是使用串口。最開始是用廠家提供的串口示例程序,但是配置異常麻煩。需要so文件,gradle需要配置,需要導(dǎo)入libs等等。
在第二個(gè)項(xiàng)目中,恰好又有串口的需求,但是開發(fā)版換了,使用之前的廠家的so文件,竟然出錯(cuò)沒(méi)法運(yùn)行。由于之前的廠家未提供相應(yīng)的源碼,所以無(wú)法查看是什么導(dǎo)致的so文件出錯(cuò)。所以借鑒網(wǎng)上的項(xiàng)目和谷歌的開源項(xiàng)目https://github.com/cepr/android-serialport-api來(lái)實(shí)現(xiàn)串口的調(diào)用。由于很多第三方庫(kù)質(zhì)量參差不齊,出了問(wèn)題也不知道是什么原因。因此自己基于官方公布.so來(lái)封裝串口庫(kù),最終用了很長(zhǎng)的時(shí)間,才實(shí)現(xiàn)串口的功能。同樣的問(wèn)題,由于使用NDK去調(diào)用串口的程序,配置異常麻煩。
所以,我抽時(shí)間把之前的程序梳理了一遍,方便后來(lái)者使用。
優(yōu)點(diǎn)
1.使用方便
在需要實(shí)現(xiàn)Activity或者Service中這樣寫:
SerialPortHelper serialPort;
//串口程序初始化
serialPort = new SerialPortHelper();
//設(shè)置端口
serialPort.setPort("/dev/ttyHSL1");
//設(shè)置波特率
serialPort.setBaudRate("9600");
//設(shè)置接收消息監(jiān)聽(tīng)
serialPort.setSerialPortReceivedListener(this);
//打開串口
serialPort.open();
串口發(fā)送:
//發(fā)送十六進(jìn)制
serialPort.sendHex("A55A0010002096");
//發(fā)送文本
serialPort.sendTxtString("hello world");
串口接收:實(shí)現(xiàn)SerialPortHelper.OnSerialPortReceivedListener接口
public class MainActivity extends AppCompatActivity implements SerialPortHelper.OnSerialPortReceivedListener {
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
}
...
@Override
public void onSerialPortDataReceived(ComPortData comPortData) {
//處理接收的串口消息
String s = HexStringUtils.byteArray2HexString(comPortData.getRecData());
Log.i(TAG, "onReceived: " + s);
}
}
至此,串口的打開、發(fā)送、接收就全部完成了。
2.適配性強(qiáng)
在視美泰的3288全系列、使用未知名開發(fā)板、智洋核心板等都通過(guò)實(shí)際測(cè)試??梢杂糜趯?shí)際的生產(chǎn)。
相關(guān)方法介紹
串口相關(guān)
串口操作類 → SerialPortHelper
isOpen : 是否開啟串口
getBaudRate : 獲取波特率
setBaudRate : 設(shè)置波特率
getPort : 獲取串口名稱
setPort : 設(shè)置串口名稱
open : 打開串口
close : 關(guān)閉串口
sendHex : 以16進(jìn)制發(fā)送
sendTxtString : 以文本發(fā)送
getLoopData : 獲取循環(huán)發(fā)送的數(shù)據(jù)
setLoopData : 設(shè)置循環(huán)發(fā)送的數(shù)據(jù)
getDelay : 獲取延遲
setDelay : 設(shè)置延時(shí)(毫秒)
startSend : 開啟循環(huán)發(fā)送
stopSend : 停止循環(huán)發(fā)送
OnSerialPortReceivedListener : 串口數(shù)據(jù)接收回調(diào)
串口數(shù)據(jù)基類 → ComPortData
getRecData : 獲取串口數(shù)據(jù)
setRecData : 設(shè)置串口數(shù)據(jù)
getRecTime : 獲取接收時(shí)間
setRecTime : 設(shè)置接受時(shí)間
getComPort : 獲取串口名稱
setComPort : 設(shè)置串口名稱
串口數(shù)據(jù)處理相關(guān)
十六進(jìn)制轉(zhuǎn)換 → HexStringUtils
isOdd : 判斷是否為奇數(shù)
hexString2Int : 16進(jìn)制字符串轉(zhuǎn)int
hexString2Byte : 16進(jìn)制字符串轉(zhuǎn)byte
byte2HexString : byte轉(zhuǎn)16進(jìn)制字符串
byteArray2HexString : byte數(shù)組轉(zhuǎn)16進(jìn)制字符串
hexString2ByteArray : 16進(jìn)制字符串轉(zhuǎn)byte數(shù)組
CRC校驗(yàn) → CrcUtils
isPassCRC : 返回是否通過(guò)驗(yàn)證
getCrcString : 獲取16進(jìn)制的crc字符串
toHexString : int轉(zhuǎn)16進(jìn)制字符串
getCrc : 傳入bytes,計(jì)算得到CRC驗(yàn)證碼
hexToByte : 16進(jìn)制字符串轉(zhuǎn)byte數(shù)組
如果幫助了你,請(qǐng)點(diǎn)個(gè)贊,收個(gè)藏!
歡迎加入AndroidXH技術(shù)群,群聊號(hào)碼:755520968
群沒(méi)人解散了