欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java 串口通信詳細(xì)及簡(jiǎn)單實(shí)例

 更新時(shí)間:2017年01月11日 11:07:33   投稿:lqh  
這篇文章主要介紹了java 串口通信詳細(xì)及簡(jiǎn)單實(shí)例的相關(guān)資料,在開發(fā)硬件與軟件結(jié)合的時(shí)候,就會(huì)用到串口,需要的朋友可以參考下

java 實(shí)現(xiàn)串口通信

最近做了一個(gè)與硬件相關(guān)的項(xiàng)目,剛開始聽說用java和硬件打交道,著實(shí)下了一大跳。java也可以操作硬件?

后來接觸到是用java通過串口通信控制硬件感覺使用起來還不錯(cuò),也很方便。

特拿出來和大家一起分享一下。

準(zhǔn)備工作:

首先到SUN官網(wǎng)下載一個(gè)zip包:javacomm20-win32.zip

其中重要的有這幾個(gè)文件:

win32com.dll

comm.jar

javax.comm.properties

按照說明配置好環(huán)境,如下:

將win32com.dll復(fù)制到<JDK>\bin目錄下;將comm.jar復(fù)制到<JDK>\lib;把 javax.comm.properties也同樣拷貝到<JDK>\lib目錄下。然而在真正運(yùn)行使用串口包的時(shí)候,僅作這些是不夠的。因 為通常當(dāng)運(yùn)行“java MyApp”的時(shí)候,是由JRE下的虛擬機(jī)啟動(dòng)MyApp的。而我們只復(fù)制上述文件到JDK相應(yīng)目錄下,所以應(yīng)用程序?qū)?huì)提示找不到串口。解決這個(gè)問題的 方法很簡(jiǎn)單,我們只須將上面提到的文件放到JRE相應(yīng)的目錄下就可以了

到這一個(gè)可以java 串口開發(fā)環(huán)境就搭建完成了

確認(rèn)本機(jī)可以使用的串口:

package test;

import java.util.Enumeration;
import java.util.HashMap;

import javax.comm.CommPortIdentifier;
import javax.comm.SerialPort;

public class GetSerialPorts {

  public void listPortChoices() {
    CommPortIdentifier portId;
    Enumeration en = CommPortIdentifier.getPortIdentifiers();
    // iterate through the ports.
    while (en.hasMoreElements()) {
      portId = (CommPortIdentifier) en.nextElement();
      if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
        System.out.println(portId.getName());
      }
    }

  }

  public static void main(String[] args) {

    GetSerialPorts GSP = new GetSerialPorts();
    GSP.listPortChoices();

  }

}



打開串口,關(guān)閉串口:

package test;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.HashMap;

import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.UnsupportedCommOperationException;

public class GetSerialPorts {

  private CommPortIdentifier portId;

  private SerialPort testPort;

  private CommPortIdentifier myPort;

  private InputStream is;

  private OutputStream os;

