Java 通过RXTX和GSM调制解调器发送长信息

Java 通过RXTX和GSM调制解调器发送长信息,java,rxtx,Java,Rxtx,我的Java应用程序通过连接到COM端口的GSM调制解调器发送SMS。对于COM端口通信,我使用RXTX。除了发送长(连接)短信外,一切正常 端口的代码: private final String comPortID; private SerialPort serialPort; private OutputStreamWriter out; private InputStreamReader in; public Port(String comPort

我的Java应用程序通过连接到COM端口的GSM调制解调器发送SMS。对于COM端口通信,我使用RXTX。除了发送长(连接)短信外,一切正常

端口的代码:

private final String        comPortID;

private SerialPort          serialPort;
private OutputStreamWriter  out;
private InputStreamReader   in;

public Port(String comPortID)
{
    this.comPortID = comPortID;
}

public void open() throws Exception
{
    CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(comPortID);
    serialPort = (SerialPort) portId.open("SMS Transceiver", 100);
    serialPort.setSerialPortParams(19200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
    out = new OutputStreamWriter(serialPort.getOutputStream(), "ISO-8859-1");
    in = new InputStreamReader(serialPort.getInputStream(), "ISO-8859-1");
    System.out.println("Port " + comPortID + " opened successfully");
}

public void write(String s) throws Exception
{
    out.write(s);
    out.flush();
    System.out.println("Write to port: " + s);
}

public void write(char[] s) throws Exception
{
    out.write(s);
    out.flush();
    System.out.println("Write to port: " + new String(s));
}

public void writeln(String s) throws Exception
{
    out.write(s);
    out.write('\r');
    out.flush();
    System.out.println("Write to port: " + s);
}

public String read() throws Exception
{
    final long timeout = System.currentTimeMillis() + 3000;
    do
    {
        Thread.sleep(100);
    } while (!in.ready() && System.currentTimeMillis() < timeout);

    final StringBuilder answer = new StringBuilder();
    for (int i = 0; i < 10; i++)
    {
        while (in.ready())
        {
            int n = in.read();
            if (n != -1)
            {
                char c = (char) n;
                answer.append(c);
                Thread.sleep(1);
            }
            else
            {
                break;
            }
        }
        Thread.sleep(100);
    }
    System.out.println("Read from port: " + answer);
    return answer.toString();
}

public void close()
{
    try
    {
        serialPort.close();
        System.out.println("Port closed");
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}
private final String comPortID;
专用串行端口串行端口;
私人输出;
私有输入流读卡器;
公共端口(字符串组件ID)
{
this.comPortID=comPortID;
}
public void open()引发异常
{
CommPortIdentifier portId=CommPortIdentifier.getPortIdentifier(comPortID);
serialPort=(serialPort)portId.open(“SMS收发器”,100);
serialPort.setSerialPortParams(19200,serialPort.DATABITS_8,serialPort.STOPBITS_1,serialPort.PARITY_NONE);
out=新的OutputStreamWriter(serialPort.getOutputStream(),“ISO-8859-1”);
in=新的InputStreamReader(serialPort.getInputStream(),“ISO-8859-1”);
System.out.println(“端口”+comPortID+“已成功打开”);
}
public void write(字符串s)引发异常
{
写出。写出(s);
out.flush();
System.out.println(“写入端口:+s”);
}
public void write(char[]s)引发异常
{
写出。写出(s);
out.flush();
System.out.println(“写入端口:+新字符串”);
}
public void writeln(字符串s)引发异常
{
写出。写出(s);
out.write('\r');
out.flush();
System.out.println(“写入端口:+s”);
}
公共字符串read()引发异常
{
最终长超时=System.currentTimeMillis()+3000;
做
{
睡眠(100);
}而(!in.ready()&&System.currentTimeMillis()
发送长短信的代码:

        // Set message format to PDU mode  
        port.writeln("AT+CMGF=0");
        port.read();

        for (int i = 0; i < textParts.size(); i++)
        {
            System.out.println("\nSend SMS " + (i + 1) + " of " + textParts.size() + "\n");

            final byte[] pdu = PDUUtils.getPDUPart(dialno, textParts.get(i), DataCodingScheme.UCS2, i + 1, textParts.size());
            final char[] pduHex = PDUUtils.toHexString(pdu);

            port.writeln("AT+CMGS=" + pdu.length);
            port.read();

            port.write("00");
            port.write(pduHex);
            port.write("\u001A"); // Ctrl-Z
            port.read();
        }
//将消息格式设置为PDU模式
port.writeln(“AT+CMGF=0”);
port.read();
对于(int i=0;i
问题不在于PDU或AT命令的创建。当我用PUTTY连接到COM端口并向调制解调器发送完全相同的命令时,信息将被正确发送,并在手机上作为一条短信正确接收。因此,一定存在时间或配置问题

我没有收到任何错误消息或异常。read()方法接收的信息始终与我以前通过write()方法写入的字符串相同。除了最后一条消息的最后一次读取()(长短信分为两部分)。在那里我得到了不同的结果。通常返回字符串“AA”或“AAA”,有时返回我通过write()方法编写的字符串。在所有情况下,我的手机都不会收到短信

发送短信息没有一般问题。当我发送两条未连接的短信(仍处于PDU模式)时,一切正常

有人能帮我吗