I am working on a project where I need to connect an electronic scale (T-Scale QTP 15MR) to my computer using a RS232-to-USB converter and read data from it using Java. I am certain about the COM port and the baud rate settings being correct. I have successfully opened the port using serialPort.openPort();. However, I'm not receiving any data from the scale. I'm wondering if I need to send any specific commands to the scale to initiate data transmission. Below is the code snippet I'm using:
public class Scale {
SerialPort serialPort;
String dataBuffer;
public Scale(String portName, int baudRate) {
try {
serialPort = SerialPort.getCommPort(portName);
serialPort.setBaudRate(baudRate);
serialPort.setNumDataBits(8);
serialPort.setNumStopBits(SerialPort.ONE_STOP_BIT);
serialPort.setParity(SerialPort.NO_PARITY);
serialPort.openPort();
if (serialPort.isOpen()) {
System.out.println("Port is opened");
serialPort.addDataListener(new SerialPortDataListener() {
@Override
public int getListeningEvents() {
return SerialPort.LISTENING_EVENT_DATA_RECEIVED;
}
@Override
public void serialEvent(SerialPortEvent serialPortEvent) {
byte[] newData = serialPortEvent.getReceivedData();
for (byte newDatum : newData) {
dataBuffer += (char) newDatum;
System.out.println(dataBuffer);
}
}
});
} else System.out.println("Port Open Failure");
} catch (Exception b) {
b.printStackTrace();
}
}
}
Then I call
new Scale("COM4", 9600);
My questions are:
- Do electronic scales typically require specific commands to start sending data?
- Is there anything obviously missing or incorrect in my implementation that could be preventing data reception? Any insights or suggestions would be greatly appreciated.
Thank you!
I attempted to connect an electronic scale to my computer using a RS232 to USB converter. Using the jSerialComm library in Java, I established a connection with the correct COM port and configured the baud rate. I expected that upon opening the port and setting up a DataListener, I would receive data from the scale through this Serial connection. However, even though the port opened successfully without any errors, I am not receiving any data from the scale. I am uncertain if I need to send a specific command to initiate the data transmission from the scale. I am seeking assistance from the community to better understand this issue and find a resolution.
UPDATE
"I've made further progress by using PuTTY to send data to the scale, and I successfully received responses. Specifically, when I pressed the 's' and 'w' keys on the PuTTY terminal, the scale returned the expected results, as shown in the attached image. This indicates that the scale does respond to specific commands. enter image description here However, when I try to replicate this in my Java application using the following code:
private void sendSignal(String s) {
OutputStream os = serialPort.getOutputStream();
try {
os.write(s.getBytes());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
And then calling this.sendSignal("sw"). I receive a null response instead of the expected data from the scale. It seems like there is a discrepancy in how the data is being sent or processed between PuTTY and my application. I would appreciate any insights or suggestions on how to troubleshoot this issue and successfully send commands from my Java application, similar to how it works in PuTTY."