使用Java實(shí)現(xiàn)簡單串口通信
本博文參考自http://www.dhdzp.com/article/100269.htm
www.dhdzp.com/article/100269.htm
沒想到挺多人需要這個的,很高興這篇文章能對大家有幫助,主要的工具類博文里已經(jīng)有了,當(dāng)然,要小工具源碼的留言郵箱即可。 2019.09.05
最近接觸到了串口及其讀寫,在此記錄java進(jìn)行串口讀寫的過程。
1.導(dǎo)入支持java串口通信的jar包:
在maven項(xiàng)目的pom.xml中添加RXTXcomm的依賴 或者 下載RXTXcomm.jar并導(dǎo)入到項(xiàng)目中。
支持Java串口通信操作的jar包,java.comm比較老,而且不支持64位系統(tǒng),推薦使用Rxtx這個jar包(32位/64位均支持)。
下載地址:
http://xiazai.jb51.net/201612/yuanma/javamfzrxtx(jb51.net).rar(32位)
http://xiazai.jb51.net/201612/yuanma/javamfzrxtx(jb51.net).rar(64位)
注意:運(yùn)行過程中拋出java.lang.UnsatisfiedLinkError錯誤或gnu.io下的類找不到時,將rxtx解壓包中的rxtxParallel.dll,rxtxSerial.dll 這兩個文件復(fù)制到C:\Windows\System32 目錄下可解決該錯誤。
2.編寫代碼操作串口:
串口必要參數(shù)類:包含連接串口所必須的參數(shù),方便在調(diào)用串口時設(shè)置和傳遞串口參數(shù)
/**
* 串口必要參數(shù)接收類
* @author: LinWenLi
* @date: 2018年7月21日 下午4:30:40
*/
public class ParamConfig {
private String serialNumber;// 串口號
private int baudRate; // 波特率
private int checkoutBit; // 校驗(yàn)位
private int dataBit; // 數(shù)據(jù)位
private int stopBit; // 停止位
public ParamConfig() {}
/**
* 構(gòu)造方法
* @param serialNumber 串口號
* @param baudRate 波特率
* @param checkoutBit 校驗(yàn)位
* @param dataBit 數(shù)據(jù)位
* @param stopBit 停止位
*/
public ParamConfig(String serialNumber, int baudRate, int checkoutBit, int dataBit, int stopBit) {
this.serialNumber = serialNumber;
this.baudRate = baudRate;
this.checkoutBit = checkoutBit;
this.dataBit = dataBit;
this.stopBit = stopBit;
}
getter()...
setter()...
}
串口操作類:(其中包含的CustomException是自定義異常類,僅用于拋出異常原因。)
import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import gnu.io.UnsupportedCommOperationException;
/**
* 串口參數(shù)的配置 串口一般有如下參數(shù)可以在該串口打開以前進(jìn)行配置: 包括串口號,波特率,輸入/輸出流控制,數(shù)據(jù)位數(shù),停止位和奇偶校驗(yàn)。
*/
// 注:串口操作類一定要繼承SerialPortEventListener
public class SerialPortUtils implements SerialPortEventListener {
// 檢測系統(tǒng)中可用的通訊端口類
private CommPortIdentifier commPortId;
// 枚舉類型
private Enumeration<CommPortIdentifier> portList;
// RS232串口
private SerialPort serialPort;
// 輸入流
private InputStream inputStream;
// 輸出流
private OutputStream outputStream;
// 保存串口返回信息
private String data;
// 保存串口返回信息十六進(jìn)制
private String dataHex;/**
* 初始化串口
* @author LinWenLi
* @date 2018年7月21日下午3:44:16
* @Description: TODO
* @param: paramConfig 存放串口連接必要參數(shù)的對象(會在下方給出類代碼)
* @return: void
* @throws
*/
@SuppressWarnings("unchecked")
public void init(ParamConfig paramConfig) {
// 獲取系統(tǒng)中所有的通訊端口
portList = CommPortIdentifier.getPortIdentifiers();
// 記錄是否含有指定串口
boolean isExsist = false;
// 循環(huán)通訊端口
while (portList.hasMoreElements()) {
commPortId = portList.nextElement();
// 判斷是否是串口
if (commPortId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
// 比較串口名稱是否是指定串口
if (paramConfig.getSerialNumber().equals(commPortId.getName())) {
// 串口存在
isExsist = true;
// 打開串口
try {
// open:(應(yīng)用程序名【隨意命名】,阻塞時等待的毫秒數(shù))
serialPort = (SerialPort) commPortId.open(Object.class.getSimpleName(), 2000);
// 設(shè)置串口監(jiān)聽
serialPort.addEventListener(this);
// 設(shè)置串口數(shù)據(jù)時間有效(可監(jiān)聽)
serialPort.notifyOnDataAvailable(true);
// 設(shè)置串口通訊參數(shù):波特率,數(shù)據(jù)位,停止位,校驗(yàn)方式
serialPort.setSerialPortParams(paramConfig.getBaudRate(), paramConfig.getDataBit(),
paramConfig.getStopBit(), paramConfig.getCheckoutBit());
} catch (PortInUseException e) {
throw new CustomException("端口被占用");
} catch (TooManyListenersException e) {
throw new CustomException("監(jiān)聽器過多");
} catch (UnsupportedCommOperationException e) {
throw new CustomException("不支持的COMM端口操作異常");
}
// 結(jié)束循環(huán)
break;
}
}
}
// 若不存在該串口則拋出異常
if (!isExsist) {
throw new CustomException("不存在該串口!");
}
}
/**
* 實(shí)現(xiàn)接口SerialPortEventListener中的方法 讀取從串口中接收的數(shù)據(jù)
*/
@Override
public void serialEvent(SerialPortEvent event) {
switch (event.getEventType()) {
case SerialPortEvent.BI: // 通訊中斷
case SerialPortEvent.OE: // 溢位錯誤
case SerialPortEvent.FE: // 幀錯誤
case SerialPortEvent.PE: // 奇偶校驗(yàn)錯誤
case SerialPortEvent.CD: // 載波檢測
case SerialPortEvent.CTS: // 清除發(fā)送
case SerialPortEvent.DSR: // 數(shù)據(jù)設(shè)備準(zhǔn)備好
case SerialPortEvent.RI: // 響鈴偵測
case SerialPortEvent.OUTPUT_BUFFER_EMPTY: // 輸出緩沖區(qū)已清空
break;
case SerialPortEvent.DATA_AVAILABLE: // 有數(shù)據(jù)到達(dá)
// 調(diào)用讀取數(shù)據(jù)的方法
readComm();
break;
default:
break;
}
}
/**
* 讀取串口返回信息
* @author LinWenLi
* @date 2018年7月21日下午3:43:04
* @return: void
*/
public void readComm() {
try {
inputStream = serialPort.getInputStream();
// 通過輸入流對象的available方法獲取數(shù)組字節(jié)長度
byte[] readBuffer = new byte[inputStream.available()];
// 從線路上讀取數(shù)據(jù)流
int len = 0;
while ((len = inputStream.read(readBuffer)) != -1) { // 直接獲取到的數(shù)據(jù)
data = new String(readBuffer, 0, len).trim(); // 轉(zhuǎn)為十六進(jìn)制數(shù)據(jù)
dataHex = bytesToHexString(readBuffer);
System.out.println("data:" + data);
System.out.println("dataHex:" + dataHex);// 讀取后置空流對象
inputStream.close();
inputStream = null;
break;
}
} catch (IOException e) {
throw new CustomException("讀取串口數(shù)據(jù)時發(fā)生IO異常");
}
}
/**
* 發(fā)送信息到串口
* @author LinWenLi
* @date 2018年7月21日下午3:45:22
* @param: data
* @return: void
* @throws
*/
public void sendComm(String data) {
byte[] writerBuffer = null;
try {
writerBuffer = hexToByteArray(data);
} catch (NumberFormatException e) {
throw new CustomException("命令格式錯誤!");
}
try {
outputStream = serialPort.getOutputStream();
outputStream.write(writerBuffer);
outputStream.flush();
} catch (NullPointerException e) {
throw new CustomException("找不到串口。");
} catch (IOException e) {
throw new CustomException("發(fā)送信息到串口時發(fā)生IO異常");
}
}
/**
* 關(guān)閉串口
* @author LinWenLi
* @date 2018年7月21日下午3:45:43
* @Description: 關(guān)閉串口
* @param:
* @return: void
* @throws
*/
public void closeSerialPort() {
if (serialPort != null) {
serialPort.notifyOnDataAvailable(false);
serialPort.removeEventListener();
if (inputStream != null) {
try {
inputStream.close();
inputStream = null;
} catch (IOException e) {
throw new CustomException("關(guān)閉輸入流時發(fā)生IO異常");
}
}
if (outputStream != null) {
try {
outputStream.close();
outputStream = null;
} catch (IOException e) {
throw new CustomException("關(guān)閉輸出流時發(fā)生IO異常");
}
}
serialPort.close();
serialPort = null;
}
}
/**
* 十六進(jìn)制串口返回值獲取
*/
public String getDataHex() {
String result = dataHex;
// 置空執(zhí)行結(jié)果
dataHex = null;
// 返回執(zhí)行結(jié)果
return result;
}
/**
* 串口返回值獲取
*/
public String getData() {
String result = data;
// 置空執(zhí)行結(jié)果
data = null;
// 返回執(zhí)行結(jié)果
return result;
}
/**
* Hex字符串轉(zhuǎn)byte
* @param inHex 待轉(zhuǎn)換的Hex字符串
* @return 轉(zhuǎn)換后的byte
*/
public static byte hexToByte(String inHex) {
return (byte) Integer.parseInt(inHex, 16);
}
/**
* hex字符串轉(zhuǎn)byte數(shù)組
* @param inHex 待轉(zhuǎn)換的Hex字符串
* @return 轉(zhuǎn)換后的byte數(shù)組結(jié)果
*/
public static byte[] hexToByteArray(String inHex) {
int hexlen = inHex.length();
byte[] result;
if (hexlen % 2 == 1) {
// 奇數(shù)
hexlen++;
result = new byte[(hexlen / 2)];
inHex = "0" + inHex;
} else {
// 偶數(shù)
result = new byte[(hexlen / 2)];
}
int j = 0;
for (int i = 0; i < hexlen; i += 2) {
result[j] = hexToByte(inHex.substring(i, i + 2));
j++;
}
return result;
}
/**
* 數(shù)組轉(zhuǎn)換成十六進(jìn)制字符串
* @param byte[]
* @return HexString
*/
public static final String bytesToHexString(byte[] bArray) {
StringBuffer sb = new StringBuffer(bArray.length);
String sTemp;
for (int i = 0; i < bArray.length; i++) {
sTemp = Integer.toHexString(0xFF & bArray[i]);
if (sTemp.length() < 2)
sb.append(0);
sb.append(sTemp.toUpperCase());
}
return sb.toString();
}
}
調(diào)用串口操作類的代碼:
public static void main(String[] args) {
// 實(shí)例化串口操作類對象
SerialPortUtils serialPort = new SerialPortUtils();
// 創(chuàng)建串口必要參數(shù)接收類并賦值,賦值串口號,波特率,校驗(yàn)位,數(shù)據(jù)位,停止位
ParamConfig paramConfig = new ParamConfig("COM4", 9600, 0, 8, 1);
// 初始化設(shè)置,打開串口,開始監(jiān)聽讀取串口數(shù)據(jù)
serialPort.init(paramConfig);
// 調(diào)用串口操作類的sendComm方法發(fā)送數(shù)據(jù)到串口
serialPort.sendComm("FEF10A000000000000000AFF");
// 關(guān)閉串口
serialPort.closeSerialPort();
}
當(dāng)執(zhí)行main方法中的代碼且未執(zhí)行關(guān)閉串口時,程序?qū)⒁恢碧幱陂_啟狀態(tài),自動監(jiān)聽,接收讀取來自串口的數(shù)據(jù)。
注意:一個串口只能打開一次,并只支持一個程序調(diào)用。
以上所記錄的是簡單測試java是否能成功操作串口數(shù)據(jù),至于本人所寫的Web端的讀卡器調(diào)試功能則是在串口操作類的基礎(chǔ)上編寫網(wǎng)頁界面,通過請求來控制串口的開啟關(guān)閉及相應(yīng)的設(shè)置,功能比較簡單,放個界面記錄一下:


