使用Java進行串口SerialPort通訊


1.准備工作

       在進行串口連接通訊前,必須保證你當前操作電腦上有可用且閑置的串口。因為一般的電腦上只有一個或者兩個串口,如COM1或COM2,但大多數情況下,這些串口可能會被其他的程序或者應用所占用,所以此時我們可以借助工具,在我們的電腦上建立虛擬串口。
       先下載一個叫Virtual Serial Port的軟件,通過這個軟件可以創建新的虛擬串口,如下圖所示:
通過圖中的添加串口即可進行新虛擬串口的創建,並且一般都是成對創建。
 
有了虛擬串口之后,為了方便測試新建串口是否可用,我們可以再下載一款串口測試軟件,我用的是一個串口調試助手,如下圖:
通過測試,發現我所添加的虛擬串口COM3和COM4是可以用的,那么接下來,我們就可以關閉其中一個,通過我們的JAVA程序來進行與該虛擬串口的信息交互了。
 

 

2.JAVA程序的操作

在進行程序編寫之前,我們需要下載幾個文件,才能使我們的程序進行串口通訊的操作。
     首先需要下載如下圖中的三個文件:
並將RXTXcomm.jar這個包通過集成工具的構建路徑添加到我們的項目之中;其次將 rxtxParallel.dll 和 rxtxSerial.dll 這兩個文件放到我們的JDK安裝目錄中的bin文件夾中。
     這兩個文件必須添加到指定位置,否則后面程序在運行時將會報錯找不到文件。(需要注意:這幾個文件按操作系統分32位和64位,各位需要根據自己電腦的系統下載對應的版本,不然也會報錯).
 
