簡體   English   中英

RS232 - JSSC 2.8.0串口閱讀器 - 歧義閱讀

[英]RS232 - JSSC 2.8.0 Serial Port Reader - Ambiguous reading

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;
        }
    }
}

我得到的樣本: 在此處輸入圖像描述

我不知道您使用的設備的協議,但它似乎每條消息發送 12 個字節。 <STX><+-><9 digit hex><ETX> 因此,一次讀取 12 個字節,並將十六進制轉換為十進制。

所以,首先改變

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

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

然后,解析消息:

// ☻+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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM