JAVA 串口编程(二)
生活随笔
收集整理的這篇文章主要介紹了
JAVA 串口编程(二)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
三、實例
(1)打開、關閉串口
首先使用CommPortIdentifier中的方法,獲取可用的端口,并且選擇一個端口打開作為通信端口。
A:枚舉可用端口
?
| 1? | void?listPortChoices()? |
| 2? | { |
| 3? | ?????CommPortIdentifier?portId; |
| 4? | ?????Enumeration?en?=?CommPortIdentifier.getPortIdentifiers(); |
| 5? | ??????while?(en.hasMoreElements())? |
| 6? | ???????{ |
| 7? | ?????????portId?=?(CommPortIdentifier)?en.nextElement(); |
| 8? | ?????????if?(portId.getPortType()?==?CommPortIdentifier.PORT_SERIAL)? |
| 9? | ?????????System.out.println(portId.getName()); |
| 10? | ???????} |
| 11? | ???????portChoice.select(parameters.getPortName()); |
| 12? | ?} |
| 13? | ? |
| 14? | ? |
?
B:打開指定的端口
| 1? | CommPortIdentifier?portId; |
| 2? | try? |
| 3? | { |
| 4? | ??//生成CommPortIdentifier類的實例 |
| 5? | ??portId?=?CommPortIdentifier.getPortIdentifier("COM4"); |
| 6? | }? |
| 7? | catch?(NoSuchPortException?e) |
| 8? | { |
| 9? | ???e.printStackTrace(); |
| 10? | } |
| 11? | try? |
| 12? | { |
| 13? | ????//打開串口COM4,設置超時時限為3000毫秒 |
| 14? | ??? serialPort?=?(SerialPort)?portId.open("testApp",?3000); |
| 15? | }? |
| 16? | catch?(PortInUseException?e) |
| 17? | { |
| 18? | ??? e.printStackTrace(); |
| 19? | } |
| 20? | ? |
| 21? | ? |
?
C:關閉端口
使用完的端口,必須記得將其關閉,否則其它的程序將無法使用該端口,CommPortIdentifier類只提供了開啟端口的方法,而要關閉端口,則要調用CommPort類的close()方法。
serialPort.close()
(2)設置串口參數
| 1? | try?{ |
| 2? | ??? try?{ |
| 3? | ??? ??? serialPort.setSerialPortParams(9600,?//? 波特率 |
| 4? | ??? ??? ???????????????????????????????SerialPort.DATABITS_8,//? 數據位數 |
| 5? | ??? ???????????????????????????????????SerialPort.STOPBITS_1,?//? 停止位 |
| 6? | ??? ???????????????????????????????????SerialPort.PARITY_NONE);//? 奇偶位 |
| 7? | ??? }? |
| 8? | ??? catch?(UnsupportedCommOperationException?e) |
| 9? | ??? ?{ |
| 10? | ??? ??? e.printStackTrace(); |
| 11? | ??? } |
| 12? | ? |
(3)串口的讀、寫
A:向串口寫數據
| 1? | ??? OutputStream?outputStream; |
| 2? | ??? try |
| 3? | ??? { |
| 4? | ??? ??? outputStream?=?serialPort.getOutputStream(); |
| 5? | ??? }? |
| 6? | ??? catch?(IOException?e) |
| 7? | ??? ?{ |
| 8? | ??? ??? e.printStackTrace(); |
| 9? | ??? } |
| 10? | ??? bt?=?new?byte[]?{?(byte)?0x55,?(byte)?0xAA,?(byte)?0xF1?}; |
| 11? | ??? try |
| 12? | ??? { |
| 13? | ??? ??? outputStream.write(bt); |
| 14? | ??? ??? outputStream.flush(); |
| 15? | ??? ??? outputStream.close(); |
| 16? | ??? } |
| 17? | ??? catch?(IOException?e) |
| 18? | ??? ?{ |
| 19? | ??? ??? e.printStackTrace(); |
| 20? | ??? } |
| 21? | ? |
B:讀取串口中的數據
讀操作,需繼承SerialPortEventListener接口。為SerialPort添加監聽Listener。實現該接口的serialEvent (SerialPortEvent event)方法。
| 1? | ??? ??? public?class?SerialRead?implements?SerialPortEventListener{ |
| 2? | ??? ??? |
| 3? | ??? ??? ??? InputStream?inputStream; |
| 4? | ??? ??? ??? byte[]?rBuffer?=?new?byte[38]; |
| 5? | ??? ??? ??? ?SerialRead(SerialPort?serialPort) |
| 6? | ??? ??? ??? ?{ |
| 7? | ??? ??? ??? ??? try?{ |
| 8? | ??? ??? ??? ??? ??? serialPort.addEventListener((SerialPortEventListener)?this); |
| 9? | ??? ??? ??? ??? ??? this.serialPort.notifyOnDataAvailable(true); |
| 10? | ??? ??? |
| 11? | ??? ??? ??? ??? }?catch?(TooManyListenersException?e)?{ |
| 12? | ??? ??? ??? ??? } |
| 13? | ??? ??? ??? |
| 14? | ??? ??? ??? ??? try?{ |
| 15? | ??? ??? ??? ??? ??? if?(serialPort?!=?null) |
| 16? | ??? ??? ??? ??? ??? ??? inputStream?=?serialPort.getInputStream(); |
| 17? | ??? ??? ??? ??? }?catch?(IOException?e)?{ |
| 18? | ??? ??? ??? ??? } |
| 19? | ??? ??? ??? ????} |
| 20? | ??? ??? ??? ? |
| 21? | ??? ??? ??? public?void?serialEvent(SerialPortEvent?event)? |
| 22? | ??? ??? ??? { |
| 23? | ??? ??? ??? ??? if?(event.getEventType()?==?SerialPortEvent.DATA_AVAILABLE) |
| 24? | ??? ??? ??? ??? {? |
| 25? | ??? ??? ??? ??? ??? int?count?=?0; |
| 26? | ??? ??? ??? ??? ??? try? |
| 27? | ??? ??? ??? ??? ??? { |
| 28? | ??? ??? ??? ??? ??? ??? while?((ch?=?inputStream.read())?!=?-1)? |
| 29? | ??? ??? ??? ??? ??? ??? {??? ??? |
| 30? | ??? ??? ??? ??? ??? ??? ??? bt[count++]=(byte)ch; |
| 31? | ??? ??? ??? ??? ??? ??? } |
| 32? | ??? ??? ??? ??? ??? } |
| 33? | ??? ??? ??? ??? ??? catch?(IOException?e)? |
| 34? | ??? ??? ??? ??? ??? { |
| 35? | ??? ??? ??? ??? ??? ??? e.printStackTrace(); |
| 36? | ??? ??? ??? ??? ??? } |
| 37? | ??? ??? ??? ??? }??? |
| 38? | ??? ??? ??? } |
| 39? | ? |
總結
以上是生活随笔為你收集整理的JAVA 串口编程(二)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 利用JNative实现Java调用动态库
- 下一篇: JAVA 串口编程 (一)