Java语言实现的串口通信功能示例[Java代码]
龚超 2018-07-20 来源 : 阅读 1540 评论 0

摘要:本文主要向大家介绍了Java语言实现的串口通信功能示例,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。

本文主要向大家介绍了Java语言实现的串口通信功能示例,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。

用Java实现串口通信(windows系统下),需要用到sun提供的串口包 javacomm20-win32.zip。其中要用到三个文件,配置如下:

1.comm.jar放置到 JAVA_HOME/jre/lib/ext;
2.win32com.dll放置到 JAVA_HOME/bin;
3.javax.comm.properties 两个地方都要放
jre/lib(也就是在JAVA文件夹下的jre)
JAVA_HOME/jre/lib

说一下我应用的环境。电子秤称重时,计算机通过串口给称重控制显示器发送一次命令“R”,控制显示器则发送一次重量数据给串口,计算机再读取将数据显示在网页上。这样就构成了一个实时称重系统。

读写串口的代码如下:


package com.chengzhong.tools;

import java.io.*;

import javax.comm.CommPortIdentifier;

import javax.comm.*;

/**

*

* This bean provides some basic functions to implement full duplex

* information exchange through the serial port.

*

*/

public class SerialBean

{

public static String PortName;

public static CommPortIdentifier portId;

public static SerialPort serialPort;

public static OutputStream out;

public static InputStream in;

//保存读数结果

public static String result="";

public static int openSignal=1;

/**

*

* Constructor

*

* @param PortID the ID of the serial to be used. 1 for COM1,

* 2 for COM2, etc.

*

*/

public SerialBean(int PortID)

{

PortName = "COM" +PortID;

}

/**

*

* This function initialize the serial port for communication. It starts a

* thread which consistently monitors the serial port. Any signal captured

* from the serial port is stored into a buffer area.

*

*/

public int Initialize()

{

openSignal=1;

try

{

portId = CommPortIdentifier.getPortIdentifier(PortName);

try

{

serialPort = (SerialPort)

portId.open("Serial_Communication", 2000);

} catch (PortInUseException e)

{

if(!SerialBean.portId.getCurrentOwner().equals("Serial_Communication"))

{

openSignal=2; //该串口被其它程序占用

}else if(SerialBean.portId.getCurrentOwner().equals("Serial_Communication")){

openSignal=1;

return openSignal;

}

return openSignal;

}

//Use InputStream in to read from the serial port, and OutputStream

//out to write to the serial port.

try

{

in = serialPort.getInputStream();

out = serialPort.getOutputStream();

} catch (IOException e)

{

openSignal=3; //输入输出流错误

return openSignal;

}

//Initialize the communication parameters to 9600, 8, 1, none.

try

{

serialPort.setSerialPortParams(9600,

SerialPort.DATABITS_8,

SerialPort.STOPBITS_1,

SerialPort.PARITY_NONE);

} catch (UnsupportedCommOperationException e)

{

openSignal=4; //参数不正确

return openSignal;

}

} catch (NoSuchPortException e)

{

portId=null;

openSignal=5; //没有该串口

return openSignal;

}

// when successfully open the serial port, create a new serial buffer,

// then create a thread that consistently accepts incoming signals from

// the serial port. Incoming signals are stored in the serial buffer.

// return success information

return openSignal;

}

/**

*

* This function returns a string with a certain length from the incoming

* messages.

*

* @param Length The length of the string to be returned.

*

*/

public static void ReadPort()

{

SerialBean.result="";

int c;

try {

if(in!=null){

while(in.available()>0)

{

c = in.read();

Character d = new Character((char) c);

SerialBean.result=SerialBean.result.concat(d.toString());

}

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

/**

*

* This function sends a message through the serial port.

*

* @param Msg The string to be sent.

*

*/

public static void WritePort(String Msg)

{

try

{

if(out!=null){

for (int i = 0; i < Msg.length(); i++)

out.write(Msg.charAt(i));

}

} catch (IOException e) {

return;

}

}

/**

*

* This function closes the serial port in use.

*

*/

public void ClosePort()

{

serialPort.close();

}

}



复制代码

这样通过 SerialBean.result 就可得到读数结果。

至于把数据放到网页上,就要用到Ajax了,这里用到了一个Ajax框架dwr, dwr类Put.java 如下:


package com.chengzhong.dwr;

import java.io.IOException;

import com.chengzhong.tools.Arith;

import com.chengzhong.tools.SerialBean;

public class Put {

//2011.9.17

public String write(){

//发送指令R,仪器发送一次净重数据

SerialBean.WritePort("R");

//读取数据

SerialBean.ReadPort();

String temp=SerialBean.result.trim(); //我这里temp是形如 wn125.000kg 的数据

if(!temp.equals("") && temp.length()==11)

{

return (change(temp)).toString();

}else{

return "";

}

}

//响应开始称重

public String startWeight(String num){

int n=Integer.parseInt(num.trim());

SerialBean SB = new SerialBean(n);

SB.Initialize();

return SerialBean.openSignal+""; //返回初始化信息

}

//响应停止称重

public void endWeight(){

try {

//关闭输入、输出流

SerialBean.in.close();

SerialBean.out.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

if(SerialBean.serialPort!=null){

SerialBean.serialPort.close(); //关闭串口

}

SerialBean.serialPort=null;

SerialBean.portId=null;

SerialBean.result="";

}

/**

* 将形如 wn125.000kg 格式的重量转换为 125.000 (kg)(四舍五入,小数点后保留两位)

*/

public String change(String source){

Double result=0.0;

String s1=source.substring(2,9);

try{

result=Double.parseDouble(s1);

result=Arith.round(result,2);

}catch(Exception e){

e.printStackTrace();

return "";

}

return result.toString();

}

}



复制代码

注:Arith.java是一个java 的高精度计算文件。


package com.chengzhong.tools;

import java.math.BigDecimal;

/**

* 由于Java的简单类型不能够精确的对浮点数进行运算,这个工具类提供精

* 确的浮点数运算,包括加减乘除和四舍五入。

*/

public class Arith{

//默认除法运算精度

private static final int DEF_DIV_SCALE = 10;

//这个类不能实例化

private Arith(){

}

/**

* 提供精确的加法运算。

* @param v1 被加数

* @param v2 加数

* @return 两个参数的和

*/

public static double add(double v1,double v2){

BigDecimal b1 = new BigDecimal(Double.toString(v1));

BigDecimal b2 = new BigDecimal(Double.toString(v2));

return b1.add(b2).doubleValue();

}

/**

* 提供精确的减法运算。

* @param v1 被减数

* @param v2 减数

* @return 两个参数的差

*/

public static double sub(double v1,double v2){

BigDecimal b1 = new BigDecimal(Double.toString(v1));

BigDecimal b2 = new BigDecimal(Double.toString(v2));

return b1.subtract(b2).doubleValue();

}

/**

* 提供精确的乘法运算。

* @param v1 被乘数

* @param v2 乘数

* @return 两个参数的积

*/

public static double mul(double v1,double v2){

BigDecimal b1 = new BigDecimal(Double.toString(v1));

BigDecimal b2 = new BigDecimal(Double.toString(v2));

return b1.multiply(b2).doubleValue();

}

/**

* 提供(相对)精确的除法运算,当发生除不尽的情况时,精确到

* 小数点以后10位,以后的数字四舍五入。

* @param v1 被除数

* @param v2 除数

* @return 两个参数的商

*/

public static double div(double v1,double v2){

return div(v1,v2,DEF_DIV_SCALE);

}

/**

* 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指

* 定精度,以后的数字四舍五入。

* @param v1 被除数

* @param v2 除数

* @param scale 表示表示需要精确到小数点以后几位。

* @return 两个参数的商

*/

public static double div(double v1,double v2,int scale){

if(scale<0){

throw new IllegalArgumentException(

"The scale must be a positive integer or zero");

}

BigDecimal b1 = new BigDecimal(Double.toString(v1));

BigDecimal b2 = new BigDecimal(Double.toString(v2));

return b1.divide(b2,scale,BigDecimal.ROUND_HALF_UP).doubleValue();

}

/**

* 提供精确的小数位四舍五入处理。

* @param v 需要四舍五入的数字

* @param scale 小数点后保留几位

* @return 四舍五入后的结果

*/

public static double round(double v,int scale){

if(scale<0){

throw new IllegalArgumentException(

"The scale must be a positive integer or zero");

}

BigDecimal b = new BigDecimal(Double.toString(v));

BigDecimal one = new BigDecimal("1");

return b.divide(one,scale,BigDecimal.ROUND_HALF_UP).doubleValue();

}

}



复制代码

网页页面上:




复制代码

dwr的使用就不说了

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注编程语言JAVA频道!

本文由 @职坐标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论
本文作者 联系TA

擅长针对企业软件开发的产品设计及开发的细节与流程设计课程内容。座右铭:大道至简!

  • 370
    文章
  • 22911
    人气
  • 87%
    受欢迎度

已有23人表明态度,87%喜欢该老师!

进入TA的空间
求职秘籍 直通车
  • 索取资料 索取资料 索取资料
  • 答疑解惑 答疑解惑 答疑解惑
  • 技术交流 技术交流 技术交流
  • 职业测评 职业测评 职业测评
  • 面试技巧 面试技巧 面试技巧
  • 高薪秘笈 高薪秘笈 高薪秘笈
TA的其他文章 更多>>
WEB前端必须会的基本知识题目
经验技巧 93% 的用户喜欢
Java语言中四种遍历List的方法总结(推荐)
经验技巧 91% 的用户喜欢
Java语言之SHA-256加密的两种实现方法详解
经验技巧 75% 的用户喜欢
java语言实现把两个有序数组合并到一个数组的实例
经验技巧 75% 的用户喜欢
通过Java语言代码来创建view的方法
经验技巧 80% 的用户喜欢
其他海同师资 更多>>
吕益平
吕益平 联系TA
熟悉企业软件开发的产品设计及开发
孔庆琦
孔庆琦 联系TA
对MVC模式和三层架构有深入的研究
周鸣君
周鸣君 联系TA
擅长Hadoop/Spark大数据技术
范佺菁
范佺菁 联系TA
擅长Java语言,只有合理的安排和管理时间你才能做得更多,行得更远!
金延鑫
金延鑫 联系TA
擅长与学生或家长及时有效沟通
经验技巧30天热搜词 更多>>

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程