Java实现串口通信与单片机通信实例..【Pnoter】

一 · 在windows操作系统下,实现Java串口通信,需要用到sun提供的串口通信包,javacomm【可以去Java站点去下载最新版本的压缩包,然后解压】。

解压之后要用到压缩包中的三个文件,将这三个文件按照如下方式配置【一下路径为相对路径,视个人电脑Java安装路径不同而异】:

1、comm.jar放置到 JAVA_HOME/jre/lib/ext;【jre/lib(也就是在JAVA文件夹下的jre)】

2、win32com.dll放置到 JAVA_HOME/bin;

3、javax.comm.properties两个地方都要放;

二 · 在这里我们首先了解一下Java串口通信API说明【CommunicationAPI(包含于javax.comm包中)】,这样有利进一步了解Java串口通信包,ava提供了 CommunicationAPI(包含于javax.comm包中)用于通过与机器无关的方式,控制各种外部设备。Communications API,是标准的Java的扩展部分,它在JavaAPI中是没有附带的。

三 · Communications API 简介:

Communications API 的核心是抽象的CommPort类及其两个子类:SerialPort类和ParallePort类。其中,SerialPort类是用于串口通信的类,ParallePort类是用于并行口通信的类。CommPort类还提供了常规的通信模式和方法,例如:getInputStream( )方法和getOutputStream( )方法,专用于与端口上的设备进行通信。然而,这些类的构造方法都被有意的设置为非公有的(non-public)。所以,不能直接构造对象,而是先通过静态的CommPortIdentifer.getPortIdentifiers()获得端口列表;再从这个端口列表中选择所需要的端口,并调用CommPortIdentifer对象的Open( )方法,这样,就能得到一个CommPort对象。当然,还要将这个CommPort对象的类型转换为某个非抽象的子类,表明是特定的通讯设备。该子类可以是SerialPort类和ParallePort类中的一个。下面将分别对CommPort类,CommPortIdentifier类,串口类SerialPort进行详细的介绍。 

四 · CommPortIdentifier类 :

addPortName(String, int, CommDriver) 添加端口名到端口列表里 
addPortOwnershipListener(CommPortOwnershipListener) 添加端口拥有的监听器 
removePortOwnershipListener(CommPortOwnershipListener) 移除端口拥有的监听器 
getCurrentOwner() 得到当前占有端口的对象或应用程序 
getName() 得到端口名称 
getPortIdentifier(CommPort) 得到参数打开的端口的CommPortIdentifier类型对象 
getPortIdentifier(String) 得到以参数命名的端口的CommPortIdentifier类型对象 
getPortIdentifiers() 得到系统中的端口列表 
getPortType() 得到端口的类型 
isCurrentlyOwned() 判断当前端口是否被占用 
open(FileDescriptor) 用文件描述的类型打开端口 
open(String, int) 打开端口,两个参数:程序名称,延迟时间(毫秒数) 

五 · SerialPort类:

