简体   繁体   中英

How to read a string/character from com port with java?

I googled lot about reading from a com port but it seems very difficult work for me. I found this but I don't know how to add jSSC library to my netbeans project.

Please help me by giving instructions to adding that library to my project or giving a simple code to read a string/character from a com port.

To add jSSC library in netbeans, right click on "Libraries" in your project and then "add JAR/Folder..." to select your file(s)

在此处输入图片说明

You can read with something like this :

SerialPort serialPort = new SerialPort("/dev/ttyUSB0");// For windows "COMX" (e.g : "COM1") should works
if (serialPort.openPort()) {
    serialPort.setParams(SerialPort.BAUDRATE_9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_ODD);
    byte[] buffer = serialPort.readBytes(1);// Read one byte
    String str = new String(buffer);
    serialPort.closePort();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM