使用Java實(shí)現(xiàn)簡(jiǎn)單串口通信
本博文參考自http://chabaoo.cn/article/100269.htm
沒(méi)想到挺多人需要這個(gè)的,很高興這篇文章能對(duì)大家有幫助,主要的工具類(lèi)博文里已經(jīng)有了,當(dāng)然,要小工具源碼的留言郵箱即可。 2019.09.05
最近接觸到了串口及其讀寫(xiě),在此記錄java進(jìn)行串口讀寫(xiě)的過(guò)程。
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這個(gè)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)行過(guò)程中拋出java.lang.UnsatisfiedLinkError錯(cuò)誤或gnu.io下的類(lèi)找不到時(shí),將rxtx解壓包中的rxtxParallel.dll,rxtxSerial.dll 這兩個(gè)文件復(fù)制到C:\Windows\System32 目錄下可解決該錯(cuò)誤。
2.編寫(xiě)代碼操作串口:
串口必要參數(shù)類(lèi):包含連接串口所必須的參數(shù),方便在調(diào)用串口時(shí)設(shè)置和傳遞串口參數(shù)
/**
* 串口必要參數(shù)接收類(lèi)
* @author: LinWenLi
* @date: 2018年7月21日 下午4:30:40
*/
public class ParamConfig {
private String serialNumber;// 串口號(hào)
private int baudRate; // 波特率
private int checkoutBit; // 校驗(yàn)位
private int dataBit; // 數(shù)據(jù)位
private int stopBit; // 停止位
public ParamConfig() {}
/**
* 構(gòu)造方法
* @param serialNumber 串口號(hào)
* @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()...
}
串口操作類(lèi):(其中包含的CustomException是自定義異常類(lèi),僅用于拋出異常原因。)
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ù)可以在該串口打開(kāi)以前進(jìn)行配置: 包括串口號(hào),波特率,輸入/輸出流控制,數(shù)據(jù)位數(shù),停止位和奇偶校驗(yàn)。
*/
// 注:串口操作類(lèi)一定要繼承SerialPortEventListener
public class SerialPortUtils implements SerialPortEventListener {
// 檢測(cè)系統(tǒng)中可用的通訊端口類(lèi)
private CommPortIdentifier commPortId;
// 枚舉類(lèi)型
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ù)的對(duì)象(會(huì)在下方給出類(lèi)代碼)
* @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;
// 打開(kāi)串口
try {
// open:(應(yīng)用程序名【隨意命名】,阻塞時(shí)等待的毫秒數(shù))
serialPort = (SerialPort) commPortId.open(Object.class.getSimpleName(), 2000);
// 設(shè)置串口監(jiān)聽(tīng)
serialPort.addEventListener(this);
// 設(shè)置串口數(shù)據(jù)時(shí)間有效(可監(jiān)聽(tīng))
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)聽(tīng)器過(guò)多");
} 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: // 溢位錯(cuò)誤
case SerialPortEvent.FE: // 幀錯(cuò)誤
case SerialPortEvent.PE: // 奇偶校驗(yàn)錯(cuò)誤
case SerialPortEvent.CD: // 載波檢測(cè)
case SerialPortEvent.CTS: // 清除發(fā)送
case SerialPortEvent.DSR: // 數(shù)據(jù)設(shè)備準(zhǔn)備好
case SerialPortEvent.RI: // 響鈴偵測(cè)
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();
// 通過(guò)輸入流對(duì)象的available方法獲取數(shù)組字節(jié)長(zhǎng)度
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);// 讀取后置空流對(duì)象
inputStream.close();
inputStream = null;
break;
}
} catch (IOException e) {
throw new CustomException("讀取串口數(shù)據(jù)時(shí)發(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("命令格式錯(cuò)誤!");
}
try {
outputStream = serialPort.getOutputStream();
outputStream.write(writerBuffer);
outputStream.flush();
} catch (NullPointerException e) {
throw new CustomException("找不到串口。");
} catch (IOException e) {
throw new CustomException("發(fā)送信息到串口時(shí)發(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)閉輸入流時(shí)發(fā)生IO異常");
}
}
if (outputStream != null) {
try {
outputStream.close();
outputStream = null;
} catch (IOException e) {
throw new CustomException("關(guān)閉輸出流時(shí)發(fā)生IO異常");
}
}
serialPort.close();
serialPort = null;
}
}
/**
* 十六進(jìn)制串口返回值獲取
*/
public String getDataHex() {
String result = dataHex;
// 置空?qǐng)?zhí)行結(jié)果
dataHex = null;
// 返回執(zhí)行結(jié)果
return result;
}
/**
* 串口返回值獲取
*/
public String getData() {
String result = data;
// 置空?qǐng)?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)用串口操作類(lèi)的代碼:
public static void main(String[] args) {
// 實(shí)例化串口操作類(lèi)對(duì)象
SerialPortUtils serialPort = new SerialPortUtils();
// 創(chuàng)建串口必要參數(shù)接收類(lèi)并賦值,賦值串口號(hào),波特率,校驗(yàn)位,數(shù)據(jù)位,停止位
ParamConfig paramConfig = new ParamConfig("COM4", 9600, 0, 8, 1);
// 初始化設(shè)置,打開(kāi)串口,開(kāi)始監(jiān)聽(tīng)讀取串口數(shù)據(jù)
serialPort.init(paramConfig);
// 調(diào)用串口操作類(lèi)的sendComm方法發(fā)送數(shù)據(jù)到串口
serialPort.sendComm("FEF10A000000000000000AFF");
// 關(guān)閉串口
serialPort.closeSerialPort();
}
當(dāng)執(zhí)行main方法中的代碼且未執(zhí)行關(guān)閉串口時(shí),程序?qū)⒁恢碧幱陂_(kāi)啟狀態(tài),自動(dòng)監(jiān)聽(tīng),接收讀取來(lái)自串口的數(shù)據(jù)。
注意:一個(gè)串口只能打開(kāi)一次,并只支持一個(gè)程序調(diào)用。
以上所記錄的是簡(jiǎn)單測(cè)試java是否能成功操作串口數(shù)據(jù),至于本人所寫(xiě)的Web端的讀卡器調(diào)試功能則是在串口操作類(lèi)的基礎(chǔ)上編寫(xiě)網(wǎng)頁(yè)界面,通過(guò)請(qǐng)求來(lái)控制串口的開(kāi)啟關(guān)閉及相應(yīng)的設(shè)置,功能比較簡(jiǎn)單,放個(gè)界面記錄一下:


