如何在Java应用程序中获取电话号码?

如何在Java应用程序中获取电话号码?,java,serial-port,caller-id,Java,Serial Port,Caller Id,我正在开发一个Java应用程序来获取电话号码。我使用的是USRobotics 5639调制解调器(支持来电显示),我的电话公司提供来电显示服务。当我使用超级终端时,我会得到电话号码、时间和日期: 但当我尝试在我的应用程序中执行相同操作时,我只得到以下信息: ATZ 好的 AT+VCID=1 好的 这就是我目前拥有的: import gnu.io.CommPortIdentifier; import gnu.io.PortInUseException; import gnu.io.SerialPo

我正在开发一个Java应用程序来获取电话号码。我使用的是USRobotics 5639调制解调器(支持来电显示),我的电话公司提供来电显示服务。当我使用超级终端时,我会得到电话号码、时间和日期:

但当我尝试在我的应用程序中执行相同操作时,我只得到以下信息:
ATZ
好的
AT+VCID=1
好的
这就是我目前拥有的:

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();
        }
    }
}
while(!read)
循环中,您可以将其更改为
while(true)
循环,因为您希望在初始传输后继续接收数据。实际数据提取必须由您自己完成,特别是在
NMBR=
标记之后。为了便于复制粘贴,我还清理了您的一些代码:


输入gnu.io.CommPortIdentifier;
导入gnu.io.PortinUseeException;
导入gnu.io.SerialPort;
导入gnu.io.UnsupportedComOperationException;
导入java.io.BufferedInputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.OutputStream;
导入java.util.Enumeration;
公共类Rxtx
{
静态通信识别器端口;
静态通信标识符saveportId;
静态枚举端口列表;
静态输入流输入流;
静态输出流输出流;
静态BufferedInputStream BufferedInputStream;
静态串口串口;
公共静态void main(字符串[]args)
{
字符串端口=”;
portList=CommPortIdentifier.getPortIdentifiers();
字符串反馈=”;
字符串数据=”;
while(portList.hasMoreElements())
{
portId=(CommPortIdentifier)portList.nextElement();
if(portId.getPortType()==CommPortIdentifier.PORT_串行)
{
if(portId.getName().equals(“COM5”))
{
port=portId.getName();
break;//将循环中断到下一节
}
}
}
//Cast串行端口
尝试
{
serialPort=(serialPort)portId.open(“Pruebas”,2000年);
}
捕获(PortinUseeException ex)
{
例如printStackTrace();
}
//设置输出流
尝试
{
outputStream=serialPort.getOutputStream();
}
捕获(IOEX异常)
{
例如printStackTrace();
}
//将参数设置为serialPort
尝试
{
serialPort.setSerialPortParams(115200,serialPort.DATABITS_8,serialPort.STOPBITS_1,serialPort.PARITY_NONE);
}
捕获(不受支持的MMOperationException ex)
{
例如printStackTrace();
}
尝试
{
inputStream=serialPort.getInputStream();
bufferedInputStream=新的bufferedInputStream(inputStream);
}
捕获(IOE异常)
{
e、 printStackTrace();
}
serialPort.notifyOnDataAvailable(true);
尝试
{
如果(!(outputStream==null))
{
字符串serialMessage=“ATZ\r\n”;//软重置
write(serialMessage.getBytes());
字符串comando=“AT+VCID=1\r\n”;//调用方Id格式
write(comando.getBytes());
字节[]读缓冲区=新字节[1024];
while(true)//持续检查数据。
{
字符串getInfo=“”;
睡眠(100);
while(bufferedInputStream.available()>0)
{
int numBytes=bufferedInputStream.read(readBuffer);
getInfo+=新字符串(readBuffer);
数据=数据+新字符串(readBuffer,0,numBytes);
data=data.trim();
}
反馈+=获取信息;
int length=getInfo.length();
System.out.println(“数据:+数据);
}
}
}
捕获(IOE异常)
{
e、 printStackTrace();
}
}
}
while(!read)
循环中,您可以将其更改为
while(true)
循环,因为您希望在初始传输后继续接收数据。实际数据提取必须由您自己完成,特别是在
NMBR=
标记之后。为了便于复制粘贴,我还清理了您的一些代码:


输入gnu.io.CommPortIdentifier;
导入gnu.io.PortinUseeException;
导入gnu.io.SerialPort;
导入gnu.io.UnsupportedComOperationException;
导入java.io.BufferedInputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.OutputStream;
导入java.util.Enumeration;
公共类Rxtx
{
静态通信识别器端口;
静态通信标识符saveportId;
静态枚举端口列表;
静态输入流输入流;
静态输出流输出流;
静态BufferedInputStream BufferedInputStream;
静态串口串口;
公共静态void main(字符串[]args)
{
字符串端口=”;
portList=CommPortIdentifier.getPortIdentifiers();
字符串反馈=”;
字符串数据=”;
while(portList.hasMoreElements())
{
portId=(CommPortIdentifier)portList.nextElement();
if(portId.getPortType()==CommPortIdentifier.PORT_串行)
{
if(portId.getName().equals(“COM5”))
{
port=portId.getName();
break;//将循环中断到下一节
}
}
}
//Cast串行端口
尝试
{
serialPort=(serialPort)portId.open(“Pruebas”,2000年);
}