Serial port connection balances using JSSC

0

I communicate with a scale via a serial port, and today I use the JSSC library, however the hour scale returns are of the weight and time are different characters, searching I came to the conclusion that it can some configuration information of the port that may not be legal, but I checked the settings that the scale marks suggest and did as requested but continues the same way.

Follow the code to extract the information.

import java.io.UnsupportedEncodingException;
import jssc.SerialPort;
import jssc.SerialPortException;
import jssc.SerialPortList;

public class Balanca {

public static void main(String[] args) throws UnsupportedEncodingException {
    String[] portNames = SerialPortList.getPortNames();
    for (String portName : portNames) {
        System.out.println(portName);
    }

    SerialPort serialPort = new SerialPort("COM1");
    try {
        System.out.println("Port opened: " + serialPort.openPort());
        System.out.println("Params setted: " + serialPort.setParams(4800, 8, 1, 0));
        System.out.println("successfully writen to port: " + serialPort.writeBytes(new byte[]{0x04}));
        byte[] buffer = serialPort.readBytes(46);//Read 10 bytes from serial port
        System.out.println(new String(buffer));
        System.out.println("Port closed: " + serialPort.closePort());
    } catch (SerialPortException ex) {
        System.out.println(ex);
    }

}
}

And the output I have is as follows.

When I get character return

COM1
Port opened: true
Params setted: true
successfully writen to port: true
 &��&ӐSӓӐSӓӐSӓӐSӓӐSӓӐS
Port closed: true

When I get data back in numbers

COM1
Port opened: true
Params setted: true
successfully writen to port: true
60013600136001360013600136001360
Port closed: true

The communication I used draws from the same answer from here.

RXTX: port connection serial balance

    
asked by anonymous 24.06.2016 / 16:30

1 answer

0

You've already tried modifying this line of your code:

System.out.println("Params setted: " + serialPort.setParams(4800, 8, 1, 0));

To:

System.out.println("Params setted: " + serialPort.setParams(9600, 8, 1, 0));

I do not really know the difference but for Arduino we use 9600 as the data rate in bits per second (baud) for serial data transmission.

See these links:

  • link
  • link
  • 02.05.2017 / 15:05