package rxtx;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Win10RxTxTest {
Win10RxTx rxtx ;
void baca(){
Thread thread = new Thread(){
public void run(){
System.out.println("Thread Running");
}
};
thread.start();
}
void baca2(){
Thread thread = new Thread(){
public void run(){
System.out.println("Thread Running22");
}
};
thread.start();
}
public static void main ( String[] args ) {
Win10RxTxTest x=new Win10RxTxTest();
x.baca();
System.out.println("OKAAA");
x.sambung();
x.lanjut();
System.out.println("OK");
x.baca2();
}
void sambung(){
try {
rxtx= new Win10RxTx();;
rxtx.connect("COM13");
} catch (Exception ex) {
Logger.getLogger(Win10RxTxTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
void lanjut(){
Thread readData = new Thread(new Runnable() {
@Override
public void run() {
String data;
while(true) {
if (Thread.interrupted()) break;
data = rxtx.read();
if (data != null) {
System.out.print(data);
//the real-time when data arrived
System.out.println(
(new SimpleDateFormat(" {HH:mm:ss}"))
.format(new Date()));
} else {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
break;
}
}
}
}
});
readData.start();
}
void sambungLock(){
try {
rxtx= new Win10RxTx();;
rxtx.connect("COM13");
} catch (Exception ex) {
Logger.getLogger(Win10RxTxTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
void lanjut2(){
Thread readData = new Thread(new Runnable() {
@Override
public void run() {
String data;
while(true) {
if (Thread.interrupted()) break;
data = rxtx.read();
if (data != null) {
System.out.print(data);
//the real-time when data arrived
System.out.println(
(new SimpleDateFormat(" {HH:mm:ss}"))
.format(new Date()));
} else {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
break;
}
}
}
}
});
readData.start();
//Define a input thread to get input from the user and send to RxTx
Thread userInput = new Thread(new Runnable() {
@Override
public void run() {
int c = 0;
byte[] buf = new byte[1204];
try {
while ( ( c = System.in.read(buf)) > -1 )
{
if (Thread.interrupted()) break;
//"---" is a string to terminate the program
if (c > 0) {
if (c==5 && buf[0]=='-'
&& buf[1]=='-' && buf[2]=='-') {
break;
}
//"+++" is a special string sent to RxTx
if (c==5 && buf[0]=='+'
&& buf[1]=='+' && buf[2]=='+') {
c = 3;
}
rxtx.write(Arrays.copyOf(buf, c));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
userInput.start();
//Normally the program is terminated by the user
//so we wait for the input thread to finish
userInput.join();
readData.interrupt();
readData.join();
}
}
Tidak ada komentar:
Posting Komentar