四、實例分析
同API一起下載的還有一個examples文件,里面有個最簡單的讀、寫程序,對其進行注釋,以增進了理。
(1)讀串口
1 | import java.io.*; |
2 | import java.util.*; |
3 | import javax.comm.*; |
4 | |
5 | public class SimpleRead implements Runnable, SerialPortEventListener { |
6 | |
7 | static CommPortIdentifier portId; |
8 | static Enumeration portList;//枚舉類 |
9 | |
10 | InputStream inputStream; |
11 | SerialPort serialPort; |
12 | Thread readThread; |
13 | |
14 | public static void main(String[] args) { |
15 | |
16 | portList = CommPortIdentifier.getPortIdentifiers();/*不帶參數的getPortIdentifiers方法獲得一個枚舉對象,該對象又包含了系統中管理每個端口的CommPortIdentifier對象。注意這里的端口不僅僅是指串口,也包括並口。這個方法還可以帶參數。getPortIdentifiers(CommPort)獲得與已經被應用程序打開的端口相對應的CommPortIdentifier對象。 getPortIdentifier(String portName)獲取指定端口名(比如“COM1”)的CommPortIdentifier對象。*/ |
17 | |
18 | while (portList.hasMoreElements()) { |
19 | portId = (CommPortIdentifier) portList.nextElement(); |
20 | if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)/*getPortType方法返回端口類型*/ { |
21 | // if (portId.getName().equals("COM1"))/* 找Windows下的第一個串口*/ { |
22 | if (portId.getName().equals("/dev/term/a"))/*找Unix-like系統下的第一個串口*/ { |
23 | SimpleRead reader = new SimpleRead(); |
24 | } |
25 | } |
26 | } |
27 | } |
28 | |
29 | public SimpleRead() { |
30 | try { |
31 | serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);/* open方法打開通訊端口,獲得一個CommPort對象。它使程序獨占端口。如果端口正被其他應用程序占用,將使用 CommPortOwnershipListener事件機制,傳遞一個PORT_OWNERSHIP_REQUESTED事件。每個端口都關聯一個 InputStream 何一個OutputStream。如果端口是用open方法打開的,那么任何的getInputStream都將返回相同的數據流對象,除非有close 被調用。有兩個參數,第一個為應用程序名;第二個參數是在端口打開時阻塞等待的毫秒數。*/ |
32 | } catch (PortInUseException e) {} |
33 | try { |
34 | inputStream = serialPort.getInputStream();/*獲取端口的輸入流對象*/ |
35 | } catch (IOException e) {} |
36 | try { |
37 | serialPort.addEventListener(this);/*注冊一個SerialPortEventListener事件來監聽串口事件*/ |
38 | } catch (TooManyListenersException e) {} |
39 | |
40 | serialPort.notifyOnDataAvailable(true);/*數據可用*/ |
41 | |
42 | try { |
43 | serialPort.setSerialPortParams(9600, |
44 | SerialPort.DATABITS_8, |
45 | SerialPort.STOPBITS_1, |
46 | SerialPort.PARITY_NONE);/*設置串口初始化參數,依次是波特率,數據位,停止位和校驗*/ |
47 | } catch (UnsupportedCommOperationException e) {} |
48 | |
49 | readThread = new Thread(this); |
50 | readThread.start(); |
51 | } |
52 | |
53 | public void run() { |
54 | try { |
55 | Thread.sleep(20000); |
56 | } catch (InterruptedException e) {} |
57 | } |
58 | |
59 | //串口事件 |
60 | public void serialEvent(SerialPortEvent event) { |
61 | |
62 | switch(event.getEventType()) { |
63 | case SerialPortEvent.BI:/*Break interrupt,通訊中斷*/ |
64 | case SerialPortEvent.OE:/*Overrun error,溢位錯誤*/ |
65 | case SerialPortEvent.FE:/*Framing error,傳幀錯誤*/ |
66 | case SerialPortEvent.PE:/*Parity error,校驗錯誤*/ |
67 | case SerialPortEvent.CD:/*Carrier detect,載波檢測*/ |
68 | case SerialPortEvent.CTS:/*Clear to send,清除發送*/ |
69 | case SerialPortEvent.DSR:/*Data set ready,數據設備就緒*/ |
70 | case SerialPortEvent.RI:/*Ring indicator,響鈴指示*/ |
71 | case SerialPortEvent.OUTPUT_BUFFER_EMPTY:/*Output buffer is empty,輸出緩沖區清空*/ |
72 | break; |
73 | |
74 | case SerialPortEvent.DATA_AVAILABLE:/*Data available at the serial port,端口有可用數據。讀到緩沖數組,輸出到終端*/ |
75 | byte[] readBuffer = new byte[20]; |
76 | |
77 | try { |
78 | while (inputStream.available() > 0) { |
79 | int numBytes = inputStream.read(readBuffer); |
80 | } |
81 | System.out.print(new String(readBuffer)); |
82 | } catch (IOException e) {} |
83 | break; |
84 | } |
85 | } |
86 | } |
87 |
(2)寫串口
1 | import java.io.*; |
2 | import java.util.*; |
3 | import javax.comm.*; |
4 | |
5 | public class SimpleWrite { |
6 | static Enumeration portList; |
7 | static CommPortIdentifier portId; |
8 | static String messageString = "Hello, world!/n"; |
9 | static SerialPort serialPort; |
10 | static OutputStream outputStream; |
11 | |
12 | public static void main(String[] args) { |
13 | portList = CommPortIdentifier.getPortIdentifiers(); |
14 | |
15 | while (portList.hasMoreElements()) { |
16 | portId = (CommPortIdentifier) portList.nextElement(); |
17 | if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { |
18 | // if (portId.getName().equals("COM1")) { |
19 | if (portId.getName().equals("/dev/term/a")) { |
20 | try { |
21 | serialPort = (SerialPort) |
22 | portId.open("SimpleWriteApp", 2000); |
23 | } catch (PortInUseException e) {} |
24 | try { |
25 | outputStream = serialPort.getOutputStream(); |
26 | } catch (IOException e) {} |
27 | try { |
28 | serialPort.setSerialPortParams(9600, |
29 | SerialPort.DATABITS_8, |
30 | SerialPort.STOPBITS_1, |
31 | SerialPort.PARITY_NONE); |
32 | } catch (UnsupportedCommOperationException e) {} |
33 | try { |
34 | outputStream.write(messageString.getBytes()); |
35 | } catch (IOException e) {} |
36 | } |
37 | } |
38 | } |
39 | } |
40 | } |
41 |