DATABITS_5 数据位为5 STOPBITS_2 停止位为2 PARITY_ODD 奇检验 
DATABITS_6 数据位为6 STOPBITS_1 停止位为1 PARITY_MARK 标记检验 
DATABITS_7 数据位为7 STOPBITS_1_5 停止为1.5 PARITY_NONE 空格检验 
DATABITS_8 数据位为8 PARITY_EVEN 偶检验 PARITY_SPACE 无检验 
SerialPort对象的关于串口参数的函数 
getBaudRate() 得到波特率 getParity() 得到检验类型 
getDataBits() 得到数据位数 getStopBits() 得到停止位数 
setSerialPortParams(int, int, int, int) 设置串口参数依次为(波特率,数据位,停止位,奇偶检验) 
SerialPort关于事件的静态成员变量 
BI Break interrupt中断 FE Framing error错误 
CD Carrier detect载波侦听 OE Overrun error错误 
CTS Clear to send清除以传送 PE Parity error奇偶检验错误 
DSR Data set ready数据备妥 RI Ring indicator响铃侦测 
DATA_AVAILABLE 串口中的可用数据 OUTPUT_BUFFER_EMPTY 输出缓冲区空 
SerialPort中关于事件的方法 
isCD() 是否有载波 isCTS() 是否清除以传送 isDSR() 数据是否备妥 
isDTR() 是否数据端备妥 isRI() 是否响铃侦测 isRTS()   是否要求传送 
addEventListener(SerialPortEventListener)    向SerialPort对象中添加串口事件监听器 
removeEventListener() 移除SerialPort对象中的串口事件监听器 
notifyOnBreakInterrupt(boolean) 设置中断事件true有效,false无效 
notifyOnCarrierDetect(boolean) 设置载波监听事件true有效,false无效 
notifyOnCTS(boolean) 设置清除发送事件true有效,false无效 
notifyOnDataAvailable(boolean) 设置串口有数据的事件true有效,false无效 
notifyOnDSR(boolean) 设置数据备妥事件true有效,false无效 
notifyOnFramingError(boolean) 设置发生错误事件true有效,false无效 
notifyOnOutputEmpty(boolean) 设置发送缓冲区为空事件true有效,false无效 
notifyOnParityError(boolean) 设置发生奇偶检验错误事件true有效,false无效 
notifyOnRingIndicator(boolean) 设置响铃侦测事件true有效,false无效 
getEventType() 得到发生的事件类型返回值为int型 
sendBreak(int) 设置中断过程的时间,参数为毫秒值 
setRTS(boolean) 设置或清除RTS位 
setDTR(boolean) 设置或清除DTR位 
close() 关闭串口 
getOutputStream() 得到OutputStream类型的输出流 
getInputStream() 得到InputStream类型的输入流

六 · 看一段用Java写的一个往串口写(Write)字符串的完整程序【如果你的电脑没有那么多的commport可以去网上搜索一个虚拟端口的软件,在你的电脑上开几个虚拟的commport,推荐软件Virtual Serial Port Driver ,个人表示很好用,可惜是收费的,不过可以试用,你懂!】

如下为往我的电脑com1口发送字符串的实例【已经经过测试,完整无误!】

//添加类包
import javax.comm.*;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Enumeration;
import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;

//创建一个类JavaSerialPort,通过关键词implements实声明自己使用Runnable和SerialPortEventListener这两个接口
public class myComPortServlet  {
	// 定义、声明变量
	private String appName = "Java串口通信";
	private int timeout = 2000;// 定义一个打开端口的最大等待时间
	public static String PortName;
	private CommPortIdentifier commPort;
	private SerialPort serialPort;
	private OutputStream outputStream;
	public static OutputStream out;
	public static String messageString = "点亮单片机上的第**个LED灯!";// 给选定端口发送的字符

	// listPort()方法的定义
	public void listPort() {
		CommPortIdentifier cpid;
		Enumeration en = CommPortIdentifier.getPortIdentifiers();
		//System.out.println("端口信息 :" + en);
		//System.out.println("端口列表如下:");
		while (en.hasMoreElements()) {
			cpid = (CommPortIdentifier) en.nextElement();
			if (cpid.getPortType() == CommPortIdentifier.PORT_SERIAL) {
				//System.out.println(cpid.getName() + ", "
						//+ cpid.getCurrentOwner());
			}
		}
	}

	// selectPort(String portName)方法的定义
	public void selectPort(String portName) {
		this.commPort = null;
		CommPortIdentifier cpid;
		Enumeration en = CommPortIdentifier.getPortIdentifiers();
		while (en.hasMoreElements()) {
			cpid = (CommPortIdentifier) en.nextElement();
			if (cpid.getPortType() == CommPortIdentifier.PORT_SERIAL) {
				if (cpid.getName().equals(portName)) {
					this.commPort = cpid;
				}
			}
		}
		openPort();
	}

