`

通过JAVA与串口(RS232)通信实例

    博客分类:
  • J2SE
阅读更多
最近了解到的需求是需要需激光打刻机进行(RS232)串口通信,
这里使用的是RXTX开源包实现的。
之前并没有用java做过串口通信,而且这方面资料不是很多。
项目实际应用中可能会采用VB开发(这个我就不会了)

只不过用java尝试一下,记个笔记,希望可以对相关开发用些帮助。

下面是实现代码
package test;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Date;
import java.util.Enumeration;
import java.util.TooManyListenersException;

import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import gnu.io.UnsupportedCommOperationException;

public class CommUtil implements SerialPortEventListener {

	InputStream inputStream; // 从串口来的输入流
	OutputStream outputStream;// 向串口输出的流
	SerialPort serialPort; // 串口的引用
	CommPortIdentifier portId;

	public CommUtil(Enumeration portList, String name) {
		while (portList.hasMoreElements()) {
			CommPortIdentifier temp = (CommPortIdentifier) portList.nextElement();
			if (temp.getPortType() == CommPortIdentifier.PORT_SERIAL) {// 判断如果端口类型是串口
				if (temp.getName().equals(name)) { // 判断如果端口已经启动就连接
					portId = temp;
				}
			}
		}
		try {
			serialPort = (SerialPort) portId.open("My"+name, 2000);
		} catch (PortInUseException e) {

		}
		try {
			inputStream = serialPort.getInputStream();
			outputStream = serialPort.getOutputStream();
		} catch (IOException e) {
		}
		try {
			serialPort.addEventListener(this); // 给当前串口天加一个监听器
		} catch (TooManyListenersException e) {
		}
		serialPort.notifyOnDataAvailable(true); // 当有数据时通知
		try {
			serialPort.setSerialPortParams(2400, SerialPort.DATABITS_8, // 设置串口读写参数
					SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
		} catch (UnsupportedCommOperationException e) {
		}
	}

	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:
			break;
		
		case SerialPortEvent.DATA_AVAILABLE:// 当有可用数据时读取数据,并且给串口返回数据
			byte[] readBuffer = new byte[20];

			try {
				while (inputStream.available() > 0) {
					System.out.println(inputStream.available());
					int numBytes = inputStream.read(readBuffer);
					System.out.println(numBytes);
				}
				System.out.println(new String(readBuffer).trim());
			} catch (IOException e) {
				e.printStackTrace();
			}
			break;
		}
	}
	public void send(String content){
		try {
			outputStream.write(content.getBytes());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void ClosePort() {
	    if (serialPort != null) {
	      serialPort.close();
	    }
	  }

	
}



测试
package test;

import gnu.io.CommPortIdentifier;

import java.util.Enumeration;

public class Test {

	public static void main(String[] args) throws InterruptedException {
		Enumeration portList = CommPortIdentifier.getPortIdentifiers(); //得到当前连接上的端口
		
		CommUtil comm3 = new CommUtil(portList,"COM3");
		int i = 0;
		while(i<5)
		{
			Thread.sleep(3000);
			comm3.send("hello");
			i++;
		}
		comm3.ClosePort();
	}

}
分享到:
评论
10 楼 a601962168 2017-03-02  
9 楼 wx_hello 2014-08-06  
lyj286326189 写道
报错

java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path thrown while loading gnu.io.RXTXCommDriver
Exception in thread "main" java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1738)
at java.lang.Runtime.loadLibrary0(Runtime.java:823)
at java.lang.System.loadLibrary(System.java:1028)
at gnu.io.CommPortIdentifier.<clinit>(CommPortIdentifier.java:83)
at test.Test.main(Test.java:10)

你的解决了吗?  求解。。。

最近想搞个喷码抢软件,喷一串字符。。不知道如何下手。。。
8 楼 wx_hello 2014-08-06  
最近想搞个喷码抢软件,喷一串字符。。不知道如何下手。。。
7 楼 wx_hello 2014-08-06  
问下:
Enumeration portList = CommPortIdentifier.getPortIdentifiers(); // 得到当前连接上的端口

我在上面的代码处就报错了。。



java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path thrown while loading gnu.io.RXTXCommDriver
Exception in thread "main" java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1709)
at java.lang.Runtime.loadLibrary0(Runtime.java:823)
at java.lang.System.loadLibrary(System.java:1030)
at gnu.io.CommPortIdentifier.<clinit>(CommPortIdentifier.java:83)
at test.CommUtil.main(CommUtil.java:100)




不知道如何解决。。。求大神告知。。。谢谢!
6 楼 ncx1259988 2013-10-14  
这个可以支持中文吗,没弄过这个,最近要弄一个,中文不知道怎么处理
5 楼 di1984HIT 2013-05-22  
我弄了一下,不错。
4 楼 di1984HIT 2013-05-22  
写的不错。
3 楼 lyj286326189 2012-07-20  
报错

java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path thrown while loading gnu.io.RXTXCommDriver
Exception in thread "main" java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1738)
at java.lang.Runtime.loadLibrary0(Runtime.java:823)
at java.lang.System.loadLibrary(System.java:1028)
at gnu.io.CommPortIdentifier.<clinit>(CommPortIdentifier.java:83)
at test.Test.main(Test.java:10)
2 楼 sosyi 2010-09-03  
发短信可以 打电话就找不到端口。。 郁闷中
1 楼 sosyi 2010-09-03  
知道java 调用 AT指令打电话怎么写吗?

相关推荐

Global site tag (gtag.js) - Google Analytics