到此這篇關(guān)于使用Java實(shí)現(xiàn)簡單串口通信的文章就介紹到這了,更多相關(guān)Java實(shí)現(xiàn)簡單串口通信內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Boot引入swagger-ui 后swagger-ui.html無法訪問404的問題
這篇文章主要介紹了Spring Boot引入swagger-ui 后swagger-ui.html無法訪問404的問題及解決方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
java.lang.NullPointerException 如何處理空指針異常的實(shí)現(xiàn)
這篇文章主要介紹了java.lang.NullPointerException 如何處理空指針異常的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
java 輸入一個數(shù)字,反轉(zhuǎn)輸出這個數(shù)字的值(實(shí)現(xiàn)方法)
下面小編就為大家?guī)硪黄猨ava 輸入一個數(shù)字,反轉(zhuǎn)輸出這個數(shù)字的值(實(shí)現(xiàn)方法)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-10-10
knife4j+springboot3.4異常無法正確展示文檔
本文主要介紹了knife4j+springboot3.4異常無法正確展示文檔,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01
Java使用抽象工廠模式實(shí)現(xiàn)的肯德基消費(fèi)案例詳解
這篇文章主要介紹了Java使用抽象工廠模式實(shí)現(xiàn)的肯德基消費(fèi)案例,較為詳細(xì)的分析了抽象工廠模式的定義、原理并結(jié)合實(shí)例形式分析了Java使用抽象工廠模式實(shí)現(xiàn)肯德基消費(fèi)案例的步驟與相關(guān)操作技巧,需要的朋友可以參考下2018-05-05
Java實(shí)現(xiàn)一鍵生成表controller,service,mapper文件
這篇文章主要為大家詳細(xì)介紹了如何利用Java語言實(shí)現(xiàn)一鍵生成表controller,service,mapper文件,文中的示例代碼講解詳細(xì),需要的可以收藏一下2023-05-05
5分鐘搭建SpringCloud Eureka服務(wù)注冊中心的實(shí)現(xiàn)
這篇文章主要介紹了5分鐘搭建SpringCloud Eureka服務(wù)注冊中心的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
spring-boot-maven-plugin:unknown的完美解決方法
這篇文章主要介紹了spring-boot-maven-plugin:unknown的完美解決方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11

