最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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

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

特拿出來(lái)和大家一起分享一下。

準(zhǔn)備工作:

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

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

win32com.dll

comm.jar

javax.comm.properties

按照說(shuō)明配置好環(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è)問(wèn)題的 方法很簡(jiǎn)單,我們只須將上面提到的文件放到JRE相應(yīng)的目錄下就可以了

到這一個(gè)可以java 串口開(kāi)發(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();

  }

}



打開(kāi)串口,關(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("打開(kāi)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)文章

  • 解決Mybatis-Plus更新方法不更新NULL字段的問(wèn)題

    解決Mybatis-Plus更新方法不更新NULL字段的問(wèn)題

    這篇文章主要介紹了解決Mybatis-Plus更新方法不更新NULL字段的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • mybatis plus條件構(gòu)造器queryWrapper、updateWrapper

    mybatis plus條件構(gòu)造器queryWrapper、updateWrapper

    這篇文章主要介紹了mybatis plus條件構(gòu)造器queryWrapper、updateWrapper,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Java實(shí)現(xiàn)游戲抽獎(jiǎng)算法

    Java實(shí)現(xiàn)游戲抽獎(jiǎng)算法

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)游戲抽獎(jiǎng)算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • Java虛擬機(jī)處理異常的最佳方式

    Java虛擬機(jī)處理異常的最佳方式

    這篇文章主要給大家介紹了關(guān)于Java虛擬機(jī)處理異常的最佳方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • eclipse maven 插件的安裝和配置詳解

    eclipse maven 插件的安裝和配置詳解

    這篇文章主要介紹了eclipse maven 插件的安裝和配置詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • 詳解Spring Security中的HttpBasic登錄驗(yàn)證模式

    詳解Spring Security中的HttpBasic登錄驗(yàn)證模式

    HttpBasic登錄驗(yàn)證模式是Spring Security實(shí)現(xiàn)登錄驗(yàn)證最簡(jiǎn)單的一種方式,也可以說(shuō)是最簡(jiǎn)陋的一種方式,這篇文章主要介紹了Spring Security的HttpBasic登錄驗(yàn)證模式,需要的朋友可以參考下
    2019-11-11
  • Java調(diào)取創(chuàng)藍(lán)253短信驗(yàn)證碼的實(shí)現(xiàn)代碼

    Java調(diào)取創(chuàng)藍(lán)253短信驗(yàn)證碼的實(shí)現(xiàn)代碼

    這篇文章主要介紹了Java調(diào)取創(chuàng)藍(lán)253短信驗(yàn)證碼的實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2018-04-04
  • mybatis連接mysql的實(shí)現(xiàn)過(guò)程

    mybatis連接mysql的實(shí)現(xiàn)過(guò)程

    通過(guò)配置Maven的pom文件,可以簡(jiǎn)化MyBatis連接數(shù)據(jù)庫(kù)的過(guò)程,免去手動(dòng)下載和導(dǎo)入各種依賴(lài)包的麻煩,本文介紹了如何利用Maven導(dǎo)入MyBatis及其他相關(guān)依賴(lài),如Junit、MySQL連接驅(qū)動(dòng)、Druid連接池和Dbutil等,以簡(jiǎn)化數(shù)據(jù)庫(kù)操作和測(cè)試
    2024-10-10
  • SpringBoot配置Spring?Security的實(shí)現(xiàn)示例

    SpringBoot配置Spring?Security的實(shí)現(xiàn)示例

    本文主要介紹了SpringBoot配置Spring?Security的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-10-10
  • SpringCloud基本Rest微服務(wù)工程搭建過(guò)程

    SpringCloud基本Rest微服務(wù)工程搭建過(guò)程

    這篇文章主要介紹了SpringCloud基本Rest微服務(wù)工程搭建,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01

最新評(píng)論

靖江市| 共和县| 平阳县| 雷山县| 平湖市| 合阳县| 浑源县| 新竹市| 孝感市| 商河县| 霍林郭勒市| 濮阳县| 新宾| 南陵县| 文成县| 白朗县| 禄丰县| 武功县| 古丈县| 攀枝花市| 资兴市| 天气| 榆社县| 丹阳市| 姚安县| 紫云| 河池市| 芜湖市| 黔西县| 遂宁市| 山丹县| 正镶白旗| 扶沟县| 怀安县| 哈尔滨市| 五原县| 遂溪县| 禄劝| 富平县| 邻水| 博兴县|