java - JSerialComm 无法读取或关闭 Mac 上的端口

标签 java macos

我有一个微 Controller ,我想要接收和发送数据,所以我尝试使用 UART 端口来看看这是否可行。我为我的微 Controller 编写了一个快速程序,该程序会在 LCD 屏幕上显示 char 值(不是字符本身,而是 ASCII 代码),当我按下按钮时,它会发回 76(“L”的代码,因为这是这个项目给了我所有的东西)。然后我下载了适用于我的 Mac 的 CoolTerm(我使用 Mac 是因为这是我学校给我的,而不是我选择的)并插入 this我买了 USB 转 UART 电缆。下载驱动程序后,我启动了 CoolTerm,选择了端口,并选择了波特率。我点击连接并开始按键。当我这样做时,我在 LCD 上得到了正确的相应 ASCII 值,当我按下微 Controller 上的按钮时,我在终端中收到了一个“L”。一切都很完美。然后我下载了 jserialcomm 并制作了一个小程序来看看是否可以读取来自微 Controller 的值。当我启动该程序时,即使我按下按钮或按住它,它也只会偶尔读取一个字节。它读取的字节只是 1。然后另一个问题发生了。看来港口没有正常关闭。程序终止后,我尝试再次运行它,它会卡在尝试打开端口上,并且此后永远不会执行任何代码。当我要终止该程序时,它说它“无法终止”,然后它(我认为是)强制退出该程序。当我返回 CoolTerm 打开端口时,它卡在连接上并卡住,我必须强制退出。需要明确的是,我使用的是 cu.*,如常见问题解答所述,而不是 tty.*

这是我正在使用的 Java 代码:

 //This is what I use to set up the the Port 
    //This gets all of the ports on the machine
    SerialPort[] q;
    q = SerialPort.getCommPorts();

    //This iterates through the ports and gives a description and the name of the port
    for(SerialPort a: q){
        System.out.println(a.getDescriptivePortName() + " : " + a.getSystemPortName());
    }

    //Allows user to select which port they want
    System.out.println("Which port do you want?");
    Scanner s = new Scanner(System.in);
    int portnumber = s.nextInt();
    s.close();

    //Creates a SerialPort object of the port the user selected and opens it
    SerialPort commPort = q[portnumber];
    System.out.println(commPort.getDescriptivePortName());

    if(commPort.openPort()){
        System.out.println("Port Opened");
    }else{
        System.out.println("Port Failed to Open");
    }

    commPort.setBaudRate(230400);


//This is the first method I tried
commPort.setComPortTimeouts(SerialPort.TIMEOUT_NONBLOCKING, 1000, 0);
    try{
        while(true){
            //Waits till there are bytes available 
            while(commPort.bytesAvailable() == 0){
                Thread.sleep(20);
            }

            //Creates a buffer to read the bytes
            byte[] readBuffer = new byte[commPort.bytesAvailable()];
            int numOfBytes = commPort.readBytes(readBuffer, readBuffer.length);
            //Prints out the number of bytes read and the bytes it read
            System.out.println("Read " + numOfBytes + " bytes. Message:");
            for(byte b: readBuffer){
                System.out.println("::::" + Integer.toBinaryString(b & 0xFF));
            }

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

    }


The second method I did was using InputStreams which I would prefer not to use since I rather just read raw bytes in.

最佳答案

将超时编辑为:

commPort.setComPortTimeouts(SerialPort.TIMEOUT_NONBLOCKING, 0, 0);

或删除该行。在你的情况下你不需要它。为什么波特率奇怪?有时,USB 转 RS232 桥接器并不适用于所有波特率。

适用于 9600 波特率。

希望这有帮助,

哈尼族

关于java - JSerialComm 无法读取或关闭 Mac 上的端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41709444/

相关文章:

java - 如何在没有名称的提交按钮的情况下使用 Jsoup 提交表单?

java - 找不到类 org.apache.commons.dbcp.BasicDataSource

macos - NSWindow 的 orderOut/Back/Front 的 sender 参数的目的是什么?

swift - 在 swift ( OS X ) 中从十六进制字符串获取 CGColor

java - 设置类路径java错误

java - 膨胀异常小部件回收 View

java - 在 spring 中将 JSON 数组字段恢复为字节数组

java - 引用HTML文件中的servlet以获取Tomcat中的.war文件

java - 隐藏 JFrame 并通过单击停靠图标再次显示它

arrays - 从 NSComboBox 获取选定的文本值