java串列埠通訊
阿新 • • 發佈:2019-01-09
Rxtx開源包下載地址:http://mfizz.com/oss/rxtx-for-java
windows平臺使用方法:
1、把rxtxParallel.dll、rxtxSerial.dll拷貝到:C:\WINDOWS\system32下。
2、如果是在開發的時候(JDK),需要把RXTXcomm.jar拷貝到..\jre…\lib\ext下;將rxtxParallel.dll、rxtxSerial.dll 拷貝到..\jre…\bin下;如:D:\Program Files\java\jre1.8.0_111\lib\ext
3、java,需要匯入RXTXcomm.jar包
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.TooManyListenersException;
import gnu.io.CommPortIdentifier;
import gnu.io.NoSuchPortException;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import gnu.io.UnsupportedCommOperationException;
public class SerialUtil implements SerialPortEventListener{
public int timeout = 6; // 超時時間
public String port = "COM1"; // 埠名稱
public int dataBits = 8; // 資料位
public int stopBits = 1; // 停止位
public int parity = 0; // 奇偶校驗
public int rate = 38400; // 波特率
private SerialPort serialPort;
boolean isOpen = false; // 埠狀態:開啟或關閉
// static byte[] openJoin12 = new byte[] { 0xFE, 0x03, 0x21, 0x30, 0xFF, 0xFC,
// 0x12, 0x03 }; // 開啟 12S 允許加網指令
// static byte[] makeNet = new byte[] { 0xFE, 0x00, 0x21, 0x31, 0x10}; // 終端串列埠傳送組網指令
public SerialUtil() {
listPortChoices();
//getPortStatus();
open();
sendData("hi");
sendData("");
//getPortStatus();
//close();
//getPortStatus();
}
public void getPortStatus() {
if(isOpen) {
System.out.println("埠已開啟");
} else {
System.out.println("埠已關閉");
}
}
// 讀取所有串列埠名字
public void listPortChoices() {
CommPortIdentifier portId;
Enumeration en = CommPortIdentifier.getPortIdentifiers();
while (en.hasMoreElements()) {
portId = (CommPortIdentifier) en.nextElement();
System.out.println("port: "+ portId.getName() + " : " + portId.getPortType());
// if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { // 只得到com串列埠
// System.out.println(portId.getName());
// }
}
}
// 開啟串列埠
public void open() {
try {
CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(port);
serialPort = (SerialPort) portId.open("controller", timeout);
//inputStream = serialPort.getInputStream();
serialPort.addEventListener(this);// 增加一個監聽
serialPort.notifyOnDataAvailable(true);
serialPort.setSerialPortParams(rate, dataBits, stopBits, parity);
isOpen = true;
} catch (NoSuchPortException e) { // 埠不存在
e.printStackTrace();
} catch (PortInUseException e) { // 埠已經被佔用
e.printStackTrace();
} catch (UnsupportedCommOperationException e) { // 埠操作命令不支援
e.printStackTrace();
} catch (TooManyListenersException e) { // 太多的監聽者
e.printStackTrace();
}
}
// 關閉串列埠
public void close() {
if (isOpen) {
try {
serialPort.notifyOnDataAvailable(false);
serialPort.removeEventListener();
serialPort.close();
isOpen = false;
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 向埠傳送資料
public void sendData(String data) {
if(isOpen) {
try {
OutputStream outputStream = serialPort.getOutputStream();
outputStream.write(data.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void serialEvent(SerialPortEvent event) {
switch (event.getEventType()) {
case SerialPortEvent.BI: // 10
case SerialPortEvent.OE: // 7
case SerialPortEvent.FE: // 9
case SerialPortEvent.PE: // 8
case SerialPortEvent.CD: // 6
case SerialPortEvent.CTS: // 3
case SerialPortEvent.DSR: // 4
case SerialPortEvent.RI: // 5
case SerialPortEvent.OUTPUT_BUFFER_EMPTY: // 2
break;
case SerialPortEvent.DATA_AVAILABLE: // 1
try {
// 多次讀取,將所有資料讀入
byte[] readBuffer = new byte[1000];
InputStream inputStream = serialPort.getInputStream();
while (inputStream.available() > 0) {
inputStream.read(readBuffer);
}
System.out.println(new String(readBuffer));
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
public static void main(String[] args) {
SerialUtil su = new SerialUtil();
}
}