无法使用 jssc 2.8.0 将数据写入 Arduino

Unable to write data to Arduino using jssc 2.8.0

我的Java代码如下:

import jssc.SerialPort;
import jssc.SerialPortException;
import jssc.SerialPortList;
public class SerialPort1{
 public static boolean sentBytes;
    //public SerialPort serialPort;

    public static void main(String[] args) {
      // get computer serial ports names
      String[] portNames = SerialPortList.getPortNames();
      for (String port : portNames) {
         System.out.println(port);
      }


      // inicialization with selecting port for communication
      SerialPort serialPort = new SerialPort("/dev/ttyACM0");

      try {
         // open port for communication
         serialPort.openPort();
         serialPort.setParams(SerialPort.BAUDRATE_9600,
                 SerialPort.DATABITS_8,
                 SerialPort.STOPBITS_1,
                 SerialPort.PARITY_NONE,false,true);
         serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
         // byte data transfer
         try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        serialPort.writeInt(1); 
         try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

         System.out.println(serialPort.readString());

         // close port
         serialPort.closePort();
      } catch (SerialPortException ex) {
         System.out.println(ex);
      }
   }
}

我可以从 Arduino 读取信息。但是,我无法给 Arduino 写任何东西。如果有人能帮我解决这个问题就太好了。

我的 Arduino 代码:

 void setup() {
    Serial.begin(9600);
    }

    void loop() {
    Serial.println("Android");
    delay(2000);
    }

我从 here

下载了 jssc 库
import jssc.SerialPort;
import jssc.SerialPortException;
import jssc.SerialPortList;
public class SerialPort1{
 public static boolean sentBytes;
    //public SerialPort serialPort;

    public static void main(String[] args) {
      // get computer serial ports names
      String[] portNames = SerialPortList.getPortNames();
      for (String port : portNames) {
         System.out.println(port);
      }


      // initialization with selecting port for communication
      SerialPort serialPort = new SerialPort("/dev/ttyACM0");

      try {
         // open port for communication
         serialPort.openPort();
         serialPort.setParams(SerialPort.BAUDRATE_9600,
                 SerialPort.DATABITS_8,
                 SerialPort.STOPBITS_1,
                 SerialPort.PARITY_NONE,false,true);
         serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
         // byte data transfer
         try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        serialPort.writeInt(1); 
         try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

         System.out.println(serialPort.readString());

         // close port
         serialPort.closePort();
      } catch (SerialPortException ex) {
         System.out.println(ex);
      }
   }
}

我需要做的就是在打开串口后增加2000ms的休眠,然后写入Arduino后增加200ms的休眠。 此外,Arduino 代码中的延迟减少到 200 毫秒。

void setup() {
    Serial.begin(9600);
 }

void loop() {
    Serial.println("Android");
    delay(200);
}