-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRXTX4Arduino.java
More file actions
39 lines (36 loc) · 1.12 KB
/
RXTX4Arduino.java
File metadata and controls
39 lines (36 loc) · 1.12 KB
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
import gnu.io.*;
import java.io.IOException;
import java.util.*;
public class RXTX4Arduino{
public static void main(String[] args){
Scanner in;
SerialPort sp;
CommPortIdentifier cpi;
int timeout = 1000;
int bitrate = 115200; // 9600 bps
try{
Enumeration ePorts = CommPortIdentifier.getPortIdentifiers();
// Use this line code one time for COM port discover
// ePorts.asIterator().forEachRemaining(id-> System.out.println((CommPortIdentifier) id).getName()));
while( ePorts.hasMoreElements() ){
cpi = (CommPortIdentifier)ePorts.nextElement();
if( cpi.getName().contains(" name_of_port ") ){
sp = (SerialPort)cpi.open(cpi.getName(), timeout);
break;
}
}
if( sp != null ){
sp.setSerialPortParams(bitrate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
in = new Scanner( sp.getInputStream() );
// shows data readed
while( in.hasNext() ){
System.out.println( in.nextLine() );
}
// close the connection
sp.close();
}
} catch (AWTException | PortInUseException | IOException | UnsupportedCommOperationException ex ){
System.out.println( ex.getMessage() );
}
}
}