Axiu Blog
琢磨边写,又是charset,又是proxy,又是什么browsertype的,晕头转向,最后拿Jsoup一会儿就搞定了……不过使用之前要充分考虑可扩展性和API的丰富程度,不然后面程序体积变大集成起来很蛋疼。好了废话不说,讲讲串口的问题…… ### 总述 Java常用的(我用到的)串口工具有俩:javacomm和RXTX,前者只支持win32,后者32
以前经常用C语言操作串口,实现一些发送接收之类的小功能,最近项目中也用到了串口通信,不过语言变成了Java,这个搞起来略蛋疼。 Java说起来就是各种库各种引用,所以得到某些需求先google搜一把~~比如之前搞半天的读取网页内容,自己边琢磨边写,又是charset,又是proxy,又是什么browsertype的,晕头转向,最后拿Jsoup一会儿就搞定了……不过使用之前要充分考虑可扩展性和AP
以前经常用C语言操作串口,实现一些发送接收之类的小功能,最近项目中也用到了串口通信,不过语言变成了Java,这个搞起来略蛋疼。 Java说起来就是各种库各种引用,所以得到某些需求先google搜一把~~比如之前搞半天的读取网页内容,自己边琢磨边写,又是charset,又是proxy,又是什么browsertype的,晕头转向,最后拿Jsoup一会儿就搞定了……不过使用之前要充分考虑可扩展性和AP
JAVA串口通信的方法
Max

以前经常用C语言操作串口,实现一些发送接收之类的小功能,最近项目中也用到了串口通信,不过语言变成了Java,这个搞起来略蛋疼。

Java说起来就是各种库各种引用,所以得到某些需求先google搜一把~~比如之前搞半天的读取网页内容,自己边琢磨边写,又是charset,又是proxy,又是什么browsertype的,晕头转向,最后拿Jsoup一会儿就搞定了……不过使用之前要充分考虑可扩展性和API的丰富程度,不然后面程序体积变大集成起来很蛋疼。好了废话不说,讲讲串口的问题……

总述

Java常用的(我用到的)串口工具有俩:javacomm和RXTX,前者只支持win32,后者32和64位和其他版本都有。使用方法类似,所以就放到一起说:

下载到压缩包会看到readme之类的提示,然后把对应的dll文件和jar文件分别拷贝到/bin和/lib目录下,然后在出问题的时候继续查看readme。接着跑一下附带的例程。

具体使用过程就是:配置端口波特率、校验位等信息;打开串口;写数据;读输出;关闭。这一系列步骤在写程序的时候一定不要弄颠倒了。

串口配置

需要配置的有4项:
波特率(baudrate)— 一般9600、115200即可
(数据位)dataBits— 一般8 bits

DATABITS_5: 5 bits
DATABITS_6: 6 bits
DATABITS_7: 7 bits
DATABITS_8: 8 bits

(停止位)stopBits — 一般1 stop bit

STOPBITS_1: 1 stop bit
STOPBITS_2: 2 stop bits
STOPBITS_1_5: 1.5 stop bits

(校验位)parity — 不需校验位的话,选择SerialPort.PARITY_NONE

PARITY_NONE: no parity
PARITY_ODD: odd parity
PARITY_EVEN: even parity
PARITY_MARK: mark parity
PARITY_SPACE: space parity

流量控制模式(FlowControlMode) — 一般FLOWCONTROL_NONE

FLOWCONTROL_NONE: no flow control
FLOWCONTROL_RTSCTS_IN: RTS/CTS (hardware) flow control for input
FLOWCONTROL_RTSCTS_OUT: RTS/CTS (hardware) flow control for output
FLOWCONTROL_XONXOFF_IN: XON/XOFF (software) flow control for input
FLOWCONTROL_XONXOFF_OUT:XON/XOFF (software) flow control for output

事件监听

addEventListener(SerialPortEventListener lsnr)

用于监听串口事件(SerialEvents),可监听的事件有:

BI(Break interrupt)
CD(Carrier detect)
CTS(Clear to send)
DATA_AVAILABLE(Data available at the serial port)
DSR(Data set ready)
eventType(Deprecated. Replaced by getEventType method)
FE(Framing error)
OE(Overrun error)
OUTPUT_BUFFER_EMPTY(Output buffer is empty)
PE(Parity error)
RI(Ring indicator)

读数据

通过SerialPortEventListener来,读取到DATA_AVAILABLE,保存并处理串口内容。

写数据

使用getOutputStream来向串口发送数据,是发送数据的唯一方法。使用时,向OutPutStream中写入byte串即可,之后用SerialPortEventListener获取回馈结果。

