端口监听 : 串行通信口 « 开发相关 « Java 教程

En
Java 教程
1. 语言基础
2. 数据类型
3. 操作符
4. 流程控制
5. 类定义
6. 开发相关
7. 反射
8. 正则表达式
9. 集合
10. 线
11. 文件
12. 泛型
13. 本土化
14. Swing
15. Swing事件
16. 二维图形
17. SWT
18. SWT 二维图形
19. 网络
20. 数据库
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web服务SOA
27. EJB3
28. Spring
29. PDF
30. 电子邮件
31. 基于J2ME
32. J2EE应用
33. XML
34. 设计模式
35. 日志
36. 安全
37. Apache工具
38. 蚂蚁编译
39. JUnit单元测试
Java
Java 教程 » 开发相关 » 串行通信口 
6. 53. 3. 端口监听
import java.io.*;
import javax.comm.*;
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");
                }
            }
        }
    }

}
6. 53. 串行通信口
6. 53. 1. 端口搜索
6. 53. 2. 端口读
6. 53. 3. 端口监听
6. 53. 4. 端口写
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.