java 串口通訊詳細及復雜實例。本站提示廣大學習愛好者:(java 串口通訊詳細及復雜實例)文章只能為提供參考,不一定能成為您想要的結果。以下是java 串口通訊詳細及復雜實例正文
java 完成串口通訊
最近做了一個與硬件相關的項目,剛開端聽說用java和硬件打交道,著實下了一大跳。java也可以操作硬件?
後來接觸到是用java經過串口通訊控制硬件覺得運用起來還不錯,也很方便。
特拿出來和大家一同分享一下。
預備任務:
首先到SUN官網下載一個zip包:javacomm20-win32.zip
其中重要的有這幾個文件:
win32com.dll
comm.jar
javax.comm.properties
依照闡明配置好環境,如下:
將win32com.dll復制到<JDK>\bin目錄下;將comm.jar復制到<JDK>\lib;把 javax.comm.properties也異樣拷貝到<JDK>\lib目錄下。但是在真正運轉運用串口包的時分,僅作這些是不夠的。因 為通常當運轉“java MyApp”的時分,是由JRE下的虛擬機啟動MyApp的。而我們只復制上述文件到JDK相應目錄下,所以使用順序將會提示找不到串口。處理這個問題的 辦法很復雜,我們只須將下面提到的文件放到JRE相應的目錄下就可以了
到這一個可以java 串口開發環境就搭建完成了
確認本機可以運用的串口:
package test;
import java.util.Enumeration;
import java.util.HashMap;
import javax.comm.CommPortIdentifier;
import javax.comm.SerialPort;
public class GetSerialPorts {
public void listPortChoices() {
CommPortIdentifier portId;
Enumeration en = CommPortIdentifier.getPortIdentifiers();
// iterate through the ports.
while (en.hasMoreElements()) {
portId = (CommPortIdentifier) en.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
System.out.println(portId.getName());
}
}
}
public static void main(String[] args) {
GetSerialPorts GSP = new GetSerialPorts();
GSP.listPortChoices();
}
}
翻開串口,封閉串口:
package test;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.HashMap;
import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.UnsupportedCommOperationException;
public class GetSerialPorts {
private CommPortIdentifier portId;
private SerialPort testPort;
private CommPortIdentifier myPort;
private InputStream is;
private OutputStream os;
public void listPortChoices() {
Enumeration en = CommPortIdentifier.getPortIdentifiers();
// iterate through the ports.
while (en.hasMoreElements()) {
portId = (CommPortIdentifier) en.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
System.out.println(portId.getName());
}
myPort = portId;// 恣意取一個串口,比方com1
}
}
public boolean openPort() {
try {
testPort = (SerialPort) myPort.open("COM1", 500);// 留意這裡必需換成一個真實的串口
try {
this.testPort.setSerialPortParams(38400, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_EVEN);
} catch (UnsupportedCommOperationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
this.testPort.enableReceiveTimeout(30);
} catch (UnsupportedCommOperationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.testPort.setOutputBufferSize(1024);
this.testPort.setInputBufferSize(1024);
try {
this.is = this.testPort.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
this.os = this.testPort.getOutputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.testPort.notifyOnDataAvailable(true);
this.testPort.notifyOnOutputEmpty(true);
this.testPort.notifyOnBreakInterrupt(true);
// this.printerPort.addEventListener(new PrintPortListener(is));
System.out.println("翻開com1機串口成功");
return true;
} catch (PortInUseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
/**
* TODO 封閉端口
*
* @param
* @return Map
* @throws
*/
public boolean closePort() {
// TODO Auto-generated method stub
try {
if (null != this.testPort) {
is.close();
os.close();
this.testPort.close();
}
System.out.println("封閉COM1串口成功");
return true;
} catch (Exception e) {
// TODO Auto-generated catch block
// e.printStackTrace();
System.out.println("封閉COM1串口失敗");
return false;
}
}
public static void main(String[] args) {
GetSerialPorts GSP = new GetSerialPorts();
GSP.listPortChoices();
GSP.openPort();
}
}
讀數據:
/**
* TODO 接納端口數據
*
* @param InputStream
* @return String
* @throws
*/
public String readData(InputStream is) {
// 讀取緩沖區域
byte[] readBuffer = new byte[4096];
int readDataLength = 0;
try {
readDataLength = is.read(readBuffer);
// for (byte b : readBuffer) {
// System.out.print(b);
// }
// System.out.println();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
// 將真實數據保管到零時數組中
byte[] readTemp = new byte[readDataLength];
for (int i = 0; i < readDataLength; i++) {
readTemp[i] = readBuffer[i];
}
// 將byte數組轉換為16進制字符串
String stringTemp = FeelTheBase.bytesToHexString(readTemp);
// System.out.println("指令前往值" + stringTemp);
return stringTemp;
}
感激閱讀,希望能協助到大家,謝謝大家對本站的支持!