1

Im trying to make connection between my computer and arduino uno using Java Simple Serial Connector. Im trying to do it using code listed below. Somehow its not working ( the led diode connected to pin 7 of arduino is not turning on while running my programm, but when im using serial monitor of artuino software it does. ). Does anyone know why?

Java project code :

    import jssc.SerialPort;
import jssc.SerialPortException;

public class Main {

    public static void main(String[] args) {
        //In the constructor pass the name of the port with which we work
        SerialPort serialPort = new SerialPort("COM3");
        try {
            //Open port
            serialPort.openPort();
            //We expose the settings. You can also use this line - serialPort.setParams(9600, 8, 1, 0);
            serialPort.setParams(SerialPort.BAUDRATE_9600,
                                 SerialPort.DATABITS_8,
                                 SerialPort.STOPBITS_1,
                                 SerialPort.PARITY_NONE);
            //Writes data to port
            serialPort.writeBytes("Test".getBytes());
            //Closing the port
            serialPort.closePort();
        }
        catch (SerialPortException ex) {
            System.out.println(ex);
        }
    }
}`

Arduino code:

void setup() {
  Serial.begin(9600); //Ustawienie prędkości transmisji
  pinMode(7, OUTPUT);
  digitalWrite(7, LOW);
}

void loop() {
  if( Serial.available() > 0){
    digitalWrite(7, HIGH);

  }

}
HTF
  • 33
  • 1
  • 8
  • I would wait some after sending the bytes before closing the port, try a Thread.Sleep between writeBytes and closePort – Petter Friberg Oct 08 '15 at 15:59
  • Ok guys. to make this shi t work you have to put Thread.sleep between serialPort.setParams and serialPortwriteBytes :) Thanks for answer Petter Friberg – HTF Oct 08 '15 at 18:21
  • my pleasure if you like some more controll checkout java.nio, write your answer (solution), check solved, so that others may benefit. Have fun – Petter Friberg Oct 09 '15 at 07:19

1 Answers1

0

I think, your Arduinocode is wrong.

I do it like this.

https://www.arduino.cc/en/Serial/Write

Serial.write(val)

Serial.write(str) Serial.write(buf, len)

val: a value to send as a single byte str: a string to send as a series of bytes buf: an array to send as a series of bytes len: the length of the buffer

  • Links to potential solutions are always welcome, but please add some details for future visitors in case the link is no longer available. – Nikolay Mihaylov Aug 10 '16 at 11:14