我搞顶了。这个是反编译的CommPortIdentifier类(部分)
但用JAVA读串口还是有点问题读,读的数据会丢失,而且对长度不同的数据很难处理,因为SerialPortEventListener这个接口只有一个方法,而且没有返回值,现在我用VC写了一个串口通讯的程序,然后用JAVA调用,全部解决,如果想要,就留EMAIL
public class CommPortIdentifier
{
public static Enumeration getPortIdentifiers(){
…… } public static CommPortIdentifier getPortIdentifier(String s)
throws NoSuchPortException
{
SecurityManager securitymanager = System.getSecurityManager();
if(securitymanager != null)
securitymanager.checkDelete(propfilename);
CommPortIdentifier commportidentifier = null;
synchronized(lock)
{
for(commportidentifier = masterIdList; commportidentifier != null; commportidentifier = commportidentifier.next)
if(commportidentifier.name.equals(s))
break; }
if(commportidentifier != null)
return commportidentifier;
else
throw new NoSuchPortException();
} public static CommPortIdentifier getPortIdentifier(CommPort commport)
throws NoSuchPortException
{
SecurityManager securitymanager = System.getSecurityManager();
if(securitymanager != null)
securitymanager.checkDelete(propfilename);
CommPortIdentifier commportidentifier = null;
synchronized(lock)
{
for(commportidentifier = masterIdList; commportidentifier != null; commportidentifier = commportidentifier.next)
if(commportidentifier.port == commport)
break; }
if(commportidentifier != null)
return commportidentifier;
else
throw new NoSuchPortException();
} private static void addPort(CommPort commport, int i)
{……} public static void addPortName(String s, int i, CommDriver commdriver)
{……} public String getName()
{return name;} public int getPortType()
{return portType;} private native int nCreateMutex(String s);
private native int nCreateEvent(String s);
private native boolean nClaimMutex(int i, int j);
private native boolean nReleaseMutex(int i);
private native boolean nPulseEvent(int i);
private native boolean nSetEvent(int i);
private native boolean nCloseHandle(int i);
private void createNativeObjects()
{……} public synchronized CommPort open(String s, int i)
throws PortInUseException
{
if(!nativeObjectsCreated)
createNativeObjects();
if(owned)
{
maskOwnershipEvents = true;
fireOwnershipEvent(3);
maskOwnershipEvents = false;
if(owned)
throw new PortInUseException(owner);
}
port = driver.getCommPort(name, portType);
if(port == null)
{
nSetEvent(ownershipRequestedEvent);
for(int j = i <= 200 ? 200 : i; j > 0; j -= 200)
{
try
{
wait(200L);
}
catch(InterruptedException _ex) { }
port = driver.getCommPort(name, portType);
if(port != null)
break;
} if(port == null)
{
String s1 = nGetOwner(shname);
if(s1 == null || s1.length() == 0)
s1 = "Unknown Windows Application";
throw new PortInUseException(s1);
}
}
owned = true;
owner = s;
if(s == null || s.length() == 0)
shmem = nSetOwner(shname, "Unspecified Java Application", true);
else
shmem = nSetOwner(shname, s, true);
if(!nSetEvent(ownedEvent))
System.err.println("Error pulsing ownedEvent");
return port;
} private native int nSetOwner(String s, String s1, boolean flag); private native String nGetOwner(String s); private native void nUnsetOwner(String s); public String getCurrentOwner()
{…} public boolean isCurrentlyOwned()
{……} public void addPortOwnershipListener(CommPortOwnershipListener commportownershiplistener)
{……} public void removePortOwnershipListener(CommPortOwnershipListener commportownershiplistener)
{……} private native int nWaitForEvents(int i, int j, int k); void ownershipThreadWaiter()
{……} synchronized void internalClosePort()
{……} CommPortIdentifier(String s, CommPort commport, int i, CommDriver commdriver)
{
cpoList = new CpoList();
name = s;
port = commport;
portType = i;
next = null;
driver = commdriver;
shname = "javax.comm-" + s;
shmem = nSetOwner(shname, "", false);
} void fireOwnershipEvent(int i)
{……} private static String[] parsePropsFile(InputStream inputstream)
{……} private static void loadDriver(String s)
throws IOException
{
File file = new File(s);
BufferedInputStream bufferedinputstream = new BufferedInputStream(new FileInputStream(file));
String as[] = parsePropsFile(bufferedinputstream);
if(as != null)
{
for(int i = 0; i < as.length; i++)
if(as[i].regionMatches(true, 0, "driver=", 0, 7))
{
String s1 = as[i].substring(7);
s1.trim();
try
{
CommDriver commdriver = (CommDriver)Class.forName(s1).newInstance();
commdriver.initialize();
}
catch(Throwable throwable)
{
System.err.println("Caught " + throwable + " while loading driver " + s1);
}
}
}
} private static String findPropFile()
{……} public CommPort open(FileDescriptor filedescriptor)
throws UnsupportedCommOperationException
{
throw new UnsupportedCommOperationException();
} String name;
private int portType;
public static final int PORT_SERIAL = 1;
public static final int PORT_PARALLEL = 2;
private boolean nativeObjectsCreated;
private int ownedEvent;
private int unownedEvent;
private int ownershipRequestedEvent;
private int shmem;
private String shname;
private boolean maskOwnershipEvents;
OwnershipEventThread oeThread;
CpoList cpoList;
CommPortIdentifier next;
private CommPort port;
private CommDriver driver;
static Object lock = new Object();
static String propfilename;
static CommPortIdentifier masterIdList;
boolean owned;
String owner; static
{
String s;
if((s = System.getProperty("javax.comm.properties")) != null)
System.err.println("Comm Drivers: " + s);
String s1 = System.getProperty("java.home") + File.separator + "lib" + File.separator + "javax.comm.properties";
try
{
loadDriver(s1);
propfilename = new String(s1);
}
catch(IOException _ex)
{
propfilename = findPropFile();
try
{
if(propfilename != null)
loadDriver(propfilename);
}
catch(IOException ioexception)
{
propfilename = new String(" ");
System.err.println(ioexception);
}
}
}
}
但用JAVA读串口还是有点问题读,读的数据会丢失,而且对长度不同的数据很难处理,因为SerialPortEventListener这个接口只有一个方法,而且没有返回值,现在我用VC写了一个串口通讯的程序,然后用JAVA调用,全部解决,如果想要,就留EMAIL
public class CommPortIdentifier
{
public static Enumeration getPortIdentifiers(){
…… } public static CommPortIdentifier getPortIdentifier(String s)
throws NoSuchPortException
{
SecurityManager securitymanager = System.getSecurityManager();
if(securitymanager != null)
securitymanager.checkDelete(propfilename);
CommPortIdentifier commportidentifier = null;
synchronized(lock)
{
for(commportidentifier = masterIdList; commportidentifier != null; commportidentifier = commportidentifier.next)
if(commportidentifier.name.equals(s))
break; }
if(commportidentifier != null)
return commportidentifier;
else
throw new NoSuchPortException();
} public static CommPortIdentifier getPortIdentifier(CommPort commport)
throws NoSuchPortException
{
SecurityManager securitymanager = System.getSecurityManager();
if(securitymanager != null)
securitymanager.checkDelete(propfilename);
CommPortIdentifier commportidentifier = null;
synchronized(lock)
{
for(commportidentifier = masterIdList; commportidentifier != null; commportidentifier = commportidentifier.next)
if(commportidentifier.port == commport)
break; }
if(commportidentifier != null)
return commportidentifier;
else
throw new NoSuchPortException();
} private static void addPort(CommPort commport, int i)
{……} public static void addPortName(String s, int i, CommDriver commdriver)
{……} public String getName()
{return name;} public int getPortType()
{return portType;} private native int nCreateMutex(String s);
private native int nCreateEvent(String s);
private native boolean nClaimMutex(int i, int j);
private native boolean nReleaseMutex(int i);
private native boolean nPulseEvent(int i);
private native boolean nSetEvent(int i);
private native boolean nCloseHandle(int i);
private void createNativeObjects()
{……} public synchronized CommPort open(String s, int i)
throws PortInUseException
{
if(!nativeObjectsCreated)
createNativeObjects();
if(owned)
{
maskOwnershipEvents = true;
fireOwnershipEvent(3);
maskOwnershipEvents = false;
if(owned)
throw new PortInUseException(owner);
}
port = driver.getCommPort(name, portType);
if(port == null)
{
nSetEvent(ownershipRequestedEvent);
for(int j = i <= 200 ? 200 : i; j > 0; j -= 200)
{
try
{
wait(200L);
}
catch(InterruptedException _ex) { }
port = driver.getCommPort(name, portType);
if(port != null)
break;
} if(port == null)
{
String s1 = nGetOwner(shname);
if(s1 == null || s1.length() == 0)
s1 = "Unknown Windows Application";
throw new PortInUseException(s1);
}
}
owned = true;
owner = s;
if(s == null || s.length() == 0)
shmem = nSetOwner(shname, "Unspecified Java Application", true);
else
shmem = nSetOwner(shname, s, true);
if(!nSetEvent(ownedEvent))
System.err.println("Error pulsing ownedEvent");
return port;
} private native int nSetOwner(String s, String s1, boolean flag); private native String nGetOwner(String s); private native void nUnsetOwner(String s); public String getCurrentOwner()
{…} public boolean isCurrentlyOwned()
{……} public void addPortOwnershipListener(CommPortOwnershipListener commportownershiplistener)
{……} public void removePortOwnershipListener(CommPortOwnershipListener commportownershiplistener)
{……} private native int nWaitForEvents(int i, int j, int k); void ownershipThreadWaiter()
{……} synchronized void internalClosePort()
{……} CommPortIdentifier(String s, CommPort commport, int i, CommDriver commdriver)
{
cpoList = new CpoList();
name = s;
port = commport;
portType = i;
next = null;
driver = commdriver;
shname = "javax.comm-" + s;
shmem = nSetOwner(shname, "", false);
} void fireOwnershipEvent(int i)
{……} private static String[] parsePropsFile(InputStream inputstream)
{……} private static void loadDriver(String s)
throws IOException
{
File file = new File(s);
BufferedInputStream bufferedinputstream = new BufferedInputStream(new FileInputStream(file));
String as[] = parsePropsFile(bufferedinputstream);
if(as != null)
{
for(int i = 0; i < as.length; i++)
if(as[i].regionMatches(true, 0, "driver=", 0, 7))
{
String s1 = as[i].substring(7);
s1.trim();
try
{
CommDriver commdriver = (CommDriver)Class.forName(s1).newInstance();
commdriver.initialize();
}
catch(Throwable throwable)
{
System.err.println("Caught " + throwable + " while loading driver " + s1);
}
}
}
} private static String findPropFile()
{……} public CommPort open(FileDescriptor filedescriptor)
throws UnsupportedCommOperationException
{
throw new UnsupportedCommOperationException();
} String name;
private int portType;
public static final int PORT_SERIAL = 1;
public static final int PORT_PARALLEL = 2;
private boolean nativeObjectsCreated;
private int ownedEvent;
private int unownedEvent;
private int ownershipRequestedEvent;
private int shmem;
private String shname;
private boolean maskOwnershipEvents;
OwnershipEventThread oeThread;
CpoList cpoList;
CommPortIdentifier next;
private CommPort port;
private CommDriver driver;
static Object lock = new Object();
static String propfilename;
static CommPortIdentifier masterIdList;
boolean owned;
String owner; static
{
String s;
if((s = System.getProperty("javax.comm.properties")) != null)
System.err.println("Comm Drivers: " + s);
String s1 = System.getProperty("java.home") + File.separator + "lib" + File.separator + "javax.comm.properties";
try
{
loadDriver(s1);
propfilename = new String(s1);
}
catch(IOException _ex)
{
propfilename = findPropFile();
try
{
if(propfilename != null)
loadDriver(propfilename);
}
catch(IOException ioexception)
{
propfilename = new String(" ");
System.err.println(ioexception);
}
}
}
}
解决方案 »
- 望有学习过JAVA的人回答?
- 问下 数据类型 整型 字符型 字符串型 之间的关系
- 关于nutch的配置问题
- Ant与JUnit整合问题(junitreport 标签),在线等....
- 联通短消息的编码
- 有谁用java开发过类似组态软件的应用软件?不知道用java开发难度大吗?
- 图片如何实现不同角度的转换(如:90度,180度,360度等等)
- 为会么在Solaris9中生成一个Frame类的实例时会抛出NoClassDefFoundError错误
- 在MyEclipse中运行下面代码出现的错误是:You have an error in your SQL syntax; check the manual t
- 怎样使生成的窗口不会被任意拉大?
- 我真的很迷茫!请大家帮帮忙!
- 这个程序怎么写?我是新手
在楼上我留了联系方式了,兄弟的问题对你来说应该不是问题,还望指点,如何配置JAVAX.COMM
谢谢了!
哪个文章我早就看了,但存在一个问题,就是我每次读的数据如果不一样长的话,就不好处理,因为他在读取数据的时候需要一个长度的参数,而我每次读取的数据是不定长的,所以就没有办法了。至于你的问题很简单,把配置和.dll文件复制到jre/lib/ext. jar复制到jre/lib就可以了
在JB 6的JDK 1.3.1下的jre/lib下没有ext这个文件夹啊,你是用的JB6吗
能认识一下吗,我做的东西和你的有很多共同之处,向高人学习啊,我在上海!
能留个联系方式吗,如果不方便的话,可以发个邮件给我啊!
编译还是通不过啊,真奇怪了,完全按照SUN的配置来的place the win32com.dll in <jdk>\jre\bin directory. Place the comm.jar in <jdk>\jre\lib\ext. Place the javax.comm.properties in <jdk>\jre\lib . Do not alter the CLASSPATH. 又麻烦你了,真不好意思 !