一個基本入門的介紹:

http://cysky.blog.hexun.com.tw/6387182_d.html

範例code:

import java.util.Enumeration;
import gnu.io.*;
import java.io.*;
import javax.swing.*;
import java.util.*;
 
 
public class Core extends JFrame implements Runnable, SerialPortEventListener
{
 
    private static SerialPort serialPort;
    private static InputStream inStream;
    private static OutputStream outStream;
    private static String inputLine; 
static String TimeStamp;
    protected      String divertCode = "10";
 
    Thread readThread;
 
//===================================================================<constructor>
    publicCore()
    { 
            
        Enumeration ports = CommPortIdentifier.getPortIdentifiers();
        System.out.println("start");
        while(ports.hasMoreElements())
        {
            CommPortIdentifier port = (CommPortIdentifier) ports.nextElement();
            System.out.print(port.getName() + " -> " + port.getCurrentOwner() + " -> ");
            switch(port.getPortType())
            {
                case CommPortIdentifier.PORT_PARALLEL:
                    System.out.println("parell");
                break;
                case CommPortIdentifier.PORT_SERIAL:
                    System.out.println("serial");
                try {
                    serialPort = (SerialPort) port.open("core", 9600);
                    try
{
serialPort.enableReceiveTimeout(1000);
serialPort.enableReceiveThreshold(0);
}
catch (UnsupportedCommOperationException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
                    int baudRate = 9600; // 2400bps
                    serialPort.setSerialPortParams(
                            baudRate,
                            SerialPort.DATABITS_8,
                            SerialPort.STOPBITS_1,
                            SerialPort.PARITY_NONE);
                    System.out.println("sucess");
                } catch (PortInUseException e) {
                    System.out.println(e.getMessage());
                } catch (UnsupportedCommOperationException e) {
                    System.out.println(e.getMessage());
                }
                break;
            }
        }
 
        try
{
            serialPort.addEventListener(this);
        } 
        catch (TooManyListenersException e) {}
 
        serialPort.notifyOnDataAvailable(true);
        readThread = new Thread(this);
        readThread.start();
    }//==================================================================end of constructor
 
 
//===================================================================run [method][override Runable method]
    public void run()
    {
       try
       {
           Thread.sleep(100);
       }
       catch (InterruptedException e) {}
    }//==================================================================end of public void run
 
 
 
//===================================================================serialEvent [method] [Serial port Event listener]
    public void serialEvent(SerialPortEvent event)
    {
      try
      {
        System.out.println((serialPort.getInputStream()).read());
      }
      catch (Exception e) {}
    }//==================================================================end of public void serialEvent 
 
 
 
 
 
    public static void main(String[] args) 
    {
        Core f = new Core();
        f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        f.setSize( 350, 150 );
        f.setVisible( true );
    }
}
arrow
arrow
    全站熱搜

    vincint810923 發表在 痞客邦 留言(0) 人氣()