  public void listPortChoices() {

    Enumeration en = CommPortIdentifier.getPortIdentifiers();
    // iterate through the ports.
    while (en.hasMoreElements()) {
      portId = (CommPortIdentifier) en.nextElement();
      if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
        System.out.println(portId.getName());
      }
      myPort = portId;// 任意取一個(gè)串口,比如com1
    }

  }

  public boolean openPort() {
    try {
      testPort = (SerialPort) myPort.open("COM1", 500);// 注意這里必須換成一個(gè)真實(shí)的串口
      try {
        this.testPort.setSerialPortParams(38400, SerialPort.DATABITS_8,
            SerialPort.STOPBITS_1, SerialPort.PARITY_EVEN);
      } catch (UnsupportedCommOperationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      try {
        this.testPort.enableReceiveTimeout(30);
      } catch (UnsupportedCommOperationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      this.testPort.setOutputBufferSize(1024);
      this.testPort.setInputBufferSize(1024);

      try {
        this.is = this.testPort.getInputStream();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      try {
        this.os = this.testPort.getOutputStream();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      this.testPort.notifyOnDataAvailable(true);
      this.testPort.notifyOnOutputEmpty(true);
      this.testPort.notifyOnBreakInterrupt(true);

      // this.printerPort.addEventListener(new PrintPortListener(is));
      System.out.println("打開com1機(jī)串口成功");
      return true;
    } catch (PortInUseException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      return false;
    }

  }

  /**
   * TODO 關(guān)閉端口
   * 
   * @param
   * @return Map
   * @throws
   */
  public boolean closePort() {
    // TODO Auto-generated method stub
    try {
      if (null != this.testPort) {
        is.close();
        os.close();
        this.testPort.close();
      }
      System.out.println("關(guān)閉COM1串口成功");
      return true;
    } catch (Exception e) {
      // TODO Auto-generated catch block
      // e.printStackTrace();
      System.out.println("關(guān)閉COM1串口失敗");
      return false;
    }
  }

  public static void main(String[] args) {

    GetSerialPorts GSP = new GetSerialPorts();
    GSP.listPortChoices();
    GSP.openPort();

  }

}

讀數(shù)據(jù):

/**
   * TODO 接收端口數(shù)據(jù)
   * 
   * @param InputStream
   * @return String
   * @throws
   */
  public String readData(InputStream is) {
    // 讀取緩沖區(qū)域
    byte[] readBuffer = new byte[4096];
    int readDataLength = 0;
    try {
      readDataLength = is.read(readBuffer);
      // for (byte b : readBuffer) {
      // System.out.print(b);
      // }
      // System.out.println();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      return null;
    }
    // 將真實(shí)數(shù)據(jù)保存到零時(shí)數(shù)組中
    byte[] readTemp = new byte[readDataLength];
    for (int i = 0; i < readDataLength; i++) {
      readTemp[i] = readBuffer[i];
    }

    // 將byte數(shù)組轉(zhuǎn)換為16進(jìn)制字符串
    String stringTemp = FeelTheBase.bytesToHexString(readTemp);
    // System.out.println("指令返回值" + stringTemp);

    return stringTemp;

  }


感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

  • java新人基礎(chǔ)入門之遞歸調(diào)用

    java新人基礎(chǔ)入門之遞歸調(diào)用

    這篇文章主要給大家介紹了關(guān)于java新人基礎(chǔ)入門之遞歸調(diào)用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • Spring框架的ImportSelector詳細(xì)解讀

    Spring框架的ImportSelector詳細(xì)解讀

    這篇文章主要介紹了Spring框架的ImportSelector詳細(xì)解讀,Spring中一個(gè)非常重要的注解@Import中的ImportSelector接口的作用以及它到底有啥作用,也會(huì)捎帶一部分源碼說一下DeferredImportSelector是干啥的,需要的朋友可以參考下
    2024-01-01
  • Java的ConcurrentHashMap中不能存儲(chǔ)null的原因解析

    Java的ConcurrentHashMap中不能存儲(chǔ)null的原因解析

    眾所周知,在Java中Map可以存儲(chǔ)null,而ConcurrentHashMap不能存儲(chǔ)null值,那么為什么呢?今天通過源碼分析給大家詳細(xì)解讀,感興趣的朋友一起看看吧
    2022-07-07
  • 詳解Java數(shù)組的四種拷貝方式

    詳解Java數(shù)組的四種拷貝方式

    Java數(shù)組一共有四種拷貝方式: for循環(huán)、copyof/copyOfRange、arraycopy和clone。本文將為大家詳細(xì)介紹一下這四種方式,感興趣的可以了解一下
    2022-02-02
  • Mybatis中注解@MapKey的使用詳解

    Mybatis中注解@MapKey的使用詳解

    mybatis的原身是ibatis,現(xiàn)在已經(jīng)脫離了apache基金會(huì)。這篇文章主要介紹了Mybatis中注解@MapKey的使用的相關(guān)資料,需要的朋友可以參考下
    2016-10-10
  • 深入Java Final

    深入Java Final

    本篇文章,小編將為大家介紹Java Final,有需要的朋友可以參考一下
    2013-04-04
  • springboot 在xml里讀取yml的配置信息的示例代碼

    springboot 在xml里讀取yml的配置信息的示例代碼

    這篇文章主要介紹了springboot 在xml里讀取yml的配置信息的示例代碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • Java實(shí)現(xiàn)提取HTML文件中的文本內(nèi)容

    Java實(shí)現(xiàn)提取HTML文件中的文本內(nèi)容

    從?HTML?文件中提取文本內(nèi)容是數(shù)據(jù)抓取中的一個(gè)常見任務(wù),本文主要和大家分享了如何使用免費(fèi)?Java?API?從HTML?文件中提取文本內(nèi)容,需要的可以參考下
    2024-04-04
  • 最新評(píng)論