java串口开发程序

小虾米 阅读:634 2021-04-01 11:07:04 评论:0

1.pom文件

<dependency> 
	<groupId>org.bidib.jbidib.org.qbang.rxtx</groupId> 
	<artifactId>rxtxcomm</artifactId> 
	<version>2.2</version> 
</dependency> 

2.需要监听的com口

public class RxtxBuilder { 
	public static List<CommUtil> comms = new ArrayList<CommUtil>(); 
	//com列表COM1,COM2 
	public static void init(Set<String> names) { 
		Enumeration<?> portList = CommPortIdentifier.getPortIdentifiers(); // 得到当前连接上的端口 
		while (portList.hasMoreElements()) { 
			CommPortIdentifier temp = (CommPortIdentifier) portList.nextElement(); 
			System.out.println("端口类型:"+temp.getPortType()+"="+temp.getName()); 
			if (temp.getPortType() == CommPortIdentifier.PORT_SERIAL) {// 判断如果端口类型是串口 
				if (names.contains(temp.getName())) { // 判断如果端口已经启动就连接 
					System.err.println("端口---" + temp.getName() + "启动监听"); 
					CommUtil comm = new CommUtil(temp, temp.getName()); 
					comms.add(comm); 
				} 
			} 
		} 
	} 
} 

3.监听类CommUtil

public class CommUtil implements SerialPortEventListener { 
	DataInputStream inputStream; // 从串口来的输入流 
	OutputStream outputStream;// 向串口输出的流 
	SerialPort serialPort; // 串口的引用 
	CommPortIdentifier portId; 
	int count = 0; 
	String name; 
	public CommUtil(CommPortIdentifier temp, String name) { 
		this.name = name; 
		try { 
			portId = temp; 
			serialPort = (SerialPort) portId.open(name, 2000); 
		} catch (PortInUseException e) { 
			e.printStackTrace(); 
		} 
		try { 
			BufferedInputStream bufIn = new BufferedInputStream(serialPort.getInputStream()); 
			inputStream = new DataInputStream(bufIn); 
			outputStream = serialPort.getOutputStream(); 
		} catch (IOException e) { 
		} 
		try { 
			serialPort.addEventListener(this); // 给当前串口添加一个监听器 
		} catch (TooManyListenersException e) { 
		} 
		serialPort.notifyOnDataAvailable(true); // 当有数据时通知 
		try { 
			serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, // 设置串口读写参数 
					SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); 
		} catch (UnsupportedCommOperationException e) { 
		} 
	} 
 
	@Override 
	public void serialEvent(SerialPortEvent event) { 
		log.info("{},event:{}", Thread.currentThread(), event.getSource().toString()); 
		log.info("端口:{}", name); 
		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:// 当有可用数据时读取数据 
			try { 
				int available = inputStream.available(); 
				if (available < 4) 
					return; 
				int slaveAddr = inputStream.readUnsignedByte(); 
				System.out.println("available:"+available); 
				System.out.println("slaveAddr:"+slaveAddr); 
			} catch (IOException e) { 
				// TODO Auto-generated catch block 
				e.printStackTrace(); 
			} 
			break; 
		} 
	} 
 
	public void send(byte[] h2sSendBytes) { 
		try { 
			String hex = MyNumberUtil.encodeHexString(h2sSendBytes); 
			log.info("send:{}",hex); 
			outputStream.write(h2sSendBytes); 
		} catch (IOException e) { 
			e.printStackTrace(); 
		} 
	} 
 
	public void ClosePort() { 
		if (serialPort != null) { 
			serialPort.close(); 
		} 
	} 
 
	public String getName() { 
		return name; 
	} 
} 
 

4.模拟串口输入
在这里插入图片描述
5.收到数据
在这里插入图片描述

声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号