I want to read data from the scale and print it on the label with JavaFX. I made all the settings according to the documentation, but I cannot read the data. comPort.readBytes() always returns 0. Does anyone have any ideas about this? I am adding the document as a URL. I am working with cb string RS232. https://www.manualslib.com/manual/2121484/Bilanciai-D70e.html?page=51#manual
package com.togisoft.venetacollaudipeso;
import com.fazecast.jSerialComm.SerialPort;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperPrintManager;
import javax.print.PrintServiceLookup;
import javax.print.PrintService;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class AppController {
private SerialPort comPort;
@FXML
private Label weightLabel;
@FXML
public void initialize() {
// COM Portunu bul ver aç
comPort = SerialPort.getCommPort(Helper.getComPort());
comPort.setNumDataBits(7);
comPort.setBaudRate(Helper.getBaudRate());
comPort.setParity(Helper.getComParity());
comPort.setNumStopBits(Helper.getStopBit());
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
if (comPort.openPort()) {
System.out.println("Port başarıyla açıldı.");
executor.scheduleAtFixedRate(this::readFromSerial, 0, 300, TimeUnit.MILLISECONDS);
} else {
System.out.println("Port açılırken hata oluştu!.");
Platform.runLater(() -> weightLabel.setText("PORT Errore!"));
}
}
private void readFromSerial() {
while (comPort.isOpen()) {
byte[] readBuffer = new byte[8];
int numRead = comPort.readBytes(readBuffer, readBuffer.length);
if (numRead > 0) {
StringBuilder receivedData = new StringBuilder();
for (int i = 0; i < numRead; i++) {
// Son biti (en anlamsız biti) düşürerek 7 bitlik veriyi elde et
int sevenBitData = readBuffer[i] & 0x7F;
receivedData.append((char) sevenBitData); // Veriyi String olarak birleştir
}
String weight = extractWeightFromData(receivedData.toString());
updateUIWithWeight(formatWeight(weight));
}
}
}
private String formatWeight(String weightData) {
if (weightData == null || !weightData.contains(".")) {
return "0.0";
}
int significantDigits = 0;
for (char c : weightData.toCharArray()) {
if (Character.isDigit(c)) {
significantDigits++;
}
}
if (significantDigits >= 5) {
try {
float weight = Float.parseFloat(weightData);
if (weight == (int) weight) {
// Ondalık kısmı sıfır ise, ondalık kısmı gösterme
return String.format("%.0f", weight);
} else {
// Ondalık kısmı sıfırdan farklı ise, ondalık kısmı göster
return String.format("%.1f", weight);
}
} catch (NumberFormatException e) {
System.out.println(e.getLocalizedMessage());// Hata durumunda işlemler
}
}
return "0.0"; // Varsayılan olarak 0 döndür
}
private String extractWeightFromData(String data) {
if (data.length() >= 8 && data.startsWith("$(24H")) {
return data.substring(2, 7); // 3–7 karakterleri alarak ağırlık bilgisini çıkar
}
return "";
}
private void updateUIWithWeight(String weight) {
Platform.runLater(() -> weightLabel.setText(weight));
}
@FXML
void onStamp() {
System.out.println(weightLabel.getText());
try {
String sourceFileName = "weight.jasper";
Map<String, Object> parameters = new HashMap<>();
parameters.put("kilo", weightLabel.getText() + " KG"); // Örnek parametre
JasperPrint print = JasperFillManager.fillReport(sourceFileName, parameters, new JREmptyDataSource());
PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
PrintService selectedPrintService = null;
for (PrintService printService : printServices) {
if (printService.getName().equalsIgnoreCase(Helper.getPrinter())) {
selectedPrintService = printService;
break;
}
}
if (selectedPrintService != null) {
JasperPrintManager.printReport(print, false);
} else {
System.out.println("Belirtilen yazıcı bulunamadı.");
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
@FXML
public void onEnterPressed(KeyEvent event) {
if (event.getCode() == KeyCode.ENTER) {
System.out.println("Enter pressed");
onStamp();
}
}
@Override
protected void finalize() {
if (comPort != null && comPort.isOpen()) {
comPort.closePort();
System.out.println("Port closed.");
}
}
}