通過JAVA與串口(RS232)通信實例
文章分類: Java編程 關鍵字: java com 串口 rs232
最近了解到的需求是需要需激光打刻機進行(RS232)串口通信,
這里使用的是RXTX開源包實現的。
之前并沒有用java做過串口通信,而且這方面資料不是很多。
項目實際應用中可能會采用VB開發(這個我就不會了)
只不過用java嘗試一下,記個筆記,希望可以對相關開發用些幫助。
下面是實現代碼
測試
這里使用的是RXTX開源包實現的。
之前并沒有用java做過串口通信,而且這方面資料不是很多。
項目實際應用中可能會采用VB開發(這個我就不會了)
只不過用java嘗試一下,記個筆記,希望可以對相關開發用些幫助。
下面是實現代碼
- 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;? //?從串口來的輸入流 ??
- ????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))?{? //?判斷如果端口已經啟動就連接 ??
- ????????????????????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 );? //?給當前串口天加一個監聽器 ??
- ????????}? catch ?(TooManyListenersException?e)?{ ??
- ????????} ??
- ????????serialPort.notifyOnDataAvailable( true );? //?當有數據時通知 ??
- ???????? try ?{ ??
- ????????????serialPort.setSerialPortParams( 2400 ,?SerialPort.DATABITS_8,? //?設置串口讀寫參數 ??
- ????????????????????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: //?當有可用數據時讀取數據,并且給串口返回數據 ??
- ???????????? 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; // 從串口來的輸入流 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)) { // 判斷如果端口已經啟動就連接 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); // 給當前串口天加一個監聽器 } catch (TooManyListenersException e) { } serialPort.notifyOnDataAvailable(true); // 當有數據時通知 try { serialPort.setSerialPortParams(2400, SerialPort.DATABITS_8, // 設置串口讀寫參數 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:// 當有可用數據時讀取數據,并且給串口返回數據 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 ?gnu.io.CommPortIdentifier; ??
- ??
- import ?java.util.Enumeration; ??
- ??
- public ? class ?Test?{ ??
- ??
- ???? public ? static ? void ?main(String[]?args)? throws ?InterruptedException?{ ??
- ????????Enumeration?portList?=?CommPortIdentifier.getPortIdentifiers();? //得到當前連接上的端口 ??
- ???????? ??
- ????????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(); //得到當前連接上的端口 CommUtil comm3 = new CommUtil(portList,"COM3"); int i = 0; while(i<5) { Thread.sleep(3000); comm3.send("hello"); i++; } comm3.ClosePort(); } }
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

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