大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專注Java教育14年 全國(guó)咨詢/投訴熱線:400-8080-105
動(dòng)力節(jié)點(diǎn)LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁(yè) 學(xué)習(xí)攻略 Java學(xué)習(xí) Java基礎(chǔ)學(xué)習(xí):java串口通信教程

Java基礎(chǔ)學(xué)習(xí):java串口通信教程

更新時(shí)間:2020-04-01 13:57:30 來(lái)源:動(dòng)力節(jié)點(diǎn) 瀏覽2505次


  用Java實(shí)現(xiàn)串口通信(windows系統(tǒng)下),需要用到sun提供的串口包 javacomm20-win32.zip。其中要用到三個(gè)文件,配置如下:


  1.comm.jar放置到 JAVA_HOME/jre/lib/ext;


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


  3.javax.comm.properties 兩個(gè)地方都要放


  jre/lib(也就是在JAVA文件夾下的jre)


  JAVA_HOME/jre/lib


  說(shuō)一下我應(yīng)用的環(huán)境。電子秤稱重時(shí),計(jì)算機(jī)通過(guò)串口給稱重控制顯示器發(fā)送一次命令“R”,控制顯示器則發(fā)送一次重量數(shù)據(jù)給串口,計(jì)算機(jī)再讀取將數(shù)據(jù)顯示在網(wǎng)頁(yè)上。這樣就構(gòu)成了一個(gè)實(shí)時(shí)稱重系統(tǒng)。


  讀寫串口的代碼如下:

  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;
  //保存讀數(shù)結(jié)果
  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; //輸入輸出流錯(cuò)誤
  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; //參數(shù)不正確
  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();
  }
  }

  這樣通過(guò) SerialBean.result 就可得到讀數(shù)結(jié)果。


  至于把數(shù)據(jù)放到網(wǎng)頁(yè)上,就要用到Ajax了,這里用到了一個(gè)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(){
  //發(fā)送指令R,儀器發(fā)送一次凈重?cái)?shù)據(jù)
  SerialBean.WritePort("R");
  //讀取數(shù)據(jù)
  SerialBean.ReadPort();
  String temp=SerialBean.result.trim(); //我這里temp是形如 wn125.000kg 的數(shù)據(jù)
  if(!temp.equals("") && temp.length()==11)
  {
  return (change(temp)).toString();
  }else{
  return "";
  }
  }
  //響應(yīng)開始稱重
  public String startWeight(String num){
  int n=Integer.parseInt(num.trim());
  SerialBean SB = new SerialBean(n);
  SB.Initialize();
  return SerialBean.openSignal+""; //返回初始化信息
  }
  //響應(yīng)停止稱重
  public void endWeight(){
  try {
  //關(guān)閉輸入、輸出流
  SerialBean.in.close();
  SerialBean.out.close();
  } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  }
  if(SerialBean.serialPort!=null){
  SerialBean.serialPort.close(); //關(guān)閉串口
  }
  SerialBean.serialPort=null;
  SerialBean.portId=null;
  SerialBean.result="";
  }
  /**
  * 將形如 wn125.000kg 格式的重量轉(zhuǎn)換為 125.000 (kg)(四舍五入,小數(shù)點(diǎn)后保留兩位)
  */
  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是一個(gè)java 的高精度計(jì)算文件。

  package com.chengzhong.tools;
  import java.math.BigDecimal;
  /**
  * 由于Java的簡(jiǎn)單類型不能夠精確的對(duì)浮點(diǎn)數(shù)進(jìn)行運(yùn)算,這個(gè)工具類提供精
  * 確的浮點(diǎn)數(shù)運(yùn)算,包括加減乘除和四舍五入。
  */
  public class Arith{
  //默認(rèn)除法運(yùn)算精度
  private static final int DEF_DIV_SCALE = 10;
  //這個(gè)類不能實(shí)例化
  private Arith(){
  }
  /**
  * 提供精確的加法運(yùn)算。
  * @param v1 被加數(shù)
  * @param v2 加數(shù)
  * @return 兩個(gè)參數(shù)的和
  */
  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();
  }
  /**
  * 提供精確的減法運(yùn)算。
  * @param v1 被減數(shù)
  * @param v2 減數(shù)
  * @return 兩個(gè)參數(shù)的差
  */
  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();
  }
  /**
  * 提供精確的乘法運(yùn)算。
  * @param v1 被乘數(shù)
  * @param v2 乘數(shù)
  * @return 兩個(gè)參數(shù)的積
  */
  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();
  }
  /**
  * 提供(相對(duì))精確的除法運(yùn)算,當(dāng)發(fā)生除不盡的情況時(shí),精確到
  * 小數(shù)點(diǎn)以后10位,以后的數(shù)字四舍五入。
  * @param v1 被除數(shù)
  * @param v2 除數(shù)
  * @return 兩個(gè)參數(shù)的商
  */
  public static double div(double v1,double v2){
  return div(v1,v2,DEF_DIV_SCALE);
  }
  /**
  * 提供(相對(duì))精確的除法運(yùn)算。當(dāng)發(fā)生除不盡的情況時(shí),由scale參數(shù)指
  * 定精度,以后的數(shù)字四舍五入。
  * @param v1 被除數(shù)
  * @param v2 除數(shù)
  * @param scale 表示表示需要精確到小數(shù)點(diǎn)以后幾位。
  * @return 兩個(gè)參數(shù)的商
  */
  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();
  }
  /**
  * 提供精確的小數(shù)位四舍五入處理。
  * @param v 需要四舍五入的數(shù)字
  * @param scale 小數(shù)點(diǎn)后保留幾位
  * @return 四舍五入后的結(jié)果
  */
  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();
  }
  }

  網(wǎng)頁(yè)頁(yè)面上:

     Java基礎(chǔ)學(xué)習(xí):java串口通信教程


  以上就是動(dòng)力節(jié)點(diǎn)java培訓(xùn)機(jī)構(gòu)的小編針對(duì)“Java基礎(chǔ)學(xué)習(xí):java串口通信教程”的內(nèi)容進(jìn)行的回答,希望對(duì)大家有所幫助,如有疑問,請(qǐng)?jiān)诰€咨詢,有專業(yè)老師隨時(shí)為你服務(wù)。


