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.