通過(guò)JAVA與串口(RS232)通信實(shí)例
文章分類: Java編程 關(guān)鍵字: java com 串口 rs232
最近了解到的需求是需要需激光打刻機(jī)進(jìn)行(RS232)串口通信,
這里使用的是RXTX開(kāi)源包實(shí)現(xiàn)的。
之前并沒(méi)有用java做過(guò)串口通信,而且這方面資料不是很多。
項(xiàng)目實(shí)際應(yīng)用中可能會(huì)采用VB開(kāi)發(fā)(這個(gè)我就不會(huì)了)
只不過(guò)用java嘗試一下,記個(gè)筆記,希望可以對(duì)相關(guān)開(kāi)發(fā)用些幫助。
下面是實(shí)現(xiàn)代碼
測(cè)試
這里使用的是RXTX開(kāi)源包實(shí)現(xiàn)的。
之前并沒(méi)有用java做過(guò)串口通信,而且這方面資料不是很多。
項(xiàng)目實(shí)際應(yīng)用中可能會(huì)采用VB開(kāi)發(fā)(這個(gè)我就不會(huì)了)
只不過(guò)用java嘗試一下,記個(gè)筆記,希望可以對(duì)相關(guān)開(kāi)發(fā)用些幫助。
下面是實(shí)現(xiàn)代碼
- package ?test; ??
- ??
- import ?java.io.IOException; ??
- import ?java.io.InputStream; ??
- import ?java.io.InputStreamReader; ??
- import ?java.io.OutputStream; ??
- import ?java.util.Date; ??
- import ?java.util.Enumeration; ??
- import ?java.util.TooManyListenersException; ??
- ??
- import ?gnu.io.CommPortIdentifier; ??
- import ?gnu.io.PortInUseException; ??
- import ?gnu.io.SerialPort; ??
- import ?gnu.io.SerialPortEvent; ??
- import ?gnu.io.SerialPortEventListener; ??
- import ?gnu.io.UnsupportedCommOperationException; ??
- ??
- public ? class ?CommUtil? implements ?SerialPortEventListener?{ ??
- ??
- ????InputStream?inputStream;? //?從串口來(lái)的輸入流 ??
- ????OutputStream?outputStream; //?向串口輸出的流 ??
- ????SerialPort?serialPort;? //?串口的引用 ??
- ????CommPortIdentifier?portId; ??
- ??
- ???? public ?CommUtil(Enumeration?portList,?String?name)?{ ??
- ???????? while ?(portList.hasMoreElements())?{ ??
- ????????????CommPortIdentifier?temp?=?(CommPortIdentifier)?portList.nextElement(); ??
- ???????????? if ?(temp.getPortType()?==?CommPortIdentifier.PORT_SERIAL)?{ //?判斷如果端口類型是串口 ??
- ???????????????? if ?(temp.getName().equals(name))?{? //?判斷如果端口已經(jīng)啟動(dòng)就連接 ??
- ????????????????????portId?=?temp; ??
- ????????????????} ??
- ????????????} ??
- ????????} ??
- ???????? try ?{ ??
- ????????????serialPort?=?(SerialPort)?portId.open( "My" +name,? 2000 ); ??
- ????????}? catch ?(PortInUseException?e)?{ ??
- ??
- ????????} ??
- ???????? try ?{ ??
- ????????????inputStream?=?serialPort.getInputStream(); ??
- ????????????outputStream?=?serialPort.getOutputStream(); ??
- ????????}? catch ?(IOException?e)?{ ??
- ????????} ??
- ???????? try ?{ ??
- ????????????serialPort.addEventListener( this );? //?給當(dāng)前串口天加一個(gè)監(jiān)聽(tīng)器 ??
- ????????}? catch ?(TooManyListenersException?e)?{ ??
- ????????} ??
- ????????serialPort.notifyOnDataAvailable( true );? //?當(dāng)有數(shù)據(jù)時(shí)通知 ??
- ???????? try ?{ ??
- ????????????serialPort.setSerialPortParams( 2400 ,?SerialPort.DATABITS_8,? //?設(shè)置串口讀寫(xiě)參數(shù) ??
- ????????????????????SerialPort.STOPBITS_1,?SerialPort.PARITY_NONE); ??
- ????????}? catch ?(UnsupportedCommOperationException?e)?{ ??
- ????????} ??
- ????} ??
- ??
- ???? public ? void ?serialEvent(SerialPortEvent?event)?{ ??
- ???????? switch ?(event.getEventType())?{ ??
- ???????? case ?SerialPortEvent.BI: ??
- ???????? case ?SerialPortEvent.OE: ??
- ???????? case ?SerialPortEvent.FE: ??
- ???????? case ?SerialPortEvent.PE: ??
- ???????? case ?SerialPortEvent.CD: ??
- ???????? case ?SerialPortEvent.CTS: ??
- ???????? case ?SerialPortEvent.DSR: ??
- ???????? case ?SerialPortEvent.RI: ??
- ???????? case ?SerialPortEvent.OUTPUT_BUFFER_EMPTY: ??
- ???????????? break ; ??
- ???????? ??
- ???????? case ?SerialPortEvent.DATA_AVAILABLE: //?當(dāng)有可用數(shù)據(jù)時(shí)讀取數(shù)據(jù),并且給串口返回?cái)?shù)據(jù) ??
- ???????????? byte []?readBuffer?=? new ? byte [ 20 ]; ??
- ??
- ???????????? try ?{ ??
- ???????????????? while ?(inputStream.available()?>? 0 )?{ ??
- ????????????????????System.out.println(inputStream.available()); ??
- ???????????????????? int ?numBytes?=?inputStream.read(readBuffer); ??
- ????????????????????System.out.println(numBytes); ??
- ????????????????} ??
- ????????????????System.out.println( new ?String(readBuffer).trim()); ??
- ????????????}? catch ?(IOException?e)?{ ??
- ????????????????e.printStackTrace(); ??
- ????????????} ??
- ???????????? break ; ??
- ????????} ??
- ????} ??
- ???? public ? void ?send(String?content){ ??
- ???????? try ?{ ??
- ????????????outputStream.write(content.getBytes()); ??
- ????????}? catch ?(IOException?e)?{ ??
- ????????????e.printStackTrace(); ??
- ????????} ??
- ????} ??
- ???? ??
- ???? public ? void ?ClosePort()?{ ??
- ???????? if ?(serialPort?!=? null )?{ ??
- ??????????serialPort.close(); ??
- ????????} ??
- ??????} ??
- ??
- ???? ??
- }??
package test; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.util.Date; import java.util.Enumeration; import java.util.TooManyListenersException; import gnu.io.CommPortIdentifier; import gnu.io.PortInUseException; import gnu.io.SerialPort; import gnu.io.SerialPortEvent; import gnu.io.SerialPortEventListener; import gnu.io.UnsupportedCommOperationException; public class CommUtil implements SerialPortEventListener { InputStream inputStream; // 從串口來(lái)的輸入流 OutputStream outputStream;// 向串口輸出的流 SerialPort serialPort; // 串口的引用 CommPortIdentifier portId; public CommUtil(Enumeration portList, String name) { while (portList.hasMoreElements()) { CommPortIdentifier temp = (CommPortIdentifier) portList.nextElement(); if (temp.getPortType() == CommPortIdentifier.PORT_SERIAL) {// 判斷如果端口類型是串口 if (temp.getName().equals(name)) { // 判斷如果端口已經(jīng)啟動(dòng)就連接 portId = temp; } } } try { serialPort = (SerialPort) portId.open("My"+name, 2000); } catch (PortInUseException e) { } try { inputStream = serialPort.getInputStream(); outputStream = serialPort.getOutputStream(); } catch (IOException e) { } try { serialPort.addEventListener(this); // 給當(dāng)前串口天加一個(gè)監(jiān)聽(tīng)器 } catch (TooManyListenersException e) { } serialPort.notifyOnDataAvailable(true); // 當(dāng)有數(shù)據(jù)時(shí)通知 try { serialPort.setSerialPortParams(2400, SerialPort.DATABITS_8, // 設(shè)置串口讀寫(xiě)參數(shù) SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); } catch (UnsupportedCommOperationException e) { } } public void serialEvent(SerialPortEvent event) { switch (event.getEventType()) { case SerialPortEvent.BI: case SerialPortEvent.OE: case SerialPortEvent.FE: case SerialPortEvent.PE: case SerialPortEvent.CD: case SerialPortEvent.CTS: case SerialPortEvent.DSR: case SerialPortEvent.RI: case SerialPortEvent.OUTPUT_BUFFER_EMPTY: break; case SerialPortEvent.DATA_AVAILABLE:// 當(dāng)有可用數(shù)據(jù)時(shí)讀取數(shù)據(jù),并且給串口返回?cái)?shù)據(jù) byte[] readBuffer = new byte[20]; try { while (inputStream.available() > 0) { System.out.println(inputStream.available()); int numBytes = inputStream.read(readBuffer); System.out.println(numBytes); } System.out.println(new String(readBuffer).trim()); } catch (IOException e) { e.printStackTrace(); } break; } } public void send(String content){ try { outputStream.write(content.getBytes()); } catch (IOException e) { e.printStackTrace(); } } public void ClosePort() { if (serialPort != null) { serialPort.close(); } } }
測(cè)試
- package ?test; ??
- ??
- import ?gnu.io.CommPortIdentifier; ??
- ??
- import ?java.util.Enumeration; ??
- ??
- public ? class ?Test?{ ??
- ??
- ???? public ? static ? void ?main(String[]?args)? throws ?InterruptedException?{ ??
- ????????Enumeration?portList?=?CommPortIdentifier.getPortIdentifiers();? //得到當(dāng)前連接上的端口 ??
- ???????? ??
- ????????CommUtil?comm3?=? new ?CommUtil(portList, "COM3" ); ??
- ???????? int ?i?=? 0 ; ??
- ???????? while (i< 5 ) ??
- ????????{ ??
- ????????????Thread.sleep( 3000 ); ??
- ????????????comm3.send( "hello" ); ??
- ????????????i++; ??
- ????????} ??
- ????????comm3.ClosePort(); ??
- ????} ??
- ??
- }??
package test; import gnu.io.CommPortIdentifier; import java.util.Enumeration; public class Test { public static void main(String[] args) throws InterruptedException { Enumeration portList = CommPortIdentifier.getPortIdentifiers(); //得到當(dāng)前連接上的端口 CommUtil comm3 = new CommUtil(portList,"COM3"); int i = 0; while(i<5) { Thread.sleep(3000); comm3.send("hello"); i++; } comm3.ClosePort(); } }
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
