java 串口通信實(shí)現(xiàn)流程示例
1、下載64位rxtx for java 鏈接:http://fizzed.com/oss/rxtx-for-java
2、下載下來(lái)的包解壓后按照說(shuō)明放到JAVA_HOME即JAVA的安裝路徑下面去
3、在maven的pom.xml下添加
<dependency> <groupId>org.rxtx</groupId> <artifactId>rxtx</artifactId> <version>2.1.7</version> </dependency>
4、串口API
CommPort:端口的抽象類
CommPortIdentifier:對(duì)串口訪問(wèn)和控制的核心類
SerialPort:通過(guò)它可以直接對(duì)串口進(jìn)行讀、寫及設(shè)置工作
5、列出本機(jī)可用端口
Enumeration<CommPortIdentifier> em = CommPortIdentifier.getPortIdentifiers(); while (em.hasMoreElements()) { String name = em.nextElement().getName(); System.out.println(name); }
6、一般步驟:打開(kāi)串口得到串口對(duì)象==》設(shè)置參數(shù)==》對(duì)串口進(jìn)行讀寫==》關(guān)閉串口,其中對(duì)串口進(jìn)行讀操作比較常用
//打開(kāi)串口 CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier("COM4");//COM4是串口名字 CommPort commPort = portIdentifier.open("COM4", 2000); //2000是打開(kāi)超時(shí)時(shí)間 serialPort = (SerialPort) commPort; //設(shè)置參數(shù)(包括波特率,輸入/輸出流控制,數(shù)據(jù)位數(shù),停止位和齊偶校驗(yàn)) serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); //監(jiān)聽(tīng)串口事件 serialPort.addEventListener(new Abc()); //Abc是實(shí)現(xiàn)SerialPortEventListener接口的類,具體讀操作在里面進(jìn)行 // 設(shè)置當(dāng)有數(shù)據(jù)到達(dá)時(shí)喚醒監(jiān)聽(tīng)接收線程 serialPort.notifyOnDataAvailable(true); // 設(shè)置當(dāng)通信中斷時(shí)喚醒中斷線程 serialPort.notifyOnBreakInterrupt(true); // in.close(); //關(guān)閉串口
Abc類內(nèi)容,即讀串口的具體操作:
public class Abc implements SerialPortEventListener { public void serialEvent(SerialPortEvent arg0) { // TODO Auto-generated method stub //對(duì)以下內(nèi)容進(jìn)行判斷并操作 /* BI -通訊中斷 CD -載波檢測(cè) CTS -清除發(fā)送 DATA_AVAILABLE -有數(shù)據(jù)到達(dá) DSR -數(shù)據(jù)設(shè)備準(zhǔn)備好 FE -幀錯(cuò)誤 OE -溢位錯(cuò)誤 OUTPUT_BUFFER_EMPTY -輸出緩沖區(qū)已清空 PE -奇偶校驗(yàn)錯(cuò) RI - 振鈴指示 */ //switch多個(gè),if單個(gè) if (arg0.getEventType() == SerialPortEvent.DATA_AVAILABLE) { try { InputStream in = null; byte[] bytes = null; in = App.serialPort.getInputStream(); int bufflenth = in.available(); while (bufflenth != 0) { // 初始化byte數(shù)組為buffer中數(shù)據(jù)的長(zhǎng)度 bytes = new byte[bufflenth]; in.read(bytes); System.out.println(new String(bytes)); bufflenth = in.available(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
寫操作:
OutputStream out = serialPort.getOutputStream(); out.write(data); //byte[] data; out.flush();
總結(jié)
以上就是本文關(guān)于java 串口通信實(shí)現(xiàn)流程示例的全部?jī)?nèi)容,希望對(duì)大家有所幫助。如有問(wèn)題可以隨時(shí)留言,期待您的寶貴意見(jiàn)。
相關(guān)文章
springboot接入cachecloud redis示例實(shí)踐
這篇文章主要介紹了springboot接入cachecloud redis示例實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10spring cloud Hystrix斷路器的使用(熔斷器)
這篇文章主要介紹了spring cloud Hystrix斷路器的使用(熔斷器),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08Java實(shí)現(xiàn)寵物商店管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)寵物商店管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10