到此這篇關(guān)于使用Java實(shí)現(xiàn)簡(jiǎn)單串口通信的文章就介紹到這了,更多相關(guān)Java實(shí)現(xiàn)簡(jiǎn)單串口通信內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot單獨(dú)使用feign簡(jiǎn)化接口調(diào)用方式
這篇文章主要介紹了springboot單獨(dú)使用feign簡(jiǎn)化接口調(diào)用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
springMVC如何對(duì)輸入數(shù)據(jù)校驗(yàn)實(shí)現(xiàn)代碼
數(shù)據(jù)的校驗(yàn)是交互式網(wǎng)站一個(gè)不可或缺的功能,數(shù)據(jù)驗(yàn)證分為客戶端驗(yàn)證和服務(wù)器端驗(yàn)證,這篇文章主要介紹了springMVC如何對(duì)輸入數(shù)據(jù)校驗(yàn),需要的朋友可以參考下2020-10-10
使用gRPC微服務(wù)的內(nèi)部通信優(yōu)化
這篇文章主要為大家介紹了微服務(wù)優(yōu)化之使用gRPC做微服務(wù)的內(nèi)部通信,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03
Android Home鍵監(jiān)聽(tīng)的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android Home 鍵監(jiān)聽(tīng)的實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
SpringBoot 單元測(cè)試實(shí)戰(zhàn)(Mockito,MockBean)
這篇文章主要介紹了SpringBoot 單元測(cè)試實(shí)戰(zhàn)(Mockito,MockBean),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
java web FTPClient實(shí)現(xiàn)上傳文件到指定服務(wù)器
這篇文章主要為大家詳細(xì)介紹了java web FTPClient實(shí)現(xiàn)上傳文件到指定服務(wù)器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06

