用java讀取microbit寫到serial的資料

URL Link //n.sfs.tw/14876

2020-12-23 11:53:58 By igogo

 

 

 

 

使用jSerialComm 函示庫

 

1
2
3
4
5
<dependency>
        <groupId>com.fazecast</groupId>
        <artifactId>jSerialComm</artifactId>
        <version>2.6.2</version>
    </dependency>

 

利用 SerialPort.getCommPorts()

找到接micro:bit的com

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
SerialPort com = SerialPort.getCommPorts()[0];
    com.setComPortParameters(115200,8,1,10);
    com.setComPortTimeouts(SerialPort.TIMEOUT_SCANNER,0,0);
 
    com.openPort();
 
    InputStream in = com.getInputStream();
 
    byte[] buffer = new byte[com.bytesAvailable()];
 
    String message = "";
    for (int i = 0; i < 30; i++) {
        int len = in.read(buffer);
        if (len > 0) {
            message = new String(buffer);
            System.out.println(message);
        }
        Thread.sleep(1000);
        len = 0;
 
    }
 
 
    in.close();
    com.closePort();
}

 

結果

1
2
3
4
5
6
7
8
9
10
11
1131431441461491501491491491491501491491491501501501511511511501501511511511521511521511521521511521511511511511521511531521521521521531511521511521501511521531521511521531521531531511531521521521511521511501521521531521521521511521521511511521521531521531531531531
5215215315315215215215215215215115215215315315215215115115215315215215215215215315315415315215315115115215315315215215315315415315315415415315415415415315315415415515315315315415415415415415415415415415415415515315415415415415415415415615415515515415415415515515415
51551551541541541541541551551<DAPLink:Overflow>
 
1121431441461491501491491491491501491491491501501501511511511501501511511511521511521511521521511521511511511511521511531521521521521531511521511521501511521531521511521531521531531511531521521521511521511501521521531521521521511521521511511521521531521531531531531
5215215315315215215215215215215115215215315315215215115115215315215215215215215315315415315215315115115215315315215215315315415315315415415315415415415315315415415515315315315415415415415415415415415415415415515315415415415415415415415615415515515415415415515515415
51551551541541541541541551551<DAPLink:Overflow>
 
1131431441461491501491491491491501491491491501501501511511511501501511511511521511521511521521511521511511511511521511531521521521521531511521511521501511521531521511521531521531531511531521521521511521511501521521531521521521511521521511511521521531521531531531531
5215215315315215215215215215215115215215315315215215115115215315215215215215215315315415315215315115115215315315215215315315415315315415415315415415415315315415415515315315315415415415415415415415415415415415515315415415415415415415415615415515515415415415515515415
51551551541541541541541551551<DAPLink:Overflow>

 

前三個數字就是方位值