接下來可以開始我們的代碼編寫了,為了方便使用,我們肯定是需要有一個SerialPort串口工具類,如下:
  1.  
    package com.cams.CaMSMobileService.SerialPort;
  2.  
     
  3.  
    import gnu.io.CommPort;
  4.  
    import gnu.io.CommPortIdentifier;
  5.  
    import gnu.io.NoSuchPortException;
  6.  
    import gnu.io.PortInUseException;
  7.  
    import gnu.io.SerialPort;
  8.  
    import gnu.io.SerialPortEventListener;
  9.  
    import gnu.io.UnsupportedCommOperationException;
  10.  
     
  11.  
    import java.io.IOException;
  12.  
    import java.io.InputStream;
  13.  
    import java.io.OutputStream;
  14.  
    import java.util.ArrayList;
  15.  
    import java.util.Enumeration;
  16.  
    import java.util.TooManyListenersException;
  17.  
     
  18.  
    import com.cams.CaMSMobileService.SerialPort.exception.NoSuchPort;
  19.  
    import com.cams.CaMSMobileService.SerialPort.exception.NotASerialPort;
  20.  
    import com.cams.CaMSMobileService.SerialPort.exception.PortInUse;
  21.  
    import com.cams.CaMSMobileService.SerialPort.exception.ReadDataFromSerialPortFailure;
  22.  
    import com.cams.CaMSMobileService.SerialPort.exception.SendDataToSerialPortFailure;
  23.  
    import com.cams.CaMSMobileService.SerialPort.exception.SerialPortInputStreamCloseFailure;
  24.  
    import com.cams.CaMSMobileService.SerialPort.exception.SerialPortOutputStreamCloseFailure;
  25.  
    import com.cams.CaMSMobileService.SerialPort.exception.SerialPortParameterFailure;
  26.  
    import com.cams.CaMSMobileService.SerialPort.exception.TooManyListeners;
  27.  
     
  28.  
    public class SerialPortManager {
  29.  
     
  30.  
    /**
  31.  
    * 查找所有可用端口
  32.  
    *
  33.  
    * @return 可用端口名稱列表
  34.  
    */
  35.  
    @SuppressWarnings("unchecked")
  36.  
    public static final ArrayList<String> findPort() {
  37.  
    // 獲得當前所有可用串口
  38.  
    Enumeration<CommPortIdentifier> portList = CommPortIdentifier
  39.  
    .getPortIdentifiers();
  40.  
    ArrayList<String> portNameList = new ArrayList<String>();
  41.  
    // 將可用串口名添加到List並返回該List
  42.  
    while (portList.hasMoreElements()) {
  43.  
    String portName = portList.nextElement().getName();
  44.  
    portNameList.add(portName);
  45.  
    }
  46.  
    return portNameList;
  47.  
    }
  48.  
     
  49.  
    /**
  50.  
    * 打開串口
  51.  
    *
  52.  
    * @param portName
  53.  
    * 端口名稱
  54.  
    * @param baudrate
  55.  
    * 波特率
  56.  
    * @return 串口對象
  57.  
    * @throws SerialPortParameterFailure
  58.  
    * 設置串口參數失敗
  59.  
    * @throws NotASerialPort
  60.  
    * 端口指向設備不是串口類型
  61.  
    * @throws NoSuchPort
  62.  
    * 沒有該端口對應的串口設備
  63.  
    * @throws PortInUse
  64.  
    * 端口已被占用
  65.  
    */
  66.  
    public static final SerialPort openPort(String portName, int baudrate)
  67.  
    throws SerialPortParameterFailure, NotASerialPort, NoSuchPort,
  68.  
    PortInUse {
  69.  
    try {
  70.  
    // 通過端口名識別端口
  71.  
    CommPortIdentifier portIdentifier = CommPortIdentifier
  72.  
    .getPortIdentifier(portName);
  73.  
    // 打開端口,設置端口名與timeout(打開操作的超時時間)
  74.  
    CommPort commPort = portIdentifier.open(portName, 2000);
  75.  
    // 判斷是不是串口
  76.  
    if (commPort instanceof SerialPort) {
  77.  
    SerialPort serialPort = (SerialPort) commPort;
  78.  
     
  79.  
    try {
  80.  
    // 設置串口的波特率等參數
  81.  
    serialPort.setSerialPortParams(baudrate,
  82.  
    SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
  83.  
    SerialPort.PARITY_NONE);
  84.  
    } catch (UnsupportedCommOperationException e) {
  85.  
    throw new SerialPortParameterFailure();
  86.  
    }
  87.  
    return serialPort;
  88.  
    } else {
  89.  
    // 不是串口
  90.  
    throw new NotASerialPort();
  91.  
    }
  92.  
    } catch (NoSuchPortException e1) {
  93.  
    throw new NoSuchPort();
  94.  
    } catch (PortInUseException e2) {
  95.  
    throw new PortInUse();
  96.  
    }
  97.  
    }
  98.  
     
  99.  
    /**
  100.  
    * 關閉串口
  101.  
    *
  102.  
    * @param serialport
  103.  
    * 待關閉的串口對象
  104.  
    */
  105.  
    public static void closePort(SerialPort serialPort) {
  106.  
    if (serialPort != null) {
  107.  
    serialPort.close();
  108.  
    serialPort = null;
  109.  
    }
  110.  
    }
  111.  
     
  112.  
    /**
  113.  
    * 向串口發送數據
  114.  
    *
  115.  
    * @param serialPort
  116.  
    * 串口對象
  117.  
    * @param order
  118.  
    * 待發送數據
  119.  
    * @throws SendDataToSerialPortFailure
  120.  
    * 向串口發送數據失敗
  121.  
    * @throws SerialPortOutputStreamCloseFailure
  122.  
    * 關閉串口對象的輸出流出錯
  123.  
    */
  124.  
    public static void sendToPort(SerialPort serialPort, byte[] order)
  125.  
    throws SendDataToSerialPortFailure,
  126.  
    SerialPortOutputStreamCloseFailure {
  127.  
    OutputStream out = null;
  128.  
    try {
  129.  
    out = serialPort.getOutputStream();
  130.  
    out.write(order);
  131.  
    out.flush();
  132.  
    } catch (IOException e) {
  133.  
    throw new SendDataToSerialPortFailure();
  134.  
    } finally {
  135.  
    try {
  136.  
    if (out != null) {
  137.  
    out.close();
  138.  
    out = null;
  139.  
    }
  140.  
    } catch (IOException e) {
  141.  
    throw new SerialPortOutputStreamCloseFailure();
  142.  
    }
  143.  
    }
  144.  
    }
  145.  
     
  146.  
    /**
  147.  
    * 從串口讀取數據
  148.  
    *
  149.  
    * @param serialPort
  150.  
    * 當前已建立連接的SerialPort對象
  151.  
    * @return 讀取到的數據
  152.  
    * @throws ReadDataFromSerialPortFailure
  153.  
    * 從串口讀取數據時出錯
  154.  
    * @throws SerialPortInputStreamCloseFailure
  155.  
    * 關閉串口對象輸入流出錯
  156.  
    */
  157.  
    public static byte[] readFromPort(SerialPort serialPort)
  158.  
    throws ReadDataFromSerialPortFailure,
  159.  
    SerialPortInputStreamCloseFailure {
  160.  
    InputStream in = null;
  161.  
    byte[] bytes = null;
  162.  
    try {
  163.  
    in = serialPort.getInputStream();
  164.  
    // 獲取buffer里的數據長度
  165.  
    int bufflenth = in.available();
  166.  
    while (bufflenth != 0) {
  167.  
    // 初始化byte數組為buffer中數據的長度
  168.  
    bytes = new byte[bufflenth];
  169.  
    in.read(bytes);
  170.  
    bufflenth = in.available();
  171.  
    }
  172.  
    } catch (IOException e) {
  173.  
    throw new ReadDataFromSerialPortFailure();
  174.  
    } finally {
  175.  
    try {
  176.  
    if (in != null) {
  177.  
    in.close();
  178.  
    in = null;
  179.  
    }
  180.  
    } catch (IOException e) {
  181.  
    throw new SerialPortInputStreamCloseFailure();
  182.  
    }
  183.  
    }
  184.  
    return bytes;
  185.  
    }
  186.  
     
  187.  
    /**
  188.  
    * 添加監聽器
  189.  
    *
  190.  
    * @param port
  191.  
    * 串口對象
  192.  
    * @param listener
  193.  
    * 串口監聽器
  194.  
    * @throws TooManyListeners
  195.  
    * 監聽類對象過多
  196.  
    */
  197.  
    public static void addListener(SerialPort port,
  198.  
    SerialPortEventListener listener) throws TooManyListeners {
  199.  
    try {
  200.  
    // 給串口添加監聽器
  201.  
    port.addEventListener(listener);
  202.  
    // 設置當有數據到達時喚醒監聽接收線程
  203.  
    port.notifyOnDataAvailable( true);
  204.  
    // 設置當通信中斷時喚醒中斷線程
  205.  
    port.notifyOnBreakInterrupt( true);
  206.  
    } catch (TooManyListenersException e) {
  207.  
    throw new TooManyListeners();
  208.  
    }
  209.  
    }
  210.  
    }
有了工具類之后,接下來我們就可以開始測試串口通訊了,如下:
 
到這里串口通訊測試的也差不多了。可能自己做的項目中需要自己做兩個串口,一個client端和一個server端。為了使串口操作變的可視化,大家也可以使用JFrame來進行操作窗口模擬,這里就不做多的介紹了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM