java串口通訊(三)---- RXTX實現串口通訊(代碼實現)

開發環境Spring Boot 

通訊設備讀卡器

協議波特率:19200

 

  • 1.測試接口代碼

    @Autowired
    private CommunicationUtils communicationUtils;


    /**
     * 執行指令
     * @param comNo 串口號
     * @param instruct 指令
     * @return  接收指令
     */
    @RequestMapping(value = "/get/address", method = RequestMethod.GET)
    public String readAddress(@RequestParam(value = "comNo", required = true) String comNo,
                                          @RequestParam(value = "instruct", required = false) String instruct) {
        return communicationUtils.send(instruct, comNo);
    }
  • 2.串口管理類
import gnu.io.*;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.TooManyListenersException;

/**
 * 串口管理
 * 
 * @author yangle
 */
@SuppressWarnings("all")
@Slf4j
public class SerialPortManager {

	/**
	 * 查找所有可用端口
	 * @return 可用端口名稱列表
	 */
	public static final List<String> findPorts() {
		// 獲得當前所有可用串口
		Enumeration<CommPortIdentifier> portList = CommPortIdentifier.getPortIdentifiers();
		List<String> portNameList = new ArrayList<String>();
		// 將可用串口名添加到List並返回該List
		while (portList.hasMoreElements()) {
			String portName = portList.nextElement().getName();
			portNameList.add(portName);
		}
		return portNameList;
	}

	/**
	 * 打開串口
	 * 
	 * @param portName
	 *            端口名稱
	 * @param baudrate
	 *            波特率
	 * @return 串口對象
	 * @throws PortInUseException
	 *             串口已被佔用
	 */
	public static final SerialPort openPort(String portName, int baudrate) throws PortInUseException {
		try {
			// 通過端口名識別端口
			CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
			// 打開端口,並給端口名字和一個timeout(打開操作的超時時間)
			CommPort commPort = portIdentifier.open(portName, 2000);
			// 判斷是不是串口
			if (commPort instanceof SerialPort) {
				SerialPort serialPort = (SerialPort) commPort;
				try {
					// 設置一下串口的波特率等參數
					// 數據位:8
					// 停止位:1
					// 校驗位:None
					serialPort.setSerialPortParams(baudrate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
							SerialPort.PARITY_NONE);
				} catch (UnsupportedCommOperationException e) {
					e.printStackTrace();
				}
				return serialPort;
			}
		} catch (NoSuchPortException e1) {
			e1.printStackTrace();
		}
		return null;
	}

	/**
	 * 關閉串口
	 * 
	 * @param serialport
	 *            待關閉的串口對象
	 */
	public static void closePort(SerialPort serialPort) {
		if (serialPort != null) {
			serialPort.close();
		}
	}

