Fazecast / jSerialComm

Platform-independent serial port access for Java
GNU Lesser General Public License v3.0
1.34k stars 287 forks source link

This port appears to have been shutdown or disconnected. #550

Closed LMS5413 closed 8 months ago

LMS5413 commented 8 months ago

Hi! Im using Arduino Uno port /dev/ttyACM0, but when i write in Serial this error appears.

fun main(args: Array<String>) {
    val arduinoController = ArduinoController("/dev/ttyACM0", 9600)
    arduinoController.init()
    arduinoController.write(1)
}
import com.fazecast.jSerialComm.SerialPort
import java.io.OutputStream

class ArduinoController (private val port: String, private val taxTransfer: Int) {
    private lateinit var serialPort: SerialPort
    private lateinit var serialOut: OutputStream

    fun init() {
        serialPort = SerialPort.getCommPort(port)
        serialPort.openPort()
        serialPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 0, 0)
        serialOut = serialPort.outputStream
    }

    fun write(int: Int) {
        println("Writted $port")
        serialOut.write(int)
    }
}

Error:

lms5413@LM /m/n/C/A/b/libs [1]> java -jar ArduinoKt.jar
Writted /dev/ttyACM0
Exception in thread "main" com.fazecast.jSerialComm.SerialPortIOException: This port appears to have been shutdown or disconnected.
    at com.fazecast.jSerialComm.SerialPort$SerialPortOutputStream.write(SerialPort.java:2060)
    at ArduinoController.write(ArduinoController.kt:17)
    at MainKt.main(Main.kt:4)

In Arduino IDE, this port appears. image

LMS5413 commented 8 months ago

Solved! I executed this program using "sudo"