Trying to communicate to Arduino with jSerialComm and Kotlin

Hi there!
I'm quite new to the Arduino world and connected a LCD Display to my Arduino Uno R3. That works great when I write simple sketches using the Serial Monitor.
So, I want to interact with the Display over the "jSerialComm"-Java Library. As I want to connect the Arduino with my Webserver (which is written with Springboot and Kotlin) later on, I've tried to use Kotlin as Language instead of Java. That works fine and I see my Arduino as "USB-Based Serial Port". To verfiy that this is my Arduino, I just disconnected it and checked the list of available devices, so yeah, the Serial works so far.
Now heres my Problem:
I'm following this Guide:

The only thing I have done is that I port the Java code into Kotlin (I also adapted the LCD Pins in the Arduino sketch to match mine).
The Problem is, that I get an Error that the Device is mybe not connected or shutdown:

Exception in thread "main" com.fazecast.jSerialComm.SerialPortIOException: This port appears to have been shutdown or disconnected.
	at MainKt.main(Main.kt:41)

So Im using Ubuntu 21.04 with an Adruino UNO R3

As I said, the Connection to the Arduino over the Sketches works totally fine and it would be very unlikely that Kotlin is the problem, or could it be?
Here is my Sketch I'm using:

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2);
  Serial.println("Arduino ready!");
}

void loop() {
  if (Serial.available()) {
    byte incomingByte = 0;
    incomingByte = Serial.read();
    if (incomingByte != -1) {
      lcd.setCursor(0, 0);
      lcd.print("I received: ");
      lcd.setCursor(0, 1);
      lcd.print(incomingByte);
    }
  }
}

And here is my example code:

import com.fazecast.jSerialComm.SerialPort
import java.lang.Exception

fun main(args: Array<String>) {
    println("USB Serial Connection Arduino")

    val serialPorts: Array<SerialPort> = SerialPort.getCommPorts()
    if (serialPorts.isEmpty()) {
        println("No Serial ports found")
        return
    }

    println("Devices found:")
    serialPorts.forEach { item -> println(item.descriptivePortName) }

    try {
        val arduino = serialPorts[0]
        arduino.setComPortParameters(9600, 8, 1, 0)
        arduino.setComPortTimeouts(SerialPort.TIMEOUT_SCANNER, 0, 0)
        if (arduino.openPort()) {
            println("Connection established to \"${arduino.descriptivePortName}\"")
        } else {
            println("Connection failed")
        }

        for (item: Int in 0..5) {
            arduino.outputStream.write(item)
            arduino.outputStream.flush()
            println("Send Number to Arduino: $item")
            Thread.sleep(1000)
        }


        if (arduino.closePort()) {
            println("Connection closed")
        } else {
            println("Connection closing failed")
        }

    } catch (e: Exception) {
        throw e.fillInStackTrace()
    }
}

If you need some more information let me know, thank you. :slight_smile:

Did you see the list of names?

Yes and I managed it. The Problem was the Serial Monitor. I can't run the Serial Monitor and my Programm at once which is a little bit annoying.

Make your program pipe everything to the Serial Monitor, or add a monitor window to your program.

It's also very normal :wink: Only one application at a time can make use of a serial port.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.