卑鄙外乡人 2022-11-21 19:11 采纳率: 0%
浏览 5
已结题

Java串口发送at指令无返回

问题遇到的现象和发生背景

Java调用rxtx串口发送at指令集无返回,有遇到过这种情况吗?发送部分代码如下

import gnu.io.*;
import java.util.*;
import java.io.*;

public class SmsMain
{
    static CommPortIdentifier portId;
    static Enumeration portList;
    static int bauds[] = {19200};    //检测端口所支持的波特率

    public static void main(String[] args)
    {
        portList = CommPortIdentifier.getPortIdentifiers();
        System.out.println("短信设备端口连接测试...");
        while (portList.hasMoreElements())
        {
            portId = (CommPortIdentifier) portList.nextElement();
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
            {
                System.out.println("找到串口: " + portId.getName());
                for (int i = 0; i < bauds.length; i++)
                {
                    System.out.print("  Trying at " + bauds[i] + "...");
                    try
                    {
                        SerialPort serialPort;
                        InputStream inStream;
                        OutputStream outStream;
                        int c;
                        String response;
                        serialPort = (SerialPort) portId.open("SMSLibCommTester", 1971);
                        serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_OUT);
                        serialPort.setSerialPortParams(bauds[i], SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
                        inStream = serialPort.getInputStream();
                        outStream = serialPort.getOutputStream();
                        serialPort.enableReceiveTimeout(1000);
                        c = inStream.read();
                        while (c != -1)
                            c = inStream.read();
                        String s = "AT\r\n";
                        byte[] bytes = s.getBytes();
                        outStream.write(bytes);
                       /* outStream.write('A');
                        outStream.write('T');
                        outStream.write('\r');
                        outStream.write('\n');*/
                        try
                        {
                            Thread.sleep(1000);
                        }
                        catch (Exception e)
                        {
                        }
                        response = "";
                        c = inStream.read();
                        while (c != -1)
                        {
                            response += (char) c;
                            c = inStream.read();
                        }
                        if (response.indexOf("OK") >= 0)
                        {
                            try
                            {
                                System.out.print("  获取设备信息...");
                                outStream.write('A');
                                outStream.write('T');
                                outStream.write('+');
                                outStream.write('C');
                                outStream.write('G');
                                outStream.write('M');
                                outStream.write('M');
                                outStream.write('\r');
                                response = "";
                                c = inStream.read();
                                while (c != -1)
                                {
                                    response += (char) c;
                                    c = inStream.read();
                                }
                                System.out.println("  发现设备: " + response.replaceAll("\\s+OK\\s+", "").replaceAll("\n", "").replaceAll("\r", ""));
                            }
                            catch (Exception e)
                            {
                                System.out.println("  没有发现设备!");
                            }
                        }
                        else System.out.println("  没有发现设备!");
                        serialPort.close();
                    }
                    catch (Exception e)
                    {
                        System.out.println("  没有发现设备!");
                    }
                }
            }
        }
    }
}


用代码块功能插入代码,请勿粘贴截图

img

  • 写回答

3条回答 默认 最新

  • Jackyin0720 2022-11-21 19:42
    关注

    这是一个分析和讲解详细的实例,返回为null时,用2种不同的思路进行修正代码。并且,代码完整,可执行。可以借鉴其中的思路。
    【Java串口通信 RXTX 解决过程】,链接:https://www.bbsmax.com/A/nAJv0eqmdr/

    评论

报告相同问题?

问题事件

  • 系统已结题 11月29日
  • 创建了问题 11月21日