怎么可能读不到数据呢?
你试试comm包的那些例子程序好了,我就是参考那些例子来做的,
程序都是没有问题的.

解决方案 »

  1.   

    import java.io.*;
    import java.util.*;
    import javax.comm.*;public class Dial_up implements SerialPortEventListener{
        static Enumeration portList;
        static CommPortIdentifier portId;
        static String messageString = "Hello, world!\n";
        static SerialPort serialPort;
        static OutputStream outputStream;
        static InputStream inputStream;    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")) {
                        try {
                            serialPort = (SerialPort)
                                portId.open("SimpleWriteApp", 2000);
                        } catch (PortInUseException e) {}                    try {
                            outputStream = serialPort.getOutputStream();
                        } catch (IOException e) {}                   try {
                         inputStream = serialPort.getInputStream();
                       } catch (IOException e) {}                    try {
                            serialPort.setSerialPortParams(9600,
                                SerialPort.DATABITS_8,
                                SerialPort.STOPBITS_1,
                                SerialPort.PARITY_NONE);
                        } catch (UnsupportedCommOperationException e) {}                    try {
                           serialPort.addEventListener(new SerialListener1());
                        } catch (TooManyListenersException e) {}
                        serialPort.notifyOnDataAvailable(true);                    try {
                            outputStream.write(messageString.getBytes());
                        } catch (IOException e) {}
                    }
                }//end of if COM1
            }//end of while
        }//end of main    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;
            }
        }
    }//end of class