win7 64為下使用rxtx串列埠通訊
阿新 • • 發佈:2019-01-09
說明一下rxtx是javacomm串列埠通訊jar的一個開源的擴充套件,繼承了它,所以,我們在環境搭建好之後,只要把
import javax.comm.;替換成import gnu.io.;即可了。
環境的搭建;
找到你的java的執行環境:如找到win64下rxtxSerial.dll檔案
如我的java的安裝目錄下:
D:\Program Files\Java\jre7
複製 rxtxSerial.dll 到D:\Program Files\Java\jre7/bin/
複製 RXTXcomm.jar 到 D:\Program Files\Java\jre7/lib/ext/
在你的開發工具中引入lib包中的RXTXcomm.jar
com1串列埠通訊的程式碼;
public static final byte[] init = new byte[]{0x1B,0x40};
public static final byte[] clean = new byte[]{0x0C};
public static final byte[] pre_display = new byte[]{0x1B,0x51,0x41};
public static final byte[] post_display = new byte[]{0x0D};
public static void displayCustomerScreen (String data, byte[] mode){
try {
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier("COM1");
SerialPort serialPort = (SerialPort)portIdentifier.open("收銀客戶端", 5000);
serialPort.setSerialPortParams(2400, 8, 1, 0);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
try {
OutputStream outputStream = serialPort.getOutputStream();
if(mode!=null){
outputStream.write(mode);
}
if(data!=null){
outputStream.write(pre_display);
outputStream.write(data.getBytes());
outputStream.write(post_display);
}
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
finally {
serialPort.close();
}
} catch (NoSuchPortException e) {
e.printStackTrace();
} catch (PortInUseException e) {
e.printStackTrace();
} catch (UnsupportedCommOperationException e) {
e.printStackTrace();
}
}
到此win7 64位下Rxtx替換comm的串列埠通訊完成。