⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 comm.java

📁 java使用JNI访问本地串口
💻 JAVA
字号:



/*
 commName is an integer from 0 to 9
 corresponding to comm0,comm1,etc in windows
 baudrate can only be 9600,19200 or 38400

 */
public class comm implements CommError {
	public int commName = 0;
	public int baudrate = 9600;
	public int state = COMMCLOSED;

	public static native int openComm(int name, int boudrate);

	public static native int closeComm(int name);

	public static native byte[] about();

	public static native int pool(int name);

	public static native int getStatus(int name);

	public static native int write(int name, byte[] args, int len);

	public static native byte[] read(int name, int len);

	static {
		System.loadLibrary("commForJava");
	}

	public comm(int s, int baud) throws CommException {
		commName = s;
		baudrate = baud;
	}

	public int OpenComm() throws CommException {
		state = comm.openComm(commName, baudrate);
		if (state < 0) {
			throw (new CommException(state));
		}
		return state;
	}

	public int CloseComm() throws CommException {
		state = comm.closeComm(commName);
		if (state < 0) {
			throw (new CommException(state));
		}
		return state;
	}

	// //////this is a blocked method,which won't return until data ready
	public int Pool() throws CommException {
		state = comm.pool(commName);
		if (state < 0) {
			throw (new CommException(state));
		}
		return state;
	}

	public int GetStatus() throws CommException {
		state = comm.getStatus(commName);
		if (state < 0) {
			throw (new CommException(state));
		}
		return state;
	}

	public int Write(byte[] args, int len) throws CommException {
		state = comm.write(commName, args, len);
		if (state < 0) {
			throw (new CommException(state));
		}
		return state;
	}

	public byte[] Read(int len) {
		return comm.read(commName, len);
	}

	public static void main(String[] args) {

		try {
			comm t2 = new comm(3, 9600);
			t2.OpenComm();
			String s = "this is kongxuetao";
			byte[] bytes = s.getBytes();
			t2.Write(bytes, bytes.length);// //send a string
			t2.CloseComm();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
		} finally {
			comm.closeComm(2);
		}
	}
}

interface CommError {
	final static int INVALIDNAME = -1;
	final static int INVALIDBAUD = -2;
	final static int CERROR = -3;
	final static int COMMEXIST = -4;
	final static int COMMCLOSED = -5;
	final static int COMMCREATIONFAILS = -6;
	final static int SETSTATEERROR = -7;
	final static int COMMERROR = -8;
	final static int OK = 0;
}

class CommException extends Exception implements CommError {
	static final long serialVersionUID = 1l;
	public String errorDetails;

	public CommException(String s) {
		super(s);
		errorDetails = s;
	}

	public CommException(int type) {
		super();
		if (type == CommError.CERROR) {
			errorDetails = "CERROR,error in c functions";
		} else if (type == CommError.INVALIDNAME) {
			errorDetails = "INVALIDNAME,should be int within 0-9";
		} else if (type == CommError.INVALIDBAUD) {
			errorDetails = "INVALIDBAUD,should be 9600,19200,or 38400";
		} else if (type == CommError.COMMEXIST) {
			errorDetails = "COMMEXIST,comm already exists";
		} else if (type == CommError.COMMCLOSED) {
			errorDetails = "COMMCLOSED,comm closed";
		} else if (type == CommError.COMMCREATIONFAILS) {
			errorDetails = "COMMCREATIONFAILS,comm creation fails";
		} else if (type == CommError.SETSTATEERROR) {
			errorDetails = "SETSTATEERROR,set comm states failed";
		} else if (type == CommError.COMMERROR) {
			errorDetails = "COMMERROR,comm error";
		} else {
			errorDetails = "UNKNOWNERROR";
		}
	}

	public String getMessage() {
		return errorDetails;
	}
}

⌨️ 快捷键说明

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