2016-05-25 18 views
0

arbeite an Anrufer-ID mit Java-Sprache. Ich habe ein USB-Robotermodem, und dieses Modem ist mit der Telefonleitung verbunden. Ich möchte die Anrufer-ID von diesem Modem erhalten, wenn das Telefon klingelt.Wie schreibe ich auf serielle Schnittstelle, um Anrufer-ID von Modem in Java zu bekommen?

Ich verwende RXTX-Bibliothek in meinem Beispielprojekt für Anrufer-ID, jetzt kommuniziert mit den seriellen Anschlüssen des Systems, bin erfolgreich lesen und schreiben Sie die Daten vom Port.

Wenn Telefon klingelt, Java-Programm geben Ausgang: ring, aber wenn ich Befehl übergeben für Anrufer-ID, dass die Zeit Ausgang zum Modem ist: ERROR

Unten ist der Beispielcode, bitte helfen Sie mir Anrufer-ID angezeigt werden soll.

Und mein Modem ist usb Roboter-Modem

package rxtx.demo; 

import gnu.io.CommPort; 
import gnu.io.CommPortIdentifier; 
import gnu.io.SerialPort; 

import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 

public class Command { 

    SerialPort serialPort; 

    public Command() { 
     super(); 
    } 

    void connect(String portName) throws Exception { 
     CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName); 
     if (portIdentifier.isCurrentlyOwned()) { 
      System.out.println("Error: Port is currently in use"); 
     } else { 
      System.out.println("Connect 1/2"); 
      CommPort commPort = portIdentifier.open(this.getClass().getName(), 6000); 

      if (commPort instanceof SerialPort) { 
       System.out.println("Connect 2/2"); 
       serialPort = (SerialPort) commPort; 
       System.out.println("BaudRate: " + serialPort.getBaudRate()); 
       System.out.println("DataBIts: " + serialPort.getDataBits()); 
       System.out.println("StopBits: " + serialPort.getStopBits()); 
       System.out.println("Parity: " + serialPort.getParity()); 
       System.out.println("FlowControl: " + serialPort.getFlowControlMode()); 
       serialPort.setSerialPortParams(4800, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_ODD); 
       //serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN); 
       System.out.println("BaudRate: " + serialPort.getBaudRate()); 
       System.out.println("DataBIts: " + serialPort.getDataBits()); 
       System.out.println("StopBits: " + serialPort.getStopBits()); 
       System.out.println("Parity: " + serialPort.getParity()); 
       System.out.println("FlowControl: " + serialPort.getFlowControlMode()); 
       InputStream in = serialPort.getInputStream(); 
       OutputStream out = serialPort.getOutputStream(); 

       (new Thread(new SerialReader(in))).start(); 
       (new Thread(new SerialWriter(out, in))).start(); 

       //out.write("AT&Zn?".getBytes()); 
       //out.flush(); 
      } else { 
       System.out.println("Error: Only serial ports are handled by this example."); 
      } 
     } 
    } 

    /** 
    * 
    */ 
    public static class SerialReader implements Runnable { 

     InputStream in; 

     public SerialReader(InputStream in) { 
      this.in = in; 
     } 

     public void run() { 
      byte[] buffer = new byte[1024]; 
      int len = -1; 
      try { 
       while ((len = this.in.read(buffer)) > -1) { 
        //System.out.println("Received a signal."); 
        System.out.print(new String(buffer, 0, len)); 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

    /** 
    * 
    */ 
    public static class SerialWriter implements Runnable { 

     OutputStream out; 
     InputStream in; 

     public SerialWriter(OutputStream out, InputStream in) { 
      this.out = out; 
      this.in = in; 
     } 

     public void run() { 
      try { 


       byte[] array = {0x1B, 0x50, 0x0D, 0x0A}; 
       while (true) { 
        //this.out.write("AT#CID=1".getBytes()); 
        this.out.write("AT+GCI=B5".getBytes()); 
        //this.out.write("AT+VCID=2".getBytes()); 
        this.out.write("AT+VCID=1".getBytes()); 

        this.out.write(new byte[]{0x1B, 0x50, 0x0D, 0x0A}); 
        this.out.flush(); 
        Thread.sleep(1000); 

        byte mBytesIn[] = new byte[1024]; 
        //this.in.read(mBytesIn); 
        this.in.read(mBytesIn); 
        String value = new String(mBytesIn); 
        System.out.println("Response from Serial Device: " + value); 
       } 
      } catch (IOException | InterruptedException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

    public static void main(String[] args) { 
     try { 
      (new Command()).connect("COM6"); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
} 

Antwort

0

Stellen Sie sicher, AT-Befehl senden, das Modem zu initialisieren und auch AT + VCID = 1 (oder dessen Äquivalent), um die Anrufer-ID-Funktion zu aktivieren.

Es ist auch nützlich, das Vorhandensein eines solchen Dienstes bei Ihrer Telefongesellschaft zu bestätigen.