arduinojssc

Unable to write data to Arduino using jssc 2.8.0


My Java code is as follows:

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);
      }
   }
}

I'm able to read from Arduino. But, I'm unable to write anything to Arduino. It'd be really great if someone helps me out with this.

My Arduino code:

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

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

I downloaded the jssc library from here


Solution

  • 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);
          }
       }
    }
    

    All I needed to do was add a sleep of 2000ms after opening the serial port, and then add a 200ms sleep after writing to Arduino. Also, the delay in the Arduino code was reduced to 200ms.

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