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;//此外还有什么PORT,这个可能是串口吧?
    Thread readThread;    public static void main(String[] args) {
        portList = CommPortIdentifier.getPortIdentifiers();//这个玩艺会自己去搜索硬件设备?        while (portList.hasMoreElements()) {
            portId = (CommPortIdentifier) portList.nextElement();
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                // if (portId.getName().equals("COM1")) {
                if (portId.getName().equals("/dev/term/a")) {
                    SimpleRead reader = new SimpleRead();
                }
            }
        }
    }    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;
        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;
        }
    }
}

解决方案 »

  1.   

    http://www.xici.net/board/xici_doc_show.asp?id=VXnRUcnCfXjtUcVtfDLRfbQEfXVDf2ZuAbQQ&grp=108016
    看看这个帖子,希望能对你有帮助
      

  2.   

    用的是SUN的串口控件:
    ---------------
    package test;
    import javax.comm.*;
    import java.io.*;
    import java.util.*;
    public class Comm
    {
        static javax.comm.CommPortIdentifier portId;//串口管理器,负责打开,调用,和初始化串口等管理工作
        static javax.comm.SerialPort serialPort;//负责串口的数据处理工作(如打开输入输出流,设置波特率等工作
        static java.io.OutputStream os;//输出流
        public static void main(String []a)
        {
            System.out.println("site 1");
            try
            {
                portId=javax.comm.CommPortIdentifier.getPortIdentifier("COM4");//我的串口的名字叫COM4,一般的人是COM1
                serialPort = (SerialPort)portId.open("SimpleWriteApp", 2000);//如果2000毫秒内打不开串口,就报异常
                serialPort.setSerialPortParams(57600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);//设置好波特率等串口参数
                os=serialPort.getOutputStream();
                os.write(("test writing\r\n").getBytes());
            }
            catch(Exception ex)
            {
            }
        }
    }