提交申請(qǐng)后,顧問老師會(huì)電話與您溝通安排學(xué)習(xí)

  • 全國(guó)校區(qū) 2025-06-26 搶座中
免費(fèi)課程推薦 >>
技術(shù)文檔推薦 >>
主站蜘蛛池模板: 九天玄帝诀高清300集免费观看 | 九九99久久精品国产 | 国产日韩在线 | 狠狠色噜噜噜噜狠狠狠狠狠狠奇米 | 男人天堂日韩 | 国产精品成人不卡在线观看 | 免费国产一区二区三区 | 国产在线操 | 久久女同互慰一区二区三区 | 欧美福利在线 | 午夜在线视频网站 | 亚洲精品日韩中文字幕久久久 | 国内精品久久久久尤物 | 激情奇米| 深夜福利国产 | 特级毛片在线观看 | 日日好| 国产欧美一区二区精品仙草咪 | 精品在线观看一区 | 免费观看性欧美一级 | 黄色录像欧美 | 亚洲第一综合网站 | 羞羞视频网站 | 在线观看免费av网站 | 色姑娘综合 | 中国第一毛片 | 亚洲区一| 狠狠色噜噜狠狠米奇777 | 欧美日韩视频在线成人 | 久久精品日日躁夜夜躁欧美 | 奇米影视第四色777 奇米影视第四色7777 | 亚洲一区小说区中文字幕 | 伊人8| 精品一区二区免费视频 | 九九久久国产精品大片 | 国产精品1区2区3区在线播放 | 欧美aa一级片 | 亚洲高清视频免费 | 抱着cao才爽视频 | 欧美亚洲一区二区三区 | 久久久久久久久久免费视频 |