溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Java實(shí)現(xiàn)簡(jiǎn)單串口通信的方法

發(fā)布時(shí)間:2020-07-23 13:58:00 來(lái)源:億速云 閱讀:172 作者:小豬 欄目:開發(fā)技術(shù)

這篇文章主要講解了Java實(shí)現(xiàn)簡(jiǎn)單串口通信的方法,內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。

沒(méi)想到挺多人需要這個(gè)的,很高興這篇文章能對(duì)大家有幫助,主要的工具類博文里已經(jīng)有了,當(dāng)然,要小工具源碼的留言郵箱即可。 2019.09.05

最近接觸到了串口及其讀寫,在此記錄java進(jìn)行串口讀寫的過(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下的類找不到時(shí),將rxtx解壓包中的rxtxParallel.dll,rxtxSerial.dll 這兩個(gè)文件復(fù)制到C:\Windows\System32 目錄下可解決該錯(cuò)誤。

2.編寫代碼操作串口:

串口必要參數(shù)類:包含連接串口所必須的參數(shù),方便在調(diào)用串口時(shí)設(shè)置和傳遞串口參數(shù)

/**
 * 串口必要參數(shù)接收類
 * @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()...
}

串口操作類:(其中包含的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)行配置: 包括串口號(hào),波特率,輸入/輸出流控制,數(shù)據(jù)位數(shù),停止位和奇偶校驗(yàn)。
 */
// 注:串口操作類一定要繼承SerialPortEventListener
public class SerialPortUtils implements SerialPortEventListener {
 // 檢測(cè)系統(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ù)的對(duì)象(會(huì)在下方給出類代碼) 
 * @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í)等待的毫秒數(shù))
   serialPort = (SerialPort) commPortId.open(Object.class.getSimpleName(), 2000);
   // 設(shè)置串口監(jiān)聽
   serialPort.addEventListener(this);
   // 設(shè)置串口數(shù)據(jù)時(shí)間有效(可監(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)聽器過(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)用串口操作類的代碼:

public static void main(String[] args) {
  // 實(shí)例化串口操作類對(duì)象
  SerialPortUtils serialPort = new SerialPortUtils();
  // 創(chuàng)建串口必要參數(shù)接收類并賦值,賦值串口號(hào),波特率,校驗(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)閉串口時(shí),程序?qū)⒁恢碧幱陂_啟狀態(tài),自動(dòng)監(jiān)聽,接收讀取來(lái)自串口的數(shù)據(jù)。

注意:一個(gè)串口只能打開一次,并只支持一個(gè)程序調(diào)用。

以上所記錄的是簡(jiǎn)單測(cè)試java是否能成功操作串口數(shù)據(jù),至于本人所寫的Web端的讀卡器調(diào)試功能則是在串口操作類的基礎(chǔ)上編寫網(wǎng)頁(yè)界面,通過(guò)請(qǐng)求來(lái)控制串口的開啟關(guān)閉及相應(yīng)的設(shè)置,功能比較簡(jiǎn)單,放個(gè)界面記錄一下:

Java實(shí)現(xiàn)簡(jiǎn)單串口通信的方法

Java實(shí)現(xiàn)簡(jiǎn)單串口通信的方法

看完上述內(nèi)容,是不是對(duì)Java實(shí)現(xiàn)簡(jiǎn)單串口通信的方法有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI