有关串口读取数据的问题。

zceast 2003-07-08 02:18:33
我在运行 commBean1.Initialize();的时候会出现如下的异常:

javax.comm.NoSuchPortException

at javax.comm.CommPortIdentifier.getPortIdentifier(CommPortIdentifier.java:105)

at study.CommBean.Initialize(CommBean.java:32)

at study.CommBean.main(CommBean.java:23)

也就说CommPortIdentifier.getPortIdentifier(portName);返回的是null;

我用的是IBM THINKPAD R31 ,这个机器本身是没有串口的,但我用USB转串的线。
因为我用超级终端是可以打开COM3的,而且可以从COM3上面读取数据。
谁能帮我解释,给分。

import javax.comm.*;
import java.io.*;
import java.util.*;

public class CommBean {

protected SerialPort serialPort;
private static String portName;
private static OutputStream out;
private static InputStream in;
private CommPortIdentifier portId;
private BufferedInputStream reader;
private String readString;
private int numBytes=0;

public CommBean(int portID) {
portName="COM"+portID;
}
public static void main(String[] args) {
CommBean commBean1 = new CommBean(3);
commBean1.Initialize();

}

public boolean Initialize(){
boolean InitSuccess=true;
boolean InitFail=false;
try{
System.out.println(portName);
portId=CommPortIdentifier.getPortIdentifier(portName);
try{
serialPort=(SerialPort)portId.open("Serial_Communication", 2000);
}catch(PortInUseException e){
System.out.println("端口被占用");
return InitFail;
}
try{
in=serialPort.getInputStream();
reader=new BufferedInputStream(in);
out=serialPort.getOutputStream();
}catch (IOException e){
System.out.println("创建流失败");
return InitFail;
}
try{
serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
}catch(UnsupportedCommOperationException e){
System.out.println("参数设置失败");
return InitFail;
}

}catch (NoSuchPortException e){
System.out.println("失败");
System.out.println(e.getMessage());
return InitFail;
}
System.out.println("成功");
return InitSuccess;
}

public void ReadPort(){
try{
serialPort.addEventListener(new commListener());
}catch(Exception e){}
serialPort.notifyOnDataAvailable(true);
}

public void StopRead(){
try{
serialPort.removeEventListener();
}catch(Exception e){}
serialPort.notifyOnDataAvailable(false);
}

public void WritePort(String Msg){
try{
for(int i=0;i<Msg.length();i++)
out.write(Msg.charAt(i));
}catch(IOException e){
}
}

public void ClosePort(){
serialPort.close();
}

public class commListener implements SerialPortEventListener{
public void serialEvent(SerialPortEvent event) {
byte[] readBuffer=new byte[100];
if(event.getEventType()==SerialPortEvent.DATA_AVAILABLE){
try {
while (in.available() > 0) {
numBytes = reader.read(readBuffer);
}
readString+= new String(readBuffer,0,numBytes);
//处理自己的字符串
}catch (IOException e) {}
}
}
}

}
...全文
282 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
eBeerlu 2003-07-11
  • 打赏
  • 举报
回复
老大:
编译还是通不过啊,真奇怪了,完全按照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.

又麻烦你了,真不好意思 !
zceast 2003-07-11
  • 打赏
  • 举报
回复
还有 ,我可能写反了, 因为我把那几个文件删了,反正放哪儿肯定可以
zceast 2003-07-11
  • 打赏
  • 举报
回复
没有的话就自己建一个就是啦。1.3的确是没有的,就是要自己建
eBeerlu 2003-07-10
  • 打赏
  • 举报
回复
这位仁兄,可否给兄弟一份,兄弟正在忙这个东东urfriend@citiz.net

另外请教下载了JAVAX.COMM后如何配置啊,我用的是JBuilder 6,内置jdk 1.3.1

小弟原来用VC写过一个的,不过很简单,现在也准备用JAVA调用,但是不是很明白,兄弟能不能给小弟发一份啊,不胜感激!!!
eBeerlu 2003-07-10
  • 打赏
  • 举报
回复
to:zceast
在JB 6的JDK 1.3.1下的jre/lib下没有ext这个文件夹啊,你是用的JB6吗
能认识一下吗,我做的东西和你的有很多共同之处,向高人学习啊,我在上海!
能留个联系方式吗,如果不方便的话,可以发个邮件给我啊!
zceast 2003-07-10
  • 打赏
  • 举报
回复
eBeerlu(k7)
哪个文章我早就看了,但存在一个问题,就是我每次读的数据如果不一样长的话,就不好处理,因为他在读取数据的时候需要一个长度的参数,而我每次读取的数据是不定长的,所以就没有办法了。至于你的问题很简单,把配置和.dll文件复制到jre/lib/ext. jar复制到jre/lib就可以了
yanfeilin 2003-07-10
  • 打赏
  • 举报
回复
给兄弟一份,chengfeis@mail.banner.com.cn
eBeerlu 2003-07-10
  • 打赏
  • 举报
回复
这位兄弟:IBM网站上有篇文章,你到JAVA专区搜索一下,叫做全双工的串口通讯,如果你没看过的话,我想对你有点帮助的!如果找不到我可以发给你
在楼上我留了联系方式了,兄弟的问题对你来说应该不是问题,还望指点,如何配置JAVAX.COMM
谢谢了!
zceast 2003-07-10
  • 打赏
  • 举报
回复
呵呵,兄弟门,我高兴的有点早了,因为在处理中文中出现了问题,能给解释一下吗
liuwillfly1 2003-07-09
  • 打赏
  • 举报
回复
我也需要了解方面的知识,
takecare 2003-07-09
  • 打赏
  • 举报
回复
呵呵,恭喜!
zceast 2003-07-09
  • 打赏
  • 举报
回复
我搞顶了。这个是反编译的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);
}
}
}
}

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