请各位大侠帮我看看java串口通信的问题

Gao_TF 2004-11-23 08:46:18
java 进行串口通信的流程是什么样的?我这里有一段别人的代码,说的很详细,但是运行报错。
如下
/*
*
* IMU.java 1.0
* Main Program for Serial Communication
*
* Created: March 27, 2001
*
* Author : Qingye Jiang (John)
* American GNC Corporation
* 888 Easy St, Simi Valley CA 93065-1812
*
* Contact: (805) 582-0582 (Tel), (805) 582-0098 (Fax)
* qjiang@tsinghua.edu
*
*/

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

class IMU
{

static CommPortIdentifier portId;
static SerialPort serialPort;
static OutputStream out;
static InputStream in;

public static void main(String[] args)
{
try
{
//Declare the serial port, and open it.
portId = CommPortIdentifier.getPortIdentifier("COM1");
System.out.println("portId.getName()");
System.out.println(portId.getName());
try
{
serialPort = (SerialPort) portId.open("IMU_App", 2000);
} catch (PortInUseException e)
{
//System.out.println(e.getMessage());
System.out.println("2");
}

//Use InputStream in to read from the serial port, and utputStream
//out to write to the serial port.
try
{
in = serialPort.getInputStream();
out = serialPort.getOutputStream();
} catch (IOException e)
{
//System.out.println(e.getMessage());
System.out.println("3");
}
//Initialize the communication parameters to 9600, 8, 1, none.
try
{
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);

} catch (UnsupportedCommOperationException e)
{
//System.out.println(e.getMessage());
System.out.println("serial point error");
}

} catch (NoSuchPortException e)
{

//System.out.println(e.getMessage());
System.out.println(portId.getName());
}

//Declare the serial buffer area, a thread to read from the seriial port,
//a thread to read from the serial buffer for processing, and a thread
//to write to the serial port.

SerialBuffer SB = new SerialBuffer();
ReadSerial r1 = new ReadSerial(SB, in);
ReadBuffer r2 = new ReadBuffer(SB);
WriteSerial r3 = new WriteSerial(out);


//Start all three threads.

r1.start();
r2.start();
r3.start();

}

}

这是主类,调用三个线曾
还有其它的三个类,
但是portId = CommPortIdentifier.getPortIdentifier("COM1");
这一步经过验证,portId是null
请问这是怎么回师
...全文
198 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
loswing 2004-11-29
  • 打赏
  • 举报
回复
:
Gao_TF 2004-11-24
  • 打赏
  • 举报
回复
大家帮我看一看吧
Gao_TF 2004-11-24
  • 打赏
  • 举报
回复
to avaj2004() :
非常感谢你的帮助!
我现在的问题是,在这一步
Enumeration portList = CommPortIdentifier.getPortIdentifiers();
portList 返回的是空直,进不去while循环
这是怎么回事?
我到别人的机器上也式过,结果一样
是不是机器断口需要配置啊
谢谢
avaj2004 2004-11-24
  • 打赏
  • 举报
回复
import java.util.*;
import javax.comm.*;

public class Test2 {
public static void main(String args[]) {

//列出你的计算机上的所有的串口的名称
Enumeration portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
System.out.println(portId.getName());
}
}

//如果"COM1"不存在的话就会抛出NoSuchPortException
try {
CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier("COM1");
System.out.println(portId.getName());
} catch (NoSuchPortException nspe) {
System.out.println(nspe);
}
}
}
Gao_TF 2004-11-24
  • 打赏
  • 举报
回复
怎么还是没有人回答啊,诸位帮帮忙啊,帮我分析一下,这里先谢谢了
Gao_TF 2004-11-24
  • 打赏
  • 举报
回复
哈哈,好了,搞定了,谢谢,非常感谢
avaj2004 2004-11-24
  • 打赏
  • 举报
回复
http://community.csdn.net/Expert/topic/3582/3582746.xml?temp=.8737909
Tangel0904 2004-11-24
  • 打赏
  • 举报
回复
javax.comm.properties放到jre\lib底下,comm.jar放到classPath中,win32com.dll放到系统目录底下
LittleLandlord 2004-11-24
  • 打赏
  • 举报
回复
去SUN下载或者到GOOGLE上搜索commapi,下载完解压,就有这三个文件了,有什么问题加我QQ吧173120176
Gao_TF 2004-11-24
  • 打赏
  • 举报
回复
请问一下,这三个包分别放在哪里?
bfox 2004-11-24
  • 打赏
  • 举报
回复
mark
LittleLandlord 2004-11-24
  • 打赏
  • 举报
回复
你的问题我遇到过,安装完这3个文件就OK了
LittleLandlord 2004-11-24
  • 打赏
  • 举报
回复
Sun公司提供的Java串口包你没装上,javax.comm.properties,comm.jar,win32com.dll,这三个文件在串口包里面,要拷贝到相应的目录,先下载commapi这个压缩包吧。
avaj2004 2004-11-24
  • 打赏
  • 举报
回复
请到sun的网站下载javax.comm包,分win32和Solaris两种,
win32包里面个配置说明文档JDK1.2.html
Gao_TF 2004-11-23
  • 打赏
  • 举报
回复
大侠,救命啊,我真的不知道怎么办了1!



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

class IMU
{

static CommPortIdentifier portId;
static SerialPort serialPort;
static OutputStream out;
static InputStream in;

public static void main(String[] args)
{
try
{
//Declare the serial port, and open it.
portId = CommPortIdentifier.getPortIdentifier("COM1");

{

//System.out.println(e.getMessage());
System.out.println(portId.getName());
}


}

}
好了,这回代码少了,可以看清楚了吧,就是那句话
portId = CommPortIdentifier.getPortIdentifier("COM1");
得不到com1啊,为什么啊?
portId是空的(null),这是为什么
Gao_TF 2004-11-23
  • 打赏
  • 举报
回复
大哥,大姐们,救命啊
Gao_TF 2004-11-23
  • 打赏
  • 举报
回复
to j2nix(爱生活爱星辉) :
谢谢先!
问题是,我现在不能取得CommPortIdentifier类的一个实例啊
我通过下面的语句来取
static CommPortIdentifier portId;
portId = CommPortIdentifier.getPortIdentifier("COM1");
但是取得的是null
就是说portId是null
到底是哪里出了问题呢
j2nix 2004-11-23
  • 打赏
  • 举报
回复
还是参考一下这个吧:

http://tech.sina.com.cn/c/2004-05-10/1600359503.shtml
Gao_TF 2004-11-23
  • 打赏
  • 举报
回复
怎么没人啊,非常感谢大家,谢谢大家,帮帮忙吧,谢谢了

62,614

社区成员

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

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