# Trying to communicate to Arduino with jSerialComm and Kotlin

**URL:** <https://forum.arduino.cc/t/trying-to-communicate-to-arduino-with-jserialcomm-and-kotlin/913482>\
**Category:** Interfacing w/ Software on the Computer\
**Created:** [October 10, 2021, 12:18pm UTC](https://forum.arduino.cc/t/trying-to-communicate-to-arduino-with-jserialcomm-and-kotlin/913482 "2021-10-10T12:18:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![chris\_pi](https://avatars.discourse-cdn.com/v4/letter/c/57b2e6/32.png) [@chris\_pi](https://forum.arduino.cc/u/chris_pi)\
**Post date:** [October 10, 2021, 12:18pm UTC](https://forum.arduino.cc/t/trying-to-communicate-to-arduino-with-jserialcomm-and-kotlin/913482/1 "2021-10-10T12:18:50Z")

</div>

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:

> **[Tutorial: Serial Connection between Java Application and Arduino Uno -...](https://mschoeffler.com/2017/12/29/tutorial-serial-connection-between-java-application-and-arduino-uno/)**
>
> In this tutorial, I demonstrate how to build up a serial connection between a Java Application and an Arduino Uno. The tutorial is divided into two parts: In the first part, it is explained how to send text (digits) from a Java Application to an...

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:

```cpp
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:

```cpp
#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:

```cpp
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. 🙂

---

<div class="post-metadata">

**Author:** ![DrDiettrich](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drdiettrich/32/219048_2.png) [@DrDiettrich](https://forum.arduino.cc/u/DrDiettrich)\
**Post date:** [October 10, 2021, 1:51pm UTC](https://forum.arduino.cc/t/trying-to-communicate-to-arduino-with-jserialcomm-and-kotlin/913482/2 "2021-10-10T13:51:52Z")

</div>

> [@chris\_pi](#):
>
> ```arduino
> serialPorts.forEach { item -> println(item.descriptivePortName) }
> 
> ```

Did you see the list of names?

---

<div class="post-metadata">

**Author:** ![chris\_pi](https://avatars.discourse-cdn.com/v4/letter/c/57b2e6/32.png) [@chris\_pi](https://forum.arduino.cc/u/chris_pi)\
**Post date:** [October 10, 2021, 4:16pm UTC](https://forum.arduino.cc/t/trying-to-communicate-to-arduino-with-jserialcomm-and-kotlin/913482/3 "2021-10-10T16:16:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![DrDiettrich](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drdiettrich/32/219048_2.png) [@DrDiettrich](https://forum.arduino.cc/u/DrDiettrich)\
**Post date:** [October 10, 2021, 8:28pm UTC](https://forum.arduino.cc/t/trying-to-communicate-to-arduino-with-jserialcomm-and-kotlin/913482/4 "2021-10-10T20:28:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [October 11, 2021, 3:22am UTC](https://forum.arduino.cc/t/trying-to-communicate-to-arduino-with-jserialcomm-and-kotlin/913482/5 "2021-10-11T03:22:45Z")

</div>

> [@chris\_pi](#):
>
> I can't run the Serial Monitor and my Programm at once which is a little bit annoying.

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

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 9, 2022, 3:23am UTC](https://forum.arduino.cc/t/trying-to-communicate-to-arduino-with-jserialcomm-and-kotlin/913482/6 "2022-04-09T03:23:38Z")

</div>

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