Java Tutorial/Development/CommPortIdentifier

Материал из Java эксперт
Перейти к: навигация, поиск

Port Finder

import java.io.*;
import javax.rum.*;
import java.util.*;
public class Main
{    
    public static void main(String[] args)
    {
        Enumeration ports = CommPortIdentifier.getPortIdentifiers();
        
        while(ports.hasMoreElements())
        {
            CommPortIdentifier cpi =
                           (CommPortIdentifier)ports.nextElement();
            System.out.println("Port " + cpi.getName());
        }
    }
}





Port Reader

import java.io.*;
import javax.rum.*;
import java.util.*;
public class PortReader SerialPortEventListener {
  static Enumeration ports;
  static CommPortIdentifier pID;
  InputStream inStream;
  SerialPort serPort;
  public PortReader() throws Exception{
    serPort = (SerialPort) pID.open("PortReader", 2000);
    inStream = serPort.getInputStream();
    serPort.addEventListener(this);
    serPort.notifyOnDataAvailable(true);
    serPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
        SerialPort.PARITY_NONE);
  }
  public void serialEvent(SerialPortEvent event) {
    switch (event.getEventType()) {
    case SerialPortEvent.BI:
      System.out.println("SerialPortEvent.BI occurred");
    case SerialPortEvent.OE:
      System.out.println("SerialPortEvent.OE occurred");
    case SerialPortEvent.FE:
      System.out.println("SerialPortEvent.FE occurred");
    case SerialPortEvent.PE:
      System.out.println("SerialPortEvent.PE occurred");
    case SerialPortEvent.CD:
      System.out.println("SerialPortEvent.CD occurred");
    case SerialPortEvent.CTS:
      System.out.println("SerialPortEvent.CTS occurred");
    case SerialPortEvent.DSR:
      System.out.println("SerialPortEvent.DSR occurred");
    case SerialPortEvent.RI:
      System.out.println("SerialPortEvent.RI occurred");
    case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
      System.out.println("SerialPortEvent.OUTPUT_BUFFER_EMPTY occurred");
      break;
    case SerialPortEvent.DATA_AVAILABLE:
      System.out.println("SerialPortEvent.DATA_AVAILABLE occurred");
      byte[] readBuffer = new byte[20];
      try {
        while (inStream.available() > 0) {
          int numBytes = inStream.read(readBuffer);
        }
        System.out.print(new String(readBuffer));
      } catch (IOException ioe) {
        System.out.println("Exception " + ioe);
      }
      break;
    }
  }
  public static void main(String[] args) throws Exception{
    ports = CommPortIdentifier.getPortIdentifiers();
    while (ports.hasMoreElements()) {
      pID = (CommPortIdentifier) ports.nextElement();
      System.out.println("Port " + pID.getName());
      if (pID.getPortType() == CommPortIdentifier.PORT_SERIAL) {
        if (pID.getName().equals("COM1")) {
          PortReader pReader = new PortReader();
          System.out.println("COM1 found");
        }
      }
    }
  }
}





Port Sniffer

import java.io.*;
import javax.rum.*;
import java.util.*;
public class PortSniffer SerialPortEventListener
{
    static Enumeration ports;
    static CommPortIdentifier pID;
    InputStream inStream;
    SerialPort serPort;
    
    public PortSniffer() throws Exception
    {
        serPort = (SerialPort)pID.open("PortSniffer",2000);
        inStream = serPort.getInputStream();
        serPort.addEventListener(this);
        
        serPort.notifyOnDataAvailable(true);
        serPort.notifyOnBreakInterrupt(true);
        serPort.notifyOnCarrierDetect(true);
        serPort.notifyOnCTS(true);
        serPort.notifyOnDataAvailable(true);
        serPort.notifyOnDSR(true);
        serPort.notifyOnFramingError(true);
        serPort.notifyOnOutputEmpty(true);
        serPort.notifyOnOverrunError(true);
        serPort.notifyOnParityError(true);
        serPort.notifyOnRingIndicator(true);
        
        serPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
        SerialPort.STOPBITS_1,
        SerialPort.PARITY_NONE);
    }    
    public void serialEvent(SerialPortEvent event)
    {
        switch(event.getEventType())
        {
            case SerialPortEvent.BI:
                System.out.println("SerialPortEvent.BI occurred");
            case SerialPortEvent.OE:
                System.out.println("SerialPortEvent.OE occurred");
            case SerialPortEvent.FE:
                System.out.println("SerialPortEvent.FE occurred");
            case SerialPortEvent.PE:
                System.out.println("SerialPortEvent.PE occurred");
            case SerialPortEvent.CD:
                System.out.println("SerialPortEvent.CD occurred");
            case SerialPortEvent.CTS:
                System.out.println("SerialPortEvent.CTS occurred");
            case SerialPortEvent.DSR:
                System.out.println("SerialPortEvent.DSR occurred");
            case SerialPortEvent.RI:
                System.out.println("SerialPortEvent.RI occurred");
            case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
                System.out.println("SerialPortEvent.OUTPUT_BUFFER_EMPTY occurred");
                break;
            case SerialPortEvent.DATA_AVAILABLE:
                System.out.println("SerialPortEvent.DATA_AVAILABLE occurred");
                byte[] readBuffer = new byte[20];
                
                try
                {
                    while (inStream.available() > 0)
                    {
                        int numBytes = inStream.read(readBuffer);
                    }
                    System.out.print(new String(readBuffer));
                 }catch (IOException ioe)
                {
                    System.out.println("Exception " + ioe);
                }
                break;
        }
    }
    
    public static void main(String[] args)throws Exception{
        ports = CommPortIdentifier.getPortIdentifiers();
        
        while(ports.hasMoreElements())
        {
            pID = (CommPortIdentifier)ports.nextElement();
            System.out.println("Port " + pID.getName());
            
            if (pID.getPortType() == CommPortIdentifier.PORT_SERIAL)
            {
                if (pID.getName().equals("COM1"))
                {
                    PortSniffer pSniffer = new PortSniffer();
                    System.out.println("COM1 found");
                }
            }
        }
    }
}





Port Writer

import java.io.*;
import javax.rum.*;
import java.util.*;
public class PortWriter
{
    static Enumeration ports;
    static CommPortIdentifier pID;
    static OutputStream outStream;
    SerialPort serPort;
    static String messageToSend = "message!\n";
    
    public PortWriter() throws Exception{
        serPort = (SerialPort)pID.open("PortWriter",2000);
        outStream = serPort.getOutputStream();
        serPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
        SerialPort.STOPBITS_1,
        SerialPort.PARITY_NONE);
    }    
    
    public static void main(String[] args) throws Exception{
        ports = CommPortIdentifier.getPortIdentifiers();
        
        while(ports.hasMoreElements())
        {
            pID = (CommPortIdentifier)ports.nextElement();
            System.out.println("Port " + pID.getName());
            
            if (pID.getPortType() == CommPortIdentifier.PORT_SERIAL)
            {
                if (pID.getName().equals("COM1"))
                {
                    PortWriter pWriter = new PortWriter();
                    System.out.println("COM1 found");
                }
            }
        }
        outStream.write(messageToSend.getBytes());
    }
}