Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
996 views
in Technique[技术] by (71.8m points)

serial port - How to use jssc in Kotlin?

I'm developing a program to measure weight by connecting a PC with an electronic scale. I'm developing a language with Kotlin, and I'm trying to refer to Java's library, JSSC.

but I don't know what to do because I get a reference error every time. The development tool is IntelliJ from JetBrain.

import jssc.SerialPortList

object Main {
    @JvmStatic
    fun main(args: Array<String>) {
        val portNames = SerialPortList.getPortNames()
        for (i in portNames.indices) {
            println(portNames[i])
        }
    }
}

The top one is a kortlin and the bottom one is a java. I used the translator of the development tool.

import jssc.SerialPortList;

public class Main {

    public static void main(String[] args) {

            String[] portNames = SerialPortList.getPortNames();
            for(int i = 0; i < portNames.length; i++){
                System.out.println(portNames[i]);
        }
    }
}

Java executed, but Kotlin was unable to compile due to errors such as

Unresolved reference:jssc 
Unresolved reference: SerialPortList

I'm not sure how to avoid these reference errors, so I'm asking for advice. Please give me a good answer.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It looks like you want to use java-native/jssc library. The error Unresolved reference is a compilation error. Your code does not have access to the class jssc.SerialPortList. You should follow the instructions on the Github page and add the dependency in your maven or gradle config, or manually add the jar dependency into your Intellij Idea.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...