serialbuffer.java

来自「用Java在Windows下实现串口全双工通讯的例子」· Java 代码 · 共 62 行

JAVA
62
字号
package serial;

/**
 * 
 * This class implements the buffer area to store incoming data from the serial
 * port.
 * 
 */
public class SerialBuffer {
	private String Content = "";
	private String CurrentMsg = "";
	private String TempContent = "";
	private boolean available = false;
	private int LengthNeeded = 1;

	/**
	 * 
	 * This function returns a string with a certain length from the incomin
	 * messages.
	 * 
	 * @param Length
	 *            The length of the string to be returned.
	 * 
	 */
	public synchronized String GetMsg(int Length) {
		LengthNeeded = Length;
		notifyAll();
		if (LengthNeeded > Content.length()) {
			available = false;
			while (available == false) {
				try {
					wait();
				} catch (InterruptedException e) {
				}
			}
		}
		CurrentMsg = Content.substring(0, LengthNeeded);
		TempContent = Content.substring(LengthNeeded);
		Content = TempContent;
		LengthNeeded = 1;
		notifyAll();
		return CurrentMsg;
	}

	/**
	 * 
	 * This function stores a character captured from the serial port to the
	 * buffer area.
	 * 
	 * @param t
	 *            The char value of the character to be stored.
	 * 
	 */
	public synchronized void PutChar(int c) {
		Character d = new Character((char) c);
		Content = Content.concat(d.toString());
		if (LengthNeeded < Content.length()) {
			available = true;
		}
		notifyAll();
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?