	/**
	 * 往串口發送數據
	 * 
	 * @param serialPort
	 *            串口對象
	 * @param order
	 *            待發送數據
	 */
	public static void sendToPort(SerialPort serialPort, byte[] order) {
		OutputStream out = null;
		try {
			out = serialPort.getOutputStream();
			out.write(order);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.close();
					out = null;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 從串口讀取數據
	 * 
	 * @param serialPort
	 *            當前已建立連接的SerialPort對象
	 * @return 讀取到的數據
	 */
	public static byte[] readFromPort(SerialPort serialPort) {
		InputStream in = null;
		byte[] bytes = {};
		try {
			in = serialPort.getInputStream();
			// 緩衝區大小爲一個字節
			byte[] readBuffer = new byte[1];
			int bytesNum = in.read(readBuffer);
			while (bytesNum > 0) {
				bytes = ArrayUtils.concat(bytes, readBuffer);
				bytesNum = in.read(readBuffer);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (in != null) {
					in.close();
					in = null;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return bytes;
	}

	/**
	 * 添加監聽器
	 * 
	 * @param port
	 *            串口對象
	 * @param listener
	 *            串口存在有效數據監聽
	 */
	public static void addListener(SerialPort serialPort, DataAvailableListener listener) {
		try {
			// 給串口添加監聽器
			serialPort.addEventListener(new SerialPortListener(listener));
			// 設置當有數據到達時喚醒監聽接收線程
			serialPort.notifyOnDataAvailable(true);
			// 設置當通信中斷時喚醒中斷線程
			serialPort.notifyOnBreakInterrupt(true);
		} catch (TooManyListenersException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 串口監聽
	 */
	public static class SerialPortListener implements SerialPortEventListener {

		private DataAvailableListener mDataAvailableListener;

		public SerialPortListener(DataAvailableListener mDataAvailableListener) {
			this.mDataAvailableListener = mDataAvailableListener;
		}

		public void serialEvent(SerialPortEvent serialPortEvent) {
			switch (serialPortEvent.getEventType()) {
			case SerialPortEvent.DATA_AVAILABLE: // 1.串口存在有效數據
				if (mDataAvailableListener != null) {
					mDataAvailableListener.dataAvailable();
				}
				break;

			case SerialPortEvent.OUTPUT_BUFFER_EMPTY: // 2.輸出緩衝區已清空
				break;

			case SerialPortEvent.CTS: // 3.清除待發送數據
				break;

			case SerialPortEvent.DSR: // 4.待發送數據準備好了
				break;

			case SerialPortEvent.RI: // 5.振鈴指示
				break;

			case SerialPortEvent.CD: // 6.載波檢測
				break;

			case SerialPortEvent.OE: // 7.溢位(溢出)錯誤
				break;

			case SerialPortEvent.PE: // 8.奇偶校驗錯誤
				break;

			case SerialPortEvent.FE: // 9.幀錯誤
				break;

			case SerialPortEvent.BI: // 10.通訊中斷
				log.error("與串口設備通訊中斷");
				break;

			default:
				break;
			}
		}
	}

	/**
	 * 串口存在有效數據監聽
	 */
	public interface DataAvailableListener {
		/**
		 * 串口存在有效數據
		 */
		void dataAvailable();
	}

}
  • 3.通訊工具類
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * Coding by 李炯 on 2019/9/2 15:10
 */
@Slf4j
@Component
public class CommunicationUtils {
    public String send(String instruct, String comNo) {
        String[] result = {""};//指令返回結果
        // Setp 1 :init串口對象
        SerialPort mSerialport = null;
        try {
            mSerialport = SerialPortManager.openPort(comNo, 19200);
            // Setp 2 :發送數據
            SerialPortManager.sendToPort(mSerialport, ByteUtils.hexStr2Byte(instruct));
            // Setp 3 :添加串口監聽
            SerialPort finalMSerialport = mSerialport;
            SerialPortManager.addListener(mSerialport, new SerialPortManager.DataAvailableListener() {
                @Override
                public void dataAvailable() {
                    byte[] data = null;
                    try {
                        if (finalMSerialport == null) {
                            log.info("串口對象爲空,監聽失敗!");
                        } else {
                            data = SerialPortManager.readFromPort(finalMSerialport); // 讀取串口數據
                            result[0] = ByteUtils.byteArrayToHexString(data);// 以十六進制的形式接收數據
                        }
                    } catch (Exception e) {
                        log.error(e.toString());
                        // 發生讀取錯誤時顯示錯誤信息後退出系統
                        System.exit(0);
                    }
                }
            });
            // Setp 4 :關閉串口
            Thread.sleep(300);// 這裏是爲了監聽返回的數據,50也可以,保險起見設置200
            SerialPortManager.closePort(mSerialport);
        } catch (PortInUseException e) {
            log.error("串口:" + comNo + " 打開異常");
            log.error(e.getMessage());
        } catch (InterruptedException e) {
            log.error("Thread.sleep 打開異常");
        }
        return result[0];
    }
}
  • 3.字節數組轉換類
import lombok.extern.slf4j.Slf4j;

import java.nio.ByteBuffer;
import java.util.Locale;

/**
 * Byte轉換工具
 * 
 * @author yangle
 */
@Slf4j
public class ByteUtils {

	/**
	 * 十六進制字符串轉byte[]
	 * 作用:用於發送指令(給串口設備)
	 * @param hex 十六進制字符串
	 * @return byte[]
	 */
	public static byte[] hexStr2Byte(String hex) {
		if (hex == null) {
			return new byte[] {};
		}
		// 奇數位補0
		if (hex.length() % 2 != 0) {
			hex = "0" + hex;
		}
		int length = hex.length();
		ByteBuffer buffer = ByteBuffer.allocate(length / 2);
		int index=0;
		String indexStr="";
		try {
			for (int i = 0; i < length; i++) {
				String hexStr = hex.charAt(i) + "";
				i++;
				hexStr += hex.charAt(i);
				indexStr= hexStr;
				index = i;
				byte b = (byte) Integer.parseInt(hexStr, 16);
				buffer.put(b);
				log.debug("ByteUtils.hexStr2Byte ----> indexStr:"+indexStr);
				log.debug("ByteUtils.hexStr2Byte ----> b:"+b);
			}
		}catch (Exception e){
			log.error("ByteUtils.hexStr2Byte ----> indexStr:"+indexStr);
			log.error("ByteUtils.hexStr2Byte ----> index:"+index);
		}

		return buffer.array();
	}

	/**
	 * byte[]轉十六進制字符串
	 * 作用:用於接收指令(接收的byte[]轉換成HEX)
	 * @param array byte[]
	 * @return 十六進制字符串
	 */
	public static String byteArrayToHexString(byte[] array) {
		if (array == null) {
			return "";
		}
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < array.length; i++) {
			buffer.append(byteToHex(array[i]));
		}
		return buffer.toString();
	}

	/**
	 * byte轉十六進制字符
	 * @param b byte
	 * @return 十六進制字符
	 */
	public static String byteToHex(byte b) {
		String hex = Integer.toHexString(b & 0xFF);
		if (hex.length() == 1) {
			hex = '0' + hex;
		}
		return hex.toUpperCase(Locale.getDefault());
	}
}
  • 4.數組工具類
/**
 * 數組工具
 * 
 * @author yangle
 */
public class ArrayUtils {

	/**
	 * 合併數組
	 * 
	 * @param firstArray 第一個數組
	 * @param secondArray 第二個數組
	 * @return 合併後的數組
	 */
	public static byte[] concat(byte[] firstArray, byte[] secondArray) {
		if (firstArray == null || secondArray == null) {
			return null;
		}
		byte[] bytes = new byte[firstArray.length + secondArray.length];
		System.arraycopy(firstArray, 0, bytes, 0, firstArray.length);
		System.arraycopy(secondArray, 0, bytes, firstArray.length, secondArray.length);
		return bytes;
	}
}

效果

說明:效果圖中返回是指令解析對象

上面代碼運行後返回應該只有    

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章