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

时间:2016-12-01 04:45:25

标签: arduino jssc

我的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库

1 个答案:

答案 0 :(得分:0)

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代码的延迟减少到200ms。

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

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