0
package deviceintegration;
import jssc.SerialPort;
import jssc.SerialPortEvent;
import jssc.SerialPortEventListener;
import jssc.SerialPortException;
/**
 *
 *
 */
public class SerialScaleDevice implements SerialPortEventListener {
    private String m_sPortScale;
    private SerialPort m_CommSerialPort;
    private static final int SCALE_READY = 0;
    private String m_dWeight;
    private int m_iStatusScale;
    /**
     * Creates a new instance of SerialScaleDevice
     * 
     * @param sPortPrinter
     */
    public SerialScaleDevice(String sPortPrinter) {
        m_sPortScale = sPortPrinter;
        m_CommSerialPort = new SerialPort(m_sPortScale);
        //
        m_iStatusScale = SCALE_READY;
        m_dWeight = "";
    }
    /**
     *
     * @return
     */
    public String readWeight() {
        synchronized (this) {
            if (m_iStatusScale != SCALE_READY) {
                try {
                    wait(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (m_iStatusScale != SCALE_READY) {
                    m_iStatusScale = SCALE_READY;
                }
            }
            m_dWeight = "0.0";
            read();
            try {
                wait(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return m_dWeight;
        }
    }
    private void read() {
        try {           
            m_CommSerialPort.openPort(); 
            m_CommSerialPort.setEventsMask(SerialPort.MASK_RXCHAR);
            m_CommSerialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
            m_CommSerialPort.setParams(SerialPort.BAUDRATE_9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); 
            m_CommSerialPort.addEventListener(this);    
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * @param e
     */
    public void serialEvent(SerialPortEvent event) {
        // Determine type of event.
        switch (event.getEventType()) {
        case SerialPortEvent.BREAK:
        case SerialPortEvent.CTS:
        case SerialPortEvent.DSR:
        case SerialPortEvent.ERR:
        case SerialPortEvent.RING:
        case SerialPortEvent.RLSD:
        case SerialPortEvent.RXFLAG:
        case SerialPortEvent.TXEMPTY:
            break;
        case SerialPortEvent.RXCHAR:
            try {
                Thread.sleep(200);
                m_dWeight = new String (m_CommSerialPort.readBytes());
                System.out.println("readBytes: " + m_dWeight);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (SerialPortException e) {
                e.printStackTrace();
            }
            break;
        }
    }
}

Sample of what I get: enter image description here

001
  • 13,291
  • 5
  • 35
  • 66
  • What is your question? What is the expected output? – 001 Dec 01 '19 at 13:55
  • Side note: Hungarian notation is not used much these days. Back in olden times - before Intellisense - it was useful. But with modern IDEs it is much easier to get immediate info on a variable. – 001 Dec 01 '19 at 13:57
  • My question is how to convert this to readable data number or string ? and I expect to get a number from the scale machine that is connected. – Mustafa Samir Dec 01 '19 at 14:05
  • You mean by that It's not an ambiguous reading its just the conversion from byte array to string that isn't correct. – Mustafa Samir Dec 01 '19 at 14:08

1 Answers1

0

I don't know the protocol of the device you are using, but it appears to send 12 bytes per message. <STX><+-><9 digit hex><ETX>. So, read 12 bytes at a time, and convert the hex to decimal.

So, first change

m_dWeight = new String (m_CommSerialPort.readBytes());

to

m_dWeight = new String(m_CommSerialPort.readBytes(12));

Then, parse the message:

// ☻+00000001B♥
char stx = m_dWeight.charAt(0);
char etx = m_dWeight.charAt(11);
if (stx == 2 && etx == 3) {
    long actualValue = Long.parseLong(m_dWeight.substring(1, 11), 16);
    System.out.println(actualValue);
}
001
  • 13,291
  • 5
  • 35
  • 66
  • those other readings:☻+00014001E♥ ☻+000020019♥ ☻+000030018♥. If you look carefully you'll found that 140, 20 & 30 are the true reading, I know that by your hint and by try and error, but I still don't know what protocol the machine uses. – Mustafa Samir Dec 01 '19 at 15:46
  • @MustafaSamir Yes, without documentation, it is hard to say for sure. Googling the name brand and model number might help. Maybe it sends decimal values in the last bytes? So `00014001E` becomes 20.0030?? – 001 Dec 01 '19 at 15:56
  • For 00140001E becomes 140 after trim the last three chars you'll get the true decimal value. And I'll try to search for the model may I find something help. Thanks alot @Johnny Mopp – Mustafa Samir Dec 02 '19 at 09:45