虛擬串口工具的使用
最後更新:2018-12-03
來源:互聯網
上載者:User
1.(comm包的下載以及配置就不說了,這裡主要說工具的使用)下載VSPD(建立虛擬串口並監視通訊流量的工具,用它可以建立一對虛擬串口,例如com2-com3,那麼可以用com2發資料,com2將資料發給com3,我們可以從com3接收資料)2.下載com類比工具(LP-COMM,或Commix,或BoastSerialTool)這些工具不是必須的,他可以串連某個com口,你可以把資料填寫到輸入框,點擊發送按鈕,就把資料發送了。這樣比較方便,一般情況下,我們用程式發送資料,但為了調試,每次都啟動程式很麻煩,這樣就比較方便。但這些工具都不能接受資料!你想看看發的資料到底發送成功沒有,需要寫一個從comm口讀取資料的程式,先啟動它,當你用上述工具發送時,可以看到控制台輸出的資料(當然前提時你的程式裡有輸出語句)3.從VSPD也可以看出資料發送了,只是它只顯示流量,比如從com2發送了5kb的資料,com3接受了5kb的資料,根據這個也可以判斷資料的走向,只是具體發的什麼你不清楚。所以還是建議寫一個讀取資料的程式,並將讀取的內容在控制台列印出來,比較直觀。下面附上兩個程式,可供使用://////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////package com.mypro;import java.io.IOException;
import java.io.OutputStream;
import java.util.Enumeration;import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.UnsupportedCommOperationException;public class CommWrite {
static Enumeration portList;
static CommPortIdentifier portId;
static String messageString = "Hello, world!\n";
static SerialPort serialPort;
static OutputStream outputStream; public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers(); while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM2")) {
try {
serialPort = (SerialPort)
portId.open("SimpleWriteApp", 2000);
} catch (PortInUseException e) {}
try {
outputStream = serialPort.getOutputStream();
} catch (IOException e) {}
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {}
try {
for(int i=0;i<1000;i++){
messageString+="the time "+i+"--\n"+messageString+"\n";
outputStream.write(messageString.getBytes());
}
} catch (IOException e) {}
}
}
}
}}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////package com.mypro;import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.TooManyListenersException;import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
import javax.comm.UnsupportedCommOperationException;public class CommRead implements SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList; static InputStream inputStream;
static SerialPort serialPort;
Thread readThread;
public CommRead(){
init();
}
private void init() { portList = CommPortIdentifier.getPortIdentifiers(); while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM3")) {
try {
serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
} catch (PortInUseException e) {}
//加串口事件
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {}
//擷取輸入資料流
try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {}
serialPort.notifyOnDataAvailable(true);
//設定參數
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {}
}
}
}
} //串口事件
public void serialEvent(SerialPortEvent event) {
switch(event.getEventType()) {
case SerialPortEvent.BI:{System.out.println("SerialPortEvent.BI");}
case SerialPortEvent.OE:{System.out.println("SerialPortEvent.OE");}
case SerialPortEvent.FE:{System.out.println("SerialPortEvent.FE");}
case SerialPortEvent.PE:{System.out.println("SerialPortEvent.PE");}
case SerialPortEvent.CD:{System.out.println("SerialPortEvent.CD");}
case SerialPortEvent.CTS:{System.out.println("SerialPortEvent.CTS");}
case SerialPortEvent.DSR:{System.out.println("SerialPortEvent.DSR");}
case SerialPortEvent.RI:{System.out.println("SerialPortEvent.RI");}
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[200]; try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
// System.out.println("-----"+numBytes);
System.out.print(new String(readBuffer));
}
// System.out.print(new String(readBuffer));
} catch (IOException e) {}
break;
}
}
public static void main(String[] args) {
CommRead cc=new CommRead();
}
}