解决方案 »

  1.   

    我自己写了一个监(为什么这是和谐词语)听器,想监(为什么这是和谐词语)听别的串口发给我的串口的数据,但是不起效果,为什么啊?代码如下:
    SerialDemoMain类,主类:package com.ue.serialDemo;import java.io.IOException;
    import java.io.OutputStream;
    import java.util.TooManyListenersException;import javax.comm.CommPortIdentifier;
    import javax.comm.NoSuchPortException;
    import javax.comm.PortInUseException;
    import javax.comm.SerialPort;public class SerialDemoMain {
    public CommPortIdentifier portId;
    public SerialPort serialPort ;
     /**
     * 打开串口
     * @throws NoSuchPortException 
     * @throws PortInUseException 
     * @throws TooManyListenersException 
     */
    public void open() throws NoSuchPortException, PortInUseException, TooManyListenersException{
    portId = CommPortIdentifier.getPortIdentifier("COM1");
    serialPort = (SerialPort) portId.open("zgck", 600);
    serialPort.addEventListener(new MyPortListener(serialPort));
     }
    public void sendMsg(String msg) throws IOException, InterruptedException{
    /*往端口中写数据*/
    OutputStream os = serialPort.getOutputStream();

    if("1".equals(msg)){
         os.write(0x3B);
         os.write(0x55);
         os.write(0x31);
         os.write(0x0D);
        
         Thread.sleep(1000);
        
         os.write(0x3B);
         os.write(0x44);
         os.write(0x31);
         os.write(0x0D);
    }else if("2".equals(msg)){
    os.write(0x3B);
         os.write(0x55);
         os.write(0x32);
         os.write(0x0D);
        
        
         Thread.sleep(1000);
        
         os.write(0x3B);
         os.write(0x44);
         os.write(0x32);
         os.write(0x0D);
    }else if("3".equals(msg)){
    os.write(0x3B);
         os.write(0x55);
         os.write(0x33);
         os.write(0x0D);
        
        
         Thread.sleep(1000);
        
         os.write(0x3B);
         os.write(0x44);
         os.write(0x33);
         os.write(0x0D);
    }else{
    System.out.println("无法识别的数据:"+msg);
    }

    }


    public static void main(String[] args) throws NoSuchPortException, PortInUseException, TooManyListenersException {
    SerialDemoMain sm2 = new SerialDemoMain();
    sm2.open();

    }
    }
    MyPortListener类,监(为什么这是和谐词语)听器类:package com.ue.serialDemo;import java.io.IOException;
    import java.io.InputStream;import javax.comm.CommPort;
    import javax.comm.SerialPort;
    import javax.comm.SerialPortEvent;
    import javax.comm.SerialPortEventListener;public class MyPortListener implements SerialPortEventListener{
    public SerialPort serialPort ;
    public MyPortListener(SerialPort serialPort ) {
    this.serialPort = serialPort;
    }
    @Override
    public void serialEvent(SerialPortEvent evt)
     
    {
     
    switch (evt.getEventType())
     
    {
     
    case SerialPortEvent.CTS :
     
    System.out.println("CTS event occured.");
     
    break;
     
    case SerialPortEvent.CD :
     
    System.out.println("CD event occured.");
     
    break;
     
    case SerialPortEvent.BI :
     
    System.out.println("BI event occured.");
     
    break;
     
    case SerialPortEvent.DSR :
     
    System.out.println("DSR event occured.");
     
    break;
     
    case SerialPortEvent.FE :
     
    System.out.println("FE event occured.");
     
    break;
     
    case SerialPortEvent.OE :
     
    System.out.println("OE event occured.");
     
    break;
     
    case SerialPortEvent.PE :
     
    System.out.println("PE event occured.");
     
    break;
     
    case SerialPortEvent.RI :
     
    System.out.println("RI event occured.");
     
    break;
     
    case SerialPortEvent.OUTPUT_BUFFER_EMPTY :
     
    System.out.println("OUTPUT_BUFFER_EMPTY event occured.");
     
    break;
     
    case SerialPortEvent.DATA_AVAILABLE :
     
    System.out.println("DATA_AVAILABLE event occured.");
    int ch;
     
    StringBuffer buf = new StringBuffer();
     
     
    try {
    InputStream input = serialPort.getInputStream();
     
    while ( (ch=input.read()) > 0) {
     
    buf.append((char)ch);
     
    }
     
    System.out.print(buf);
     
    } catch (IOException e) {}

    break;
     
    }


    }跪求高手告诉我错哪了?