小弟刚接触串口,什么都不懂,希望大家指教!从sun 主页上 down 下了javacomm20-win32.zip,使用了其中的一个简单例子:
其中有两个类,一个为
import java.io.*;
import java.util.*;
import javax.comm.*;public class SimpleWrite {
    static Enumeration portList;
    static CommPortIdentifier portId;
    static String messageString = "Hello, world!\n";
    static SerialPort serialPort;
    static OutputStream outputStream;    public static void main(String[] args) {
        portList = CommPortIdentifier.getPortIdentifiers();        while (portList.hasMoreElements()) {
            portId = (CommPortIdentifier) portList.nextElement();
           /*如果端口类型是串口,则打印出其端口信息*/
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
System.out.println(portId.getName());
                
if (portId.getName().equals("COM1")) {
                //if (portId.getName().equals("/dev/term/a")) {
                    try {
                        serialPort = (SerialPort)
                            portId.open("SimpleWriteApp", 2000);
                    } catch (PortInUseException e) {
System.out.println(e.toString());
}
                    try {
                        outputStream = serialPort.getOutputStream();
                    } catch (IOException e) {
System.out.println(e.toString());
}
                    try {
                        serialPort.setSerialPortParams(9600,
                            SerialPort.DATABITS_8,
                            SerialPort.STOPBITS_1,
                            SerialPort.PARITY_NONE);
                    } catch (UnsupportedCommOperationException e) {}
                    try {
                        outputStream.write(messageString.getBytes());
                    } catch (IOException e) {}
                }
            }
        }
    }
}
另一个为:
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();        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;
        }
    }
}
 胡乱配置了comm.jar之后,发现通过上面的SimpleWrite可以检测到com1(只有com1,没有com2)。但不知道是否把数据写入到串口,也不知道怎么读出写入串口的数据?
请问:怎么读出写入串口的数据?不胜感激!