Java JSerialComm写不写?

问题描述

我有一个Java应用程序,它使用通过POM.xml文件导入的JSerialComm。我的连接代码如下:

public void connectPort(int index) {
    System.out.println("Attempting to connect to Index:" + index);
    int baudrate = 115200;
            
    SerialPort comPort = comPortList[index];
    currentConnectedPort = comPort;
    comPort.openPort();
    comPort.setParity(SerialPort.NO_PARITY);
    comPort.setNumStopBits(SerialPort.ONE_STOP_BIT);
    comPort.setNumDataBits(8);
    comPort.addDataListener(this);
    comPort.setBaudrate(baudrate);
            
    System.out.println("Connection successful!");
    updateStatus("Connected!");
}

这似乎起作用,因为它每5秒从连接到的设备触发一次我的侦听器。我可以读取设备输出的数据,但是我一辈子都无法向设备写入内容
在PuttY中,我可以简单地键入“ Boxtest start 0 -3000”,然后按Enter键,事情开始进行Boxtest的过程,但是在我的Java应用程序上,当我写入串行端口时,什么也没发生,机器完全不响应,并且我不知道我是否想念什么。

这是我的写代码

public void writetoPort(String cmd) {
    if(cmd.equals("about")) {
        String thisversion;
        thisversion = String.format("Current version is %s",currentVersion);
        updateStatus(thisversion);
    } else if(!cmd.isEmpty()){
        System.out.printf("Command received: ' %s ',Writing to Serial Port",cmd);
    parseData(cmd);
    //Convert the string to a byte array
    byte b[] = cmd.getBytes();
        currentConnectedPort.writeBytes(b,b.length);
        System.out.println("\n ********** ");
        System.out.println("Lenght is: " + b.length);
        System.out.println("Wrote: " + currentConnectedPort.writeBytes(cmd.getBytes(),cmd.length()) + " bytes");
        System.out.println(" ********** \n");
    updateStatus("Command Sent");
}    

}

从我读到的writeBytes返回它写入的字节数量的int,我的输出确认它在技术上写入了我请求的21个字节,但是接收设备没有执行任何操作。 (使用PuttY,不使用Java)。

输出如下:

Command received: ' Boxtest start 0 -3000 ',Writing to Serial PortParsing Input: Boxtest start 0 -3000 
 logging is true 
received values string

 ********** 
Lenght is: 21
Wrote: 21 bytes
 ********** 

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)