国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java 串口通信詳細及簡單實例

2019-11-26 13:16:45
字體:
來源:轉載
供稿:網友

java 實現串口通信

最近做了一個與硬件相關的項目,剛開始聽說用java和硬件打交道,著實下了一大跳。java也可以操作硬件?

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

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

準備工作:

首先到SUN官網下載一個zip包:javacomm20-win32.zip

其中重要的有這幾個文件:

win32com.dll

comm.jar

javax.comm.properties

按照說明配置好環境,如下:

將win32com.dll復制到<JDK>/bin目錄下;將comm.jar復制到<JDK>/lib;把 javax.comm.properties也同樣拷貝到<JDK>/lib目錄下。然而在真正運行使用串口包的時候,僅作這些是不夠的。因 為通常當運行“java MyApp”的時候,是由JRE下的虛擬機啟動MyApp的。而我們只復制上述文件到JDK相應目錄下,所以應用程序將會提示找不到串口。解決這個問題的 方法很簡單,我們只須將上面提到的文件放到JRE相應的目錄下就可以了

到這一個可以java 串口開發環境就搭建完成了

確認本機可以使用的串口:

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();  }}

打開串口,關閉串口:

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;// 任意取一個串口,比如com1    }  }  public boolean openPort() {    try {      testPort = (SerialPort) myPort.open("COM1", 500);// 注意這里必須換成一個真實的串口      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機串口成功");      return true;    } catch (PortInUseException e) {      // TODO Auto-generated catch block      e.printStackTrace();      return false;    }  }  /**   * TODO 關閉端口   *    * @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("關閉COM1串口成功");      return true;    } catch (Exception e) {      // TODO Auto-generated catch block      // e.printStackTrace();      System.out.println("關閉COM1串口失敗");      return false;    }  }  public static void main(String[] args) {    GetSerialPorts GSP = new GetSerialPorts();    GSP.listPortChoices();    GSP.openPort();  }}

讀數據:

/**   * TODO 接收端口

主站蜘蛛池模板:
林西县|
泰兴市|
丰镇市|
眉山市|
温州市|
通榆县|
手机|
三亚市|
甘泉县|
潞城市|
嘉禾县|
扎囊县|
迭部县|
永安市|
文安县|
阿拉善盟|
四平市|
涞源县|
河南省|
竹溪县|
永和县|
河北区|
佛坪县|
桦甸市|
碌曲县|
托里县|
嘉鱼县|
克拉玛依市|
纳雍县|
屯昌县|
南岸区|
涿州市|
六枝特区|
宁河县|
泸西县|
保亭|
鹤壁市|
鄯善县|
炉霍县|
渑池县|
西乌|