国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專(zhuān)注于服務(wù)器技術(shù)及軟件下載分享
分類(lèi)導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - java 串口通信詳細(xì)及簡(jiǎn)單實(shí)例

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

2020-07-25 15:06jingxian Java教程

這篇文章主要介紹了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ī)可以使用的串口:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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)閉串口:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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ù):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
   * 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ì)本站的支持!

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 黄久久久 | 亚洲清色| 国产免费色| 久久国产精品无码网站 | 精品久久av| 亚洲视频在线免费观看 | 欧美精品成人 | 亚洲国产精品一区二区第一页 | 九九视频在线 | 亚洲综合伊人 | 国产精品成人一区二区三区 | 中文字幕日韩在线 | 日韩中文字幕在线免费观看 | 久久天天躁狠狠躁夜夜躁2014 | 欧美精品国产精品 | 日本激情网 | 精品影院 | 91综合网| 天天干一干 | 欧美日在线 | 自拍偷拍专区 | 性色aⅴ免费视频 | 亚洲一区二区在线 | 国产一区二区三区免费 | 国产视频精品免费 | 91av国产视频 | 成人日韩| 色片视频免费 | 久久美女视频 | 精品国产黄a∨片高清在线 黄色大片aaaa | 国产亚洲精品美女久久久久久久久久 | 亚洲一区二区在线视频 | 另类视频在线 | 日韩综合一区 | 美女视频一区二区三区 | 欧美性猛交xxxx黑人猛交 | 日韩在线免费视频 | 久久国产精品久久久久久久久久 | 国产精品久久久久久久久久新婚 | 精品成人一区 | 亚洲+变态+欧美+另类+精品 |