	// openPort()方法的定义
	private void openPort() {
		try {
			serialPort = (SerialPort) commPort.open(appName, timeout);
		} catch (PortInUseException e) {
		}
		try {
			out = serialPort.getOutputStream();
		} catch (IOException e) {
		}
		try {
			serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
					SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
		} catch (UnsupportedCommOperationException e) {
		}
		try {
			out.write(messageString.getBytes());
		} catch (IOException e) {
		}
	}

	// checkPort()方法的定义
	private void checkPort() {
		if (commPort == null)
			throw new RuntimeException(
					"端口选择出错请用selectPort(String portName)方法选择正确的端口!");

		if (serialPort == null) {
			throw new RuntimeException("SerialPort 对象无效!");
		}
	}

	// write(String message)方法定义
	public void write(String message) {
		checkPort();

		try {
			outputStream = new BufferedOutputStream(
					serialPort.getOutputStream());
		} catch (IOException e) {
			throw new RuntimeException("获取端口的OutputStream出错:" + e.getMessage());
		}

		try {
			outputStream.write(message.getBytes());
			//log("信息发送成功!");
		} catch (IOException e) {
			throw new RuntimeException("向端口发送信息时出错:" + e.getMessage());
		} finally {
			try {
				outputStream.close();
			} catch (Exception e) {
			}
		}
	}

	// close()方法的定义
	public void close() {
		serialPort.close();
		serialPort = null;
		commPort = null;
	}

	// 类的主方法,Java程序运行首先是从这里开始的
	public static void main(String[] args) {
		myComPortServlet sp = new myComPortServlet();
		sp.listPort();
		sp.selectPort("COM1");// 选择COM1口交换数据数据

	}
}
七 · 我们再 看一段用Java写的一个往串口写(Read)字符串的部分代码【只是在上面的程序中添加了几个方法,read()方法和serialEvent()端口监听方法以及run()计时方法】。
如下为读取我的电脑com2口字符串的实例【已经经过测试,完整无误!】
注意事项:关串口很重要,不然会出现运行错误【如下为close定义:
public void close() {
		serialPort.close();
		serialPort = null;
		commPort = null;
】。

	// 接收函数的定义
	public void startRead(int time) {
		try {
			inputStream = new BufferedInputStream(serialPort.getInputStream());
		} catch (IOException e) {
			throw new RuntimeException("获取端口的InputStream出错:" + e.getMessage());
		}
		try {
			serialPort.addEventListener(this);
		} catch (TooManyListenersException e) {
			throw new RuntimeException(e.getMessage());
		}
		serialPort.notifyOnDataAvailable(true);
		if (time > 0) {
			this.threadTime = time * 1000;
			Thread t = new Thread(this);
			t.start();
		}
	}

	// 端口事件监听函数定义
	public void serialEvent(SerialPortEvent arg0) {
		String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
		String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=mytest";
		String userName = "sa"; // 默认用户名
		String userPwd = "9406"; // 密码
		Connection dbConn;
		switch (arg0.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[1024];
			try {
				while (inputStream.available() > 0) {
					inputStream.read(readBuffer);
					tempreture += new String(readBuffer).trim();
				}
				System.out.println(tempreture);
			} catch (IOException e) {
			}
			try {
				Class.forName(driverName);
				dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
				if (!dbConn.isClosed()) {
					// pw.println("_>Succeed to connecting SQL!" + "
" + "
"); } Statement statement1 = dbConn.createStatement(); @SuppressWarnings("unused") Statement statement2 = dbConn.createStatement(); String sql1 = "INSERT INTO tempreture(tempreture) VALUES('" + tempreture + "')"; @SuppressWarnings("unused") int rs1 = statement1.executeUpdate(sql1); } catch (Exception e) { e.printStackTrace(); } } } // 计时函数 public void run() { try { Thread.sleep(threadTime); serialPort.close();// 关闭接收函数,端口关闭 } catch (Exception e) { e.printStackTrace(); } }




  • 8
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值