我要从COM1口读取数据,为什么程序中读到的数据与预期不符?(汗,代码有点乱)import javax.comm.*;
import java.util.*;
import java.io.*;
public class serial{
public static void main(String[] args){
Enumeration en=CommPortIdentifier.getPortIdentifiers();
CommPortIdentifier portId=null;
while(en.hasMoreElements()){
portId=(CommPortIdentifier)en.nextElement();
if(portId.getPortType()==CommPortIdentifier.PORT_SERIAL){
System.out.println(portId.getName());
}
}
CommPort com1;
try{
portId=CommPortIdentifier.getPortIdentifier("COM1");
try{
com1=portId.open("serial",60);
}catch(Exception e){
e.printStackTrace();
}
if(portId.isCurrentlyOwned()){
System.out.println("Name:"+portId.getName()+" port type:"+portId.getPortType()+" is current owened by "+portId.getCurrentOwner());
}
else
System.out.println("Name:"+portId.getName()+" port type:"+portId.getPortType()+" is not owened");
if(com1==null) System.exit(0);
InputStream in=com1.getInputStream();
DataInputStream din=new DataInputStream(in);
int read;
int xxx=0x7e;
boolean start=true;
while(true){
read=in.read();//在每个数据包的开头应该有一个0X7E作为同步信号

if(read==xxx){
if(start){
System.out.println("#####packet start#####");
start=false;
}
else{
System.out.println("#####packet end  #####");
start=true;
}

}else{
System.out.println(Integer.toHexString(read));
}
}
// com1.close();
}catch(Exception e){
// System.out.println(portId.getCurrentOwener());
e.printStackTrace();
}
}
}