iteye_11551 2008-11-14 15:52
浏览 218
已采纳

关于javacomm 的一段代码,就是不能分析本机com1(or其他串口)

[code="java"]

package com.hxks.xsm;
import java.io.*;
import java.util.*;
import javax.comm.*;

public class SimpleRead implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;

InputStream inputStream;
SerialPort serialPort;
Thread readThread;

//读取设备端口号
public static void main(String[] args) {
    portList = CommPortIdentifier.getPortIdentifiers();

//System.out.println("[]____________");
while (portList.hasMoreElements()) {
System.out.println("||||||||||||||||");
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)//这里根本没判断
{
System.out.println("_________");
if (portId.getName().equals("COM1"))//而我本机是开了com1串口 进行发数据的
{
// if (portId.getName().equals("/dev/term/a")) {
SimpleRead reader = new SimpleRead();

System.out.println("............");
}
}
}
}

public SimpleRead() {
    try {
        serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
    } catch (PortInUseException e) {}
    try {
        inputStream = serialPort.getInputStream();
    } catch (IOException e) {}
try {
        serialPort.addEventListener(this);
} catch (TooManyListenersException e) {}
    serialPort.notifyOnDataAvailable(true);
    try {
        serialPort.setSerialPortParams(9600,
            SerialPort.DATABITS_8,
            SerialPort.STOPBITS_1,
            SerialPort.PARITY_NONE);
    } catch (UnsupportedCommOperationException e) {}
    readThread = new Thread(this);
    readThread.start();
}

public void run() {
    try {
        Thread.sleep(20000);
    } catch (InterruptedException e) {}
}

public void serialEvent(SerialPortEvent event) {
    switch(event.getEventType()) {
    case SerialPortEvent.BI:
    case SerialPortEvent.OE:
    case SerialPortEvent.FE:
    case SerialPortEvent.PE:
    case SerialPortEvent.CD:
    case SerialPortEvent.CTS:
    case SerialPortEvent.DSR:
    case SerialPortEvent.RI:
    case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
        break;
        //DATA_AVAILABLE 为设备有数据情况
    case SerialPortEvent.DATA_AVAILABLE:
        byte[] readBuffer = new byte[20];

        try {
            while (inputStream.available() > 0) {
                int numBytes = inputStream.read(readBuffer);
            }
            System.out.print(new String(readBuffer));
        } catch (IOException e) {}
        break;
    }
}

}
[/code]
ps 关于javacomm的 一些文章都看了 什么javax.comm.properties我也放到C:\Program Files\Java\jdk1.5.0\lib目录之下了 ,还有
win32得那个dll 也放在C:\Program Files\Java\jdk1.5.0\bin之下了 ,jar导入项目中。
另外一些人 由于win32的 冲突问题还报错 我这个程序啥错不报。
[b]问题补充:[/b]

 
package  com.hxks.xsm;
import java.io.*;
import java.util.*;
import javax.comm.*;

public class SimpleRead implements Runnable, SerialPortEventListener {
    static CommPortIdentifier portId;
    static Enumeration portList;

    InputStream inputStream;
    SerialPort serialPort;
    Thread readThread;

    //读取设备端口号
    public static void main(String[] args) {
        portList = CommPortIdentifier.getPortIdentifiers();
//System.out.println("[]____________");
        while (portList.hasMoreElements()) {
System.out.println("||||||||||||||||");
            portId = (CommPortIdentifier) portList.nextElement();
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)//这里根本没判断
 {
System.out.println("_________");
                if (portId.getName().equals("COM1"))//而我本机是开了com1串口 进行发数据的
 {
               // if (portId.getName().equals("/dev/term/a")) {
                    SimpleRead reader = new SimpleRead();
                   
System.out.println("............");
                }
            }
        }
    }

    public SimpleRead() {
        try {
            serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
        } catch (PortInUseException e) {}
        try {
            inputStream = serialPort.getInputStream();
        } catch (IOException e) {}
    try {
            serialPort.addEventListener(this);
    } catch (TooManyListenersException e) {}
        serialPort.notifyOnDataAvailable(true);
        try {
            serialPort.setSerialPortParams(9600,
                SerialPort.DATABITS_8,
                SerialPort.STOPBITS_1,
                SerialPort.PARITY_NONE);
        } catch (UnsupportedCommOperationException e) {}
        readThread = new Thread(this);
        readThread.start();
    }

    public void run() {
        try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {}
    }

    public void serialEvent(SerialPortEvent event) {
        switch(event.getEventType()) {
        case SerialPortEvent.BI:
        case SerialPortEvent.OE:
        case SerialPortEvent.FE:
        case SerialPortEvent.PE:
        case SerialPortEvent.CD:
        case SerialPortEvent.CTS:
        case SerialPortEvent.DSR:
        case SerialPortEvent.RI:
        case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
            break;
            //DATA_AVAILABLE 为设备有数据情况
        case SerialPortEvent.DATA_AVAILABLE:
            byte[] readBuffer = new byte[20];

            try {
                while (inputStream.available() > 0) {
                    int numBytes = inputStream.read(readBuffer);
                }
                System.out.print(new String(readBuffer));
            } catch (IOException e) {}
            break;
        }
    }
}

ps 关于javacomm的 一些文章都看了 什么javax.comm.properties我也放到C:\Program Files\Java\jdk1.5.0\lib目录之下了 ,还有
win32得那个dll 也放在C:\Program Files\Java\jdk1.5.0\bin之下了 ,jar导入项目中。

另外一些人 由于win32的 冲突问题还报错 我这个程序啥错不报。

多谢lx的朋友解答 但是这个myeclipse 自带的是j2ee的jar 我用的是j2sdk的呵
[b]问题补充:[/b]

多谢lx的朋友解答 但是这个myeclipse 自带的是j2ee的jar 我用的是j2sdk的呵

  • 写回答

2条回答 默认 最新

  • iteye_521 2008-11-14 17:14
    关注

    [quote]但是这个myeclipse 自带的是j2ee的jar 我用的是j2sdk的呵 [/quote]
    myeclipse新建一个工程后只会把JDK中标准的的jar加载到构建路径中,估计是找不到你的DLL的,把你的win32得那个dll放到系统system32下试试,或放到构建路径中试试

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!