RXTX例程(来自 这里 ):

public class USBComm implements SerialPortEventListener {

public static final String MESSAGE = "MESSAGE TO BE SEND";
/\*\*
 \* Stream for the storage of incoming data
 \*/
private InputStream inputStream;
/\*\*
 \* Stream for the dispatching of data
 \*/
private OutputStream outputStream;
/\*\*
 \* Timeout of the USB port
 \*/
private final int PORT\_TIMEOUT = 2000;
/\*\*
 \* Representation of the serial port used for the communication
 \*/
public SerialPort serialPort;
/\*\*
 \* Buffer that stores the received bytes from the media
 \*/
protected LinkedBlockingQueue receivedBytes;

/\*\*
 \* Builds a new manager for the communication via USB port.
 \* @exception IOException if an error occurred during the opening of the USB port
 \*/
public USBComm(String comm) throws IOException {
  receivedBytes = new LinkedBlockingQueue(100000);
	String port = comm; //place the right COM port here, OS dependent

	//Check that the USB port exists and is recognized:
	Enumeration portList = CommPortIdentifier.getPortIdentifiers();
	boolean portFound = false;
	CommPortIdentifier portId = null;
	while (portList.hasMoreElements()) {
		portId = (CommPortIdentifier) portList.nextElement();
	    if (portId.getPortType() == CommPortIdentifier.PORT\_SERIAL) {
			if (portId.getName().equals(port)) {
				Log.logAndDisplay(LogLevel.INFO, "SerialConnection", "Found port: " + port);
			    portFound = true;
			    break;
			} 
	    } 
	} 

	if (!portFound) 
	    throw new IOException("port " + port + " not found.");

	try {
		Log.logAndDisplay(LogLevel.INFO, "SerialConnection", "USB port " + port + " opening...");
	    serialPort = (SerialPort) portId.open("USBCommunicator", PORT\_TIMEOUT);
	    inputStream = serialPort.getInputStream();
	    outputStream = serialPort.getOutputStream();
	    serialPort.addEventListener(this);
		    	serialPort.notifyOnDataAvailable(true);

	    Thread.sleep(1000);

	    serialPort.setSerialPortParams(serialPort.getBaudRate(),
	    		serialPort.getDataBits(),
	    		serialPort.getStopBits(),
				serialPort.getParity());
    	serialPort.setFlowControlMode(serialPort.getFlowControlMode());

	} catch (Exception e) {
		System.err.println(e.getMessage());
		throw new IOException(e.getMessage());
	}
}


public void closeUSB(){
	//close the streams for serial port:
	try {
		inputStream.close();
		outputStream.close();
	} catch (IOException e) {
		System.err.println("Cannot close streams:" + e.getMessage());
	}
}

/\*\*
 \* Listener for USB events
 \* 
 \* @param event new event occurred on the USB port
 \*/
public void serialEvent(SerialPortEvent event){
	switch (event.getEventType()) {

		case SerialPortEvent.BI:
		case SerialPortEvent.OE:
		case SerialPortEvent.FE:
		case SerialPortEvent.PE:
		case SerialPortEvent.CD:
		case SerialPortEvent.CTS:
		case SerialPortEvent.DSR:
		case SerialPortEvent.RI:
		case SerialPortEvent.OUTPUT\_BUFFER\_EMPTY:
			//nothing to do...
		    break;

		case SerialPortEvent.DATA\_AVAILABLE:
			byte received = -1;
			do {
				try {
					received = (byte)inputStream.read();
				} catch (IOException e) {
					System.err.println("Error reading USB:" + e.getMessage());
				}
			
				synchronized (receivedBytes) {
					try {
						receivedBytes.add(received);
					} catch (IllegalStateException ew) {
						System.err.println(ew.getMessage());
						receivedBytes.poll();
						receivedBytes.add(received);
					}
				}
			} while(received != -1);

		    break;
	}
}

public void write(byte\[\] buffer){
    try {
    	outputStream.write(buffer);
    	//outputStream.flush();
    } catch (IOException e) {
    	System.err.println("Cannot write:" + e.getMessage());
    }
}
public static void main(String\[\] args){
	USBComm usb;
	try {
		usb = new USBComm("COM73"); // change to your port
	byte\[\] b = MESSAGE.getBytes();
	usb.write(b);
	usb.serialPort.close();
	usb.closeUSB();

	} catch (IOException e) {
		System.err.println("IOException:" + e.getMessage());
	}
}

}

后记嗯……好像也啥都没写的样子(我会告诉你这篇其实是测试帖代码效果的吗)……总之去看javacomm附带有例程 / RXTX的例程就可以了……

Comments