Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
472 views
in Technique[技术] by (71.8m points)

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

Sample of what I get:

(我得到的样本:) 在此处输入图片说明

  ask by Mustafa Samir translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...