1

我正在开发一个 Java 应用程序来获取电话号码。我正在使用 USRobotics 5639 调制解调器(支持来电显示),并且我的电话公司确实提供来电显示服务。当我使用超级终端时,我得到了呼叫的号码、时间和日期:
https
://imgur.com/wwwRHa7但是当我尝试在我的应用程序中做同样的事情时,我只得到以下信息:
ATZ
OK
AT+VCID= 1
OK
这就是我目前所拥有的:

import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;

public class Rxtx
{
    static CommPortIdentifier portId;
    static CommPortIdentifier saveportId;
    static Enumeration  portList;
    static InputStream inputStream;
    static OutputStream outputStream;
    static BufferedInputStream bufferedInputStream;
    static SerialPort serialPort;


    public static void main(String[] args)
    {
        boolean gotPort = false;
        String port;
        portList = CommPortIdentifier.getPortIdentifiers();
        String feedback = null;
        String data = null;

        while(portList.hasMoreElements())
        {
            portId = (CommPortIdentifier) portList.nextElement();
            if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
            {
                if(portId.getName().equals("COM5"))
                {
                    port = portId.getName();
                    gotPort = true;
                }

                if(gotPort == true)
                {
                    try
                    {
                        serialPort = (SerialPort)portId.open("Pruebas", 2000);
                    }
                    catch(PortInUseException ex)
                    {
                        ex.printStackTrace();
                    }
                    try
                    {
                        outputStream = serialPort.getOutputStream();
                    }
                    catch(IOException ex)
                    {
                        ex.printStackTrace();
                    }
                    try
                    {
                        serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
                    }
                    catch(UnsupportedCommOperationException ex)
                    {
                        ex.printStackTrace();
                    }
                    try
                    {
                        inputStream = serialPort.getInputStream();
                        bufferedInputStream = new BufferedInputStream(inputStream);
                    }
                    catch(IOException e)
                    {
                        e.printStackTrace();
                    }
                    serialPort.notifyOnDataAvailable(true);
                }
            }
        }

       try
       {
            if(!(outputStream == null))
            {
                String serialMessage = "ATZ\r\n";
                OutputStream outstream = serialPort.getOutputStream();
                outstream.write(serialMessage.getBytes());
                String comando = "AT+VCID=1\r\n";
                OutputStream os = serialPort.getOutputStream();
                os.write(comando.getBytes());

                byte[] readBuffer = new byte[1024];
                boolean read = false;
                while(!read)
                {
                    try
                    {
                        String getInfo = "";
                        Thread.sleep(100);
                        while(bufferedInputStream.available() > 0)
                        {
                            int numBytes = bufferedInputStream.read(readBuffer);
                            getInfo += new String(readBuffer);
                            read = true;
                            data = data + new String(readBuffer, 0, numBytes);
                            data = data.trim();
                        }
                        feedback += getInfo;
                        int length = getInfo.length();
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                    finally
                    {
                        System.out.println("data: " + data);
                    }
                }

            }
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
    }
}

我缺少什么来获取主叫号码的所有信息?

4

1 回答 1

0

在您的while(!read)循环中,您可以将其更改为while(true)循环,因为您希望在初始传输后继续接收数据。实际的数据提取必须由您负责,特别是在NMBR=标签之后。我还清理了您的一些代码,以便于复制粘贴:


import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;

public class Rxtx
{
    static CommPortIdentifier portId;
    static CommPortIdentifier saveportId;
    static Enumeration  portList;
    static InputStream inputStream;
    static OutputStream outputStream;
    static BufferedInputStream bufferedInputStream;
    static SerialPort serialPort;


    public static void main(String[] args)
    {
        String port = "";
        portList = CommPortIdentifier.getPortIdentifiers();
        String feedback = "";
        String data = "";

        while(portList.hasMoreElements())
        {
            portId = (CommPortIdentifier) portList.nextElement();
            if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
            {
                if(portId.getName().equals("COM5"))
                {
                    port = portId.getName();
                    break; // Breaks the loop to next section
                }
            }

        }
        //Cast serialPort
        try
        {
            serialPort = (SerialPort) portId.open("Pruebas", 2000);
        }
        catch(PortInUseException ex)
        {
            ex.printStackTrace();
        }
        //Setup outputStream
        try
        {
            outputStream = serialPort.getOutputStream();
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }
        //set params to serialPort
        try
        {
            serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        }
        catch(UnsupportedCommOperationException ex)
        {
            ex.printStackTrace();
        }
        try
        {
            inputStream = serialPort.getInputStream();
            bufferedInputStream = new BufferedInputStream(inputStream);
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
        serialPort.notifyOnDataAvailable(true);

       try
       {
            if(!(outputStream == null))
            {
                String serialMessage = "ATZ\r\n"; //Soft reset
                outputStream.write(serialMessage.getBytes());
                String comando = "AT+VCID=1\r\n"; //Caller-Id formatting
                outputStream.write(comando.getBytes());

                byte[] readBuffer = new byte[1024];
                while(true) //Continuously check for data.
                {
                    String getInfo = "";
                    Thread.sleep(100);
                    while(bufferedInputStream.available() > 0)
                    {
                        int numBytes = bufferedInputStream.read(readBuffer);
                        getInfo += new String(readBuffer);
                        data = data + new String(readBuffer, 0, numBytes);
                        data = data.trim();
                    }
                    feedback += getInfo;
                    int length = getInfo.length();
                    System.out.println("data: " + data);
                }

            }
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
    }
}
于 2019-07-17T01:56:33.493 回答