import java.io.*;
import java.util.*;
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.io.FileDescriptor;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.awt.*;

public class Monitor implements Runnable
{   private static final long serialVersionUID = 8257386587L; 
	public Thread readThread;             // zaman içinde çalışma değişkeni
    public  CommPortIdentifier portId;    // seri ba?lant? kanal? numaras?
    public  SerialPort serialPort;        // seri kanal
    public  OutputStream outputStream;    // çıktı kanalı
    public  InputStream inputStream;      // girdi kanal?
    public  int sleep1;                   // readThread bekleme zamanı milisaniye
    //kontrol parametresi
    public Monitor(String s)
    { portId=portId(s);
    System.out.println("portID is read");
      try { serialPort = (SerialPort)
            portId.open(s, 2000);
                    } catch (gnu.io.PortInUseException e) {}
                    try {
                        outputStream = serialPort.getOutputStream();
                    } catch (IOException e) {}
                    try {
                        serialPort.setSerialPortParams(9600,
                            SerialPort.DATABITS_8,
                            SerialPort.STOPBITS_1,
                            SerialPort.PARITY_NONE);
                    } catch (gnu.io.UnsupportedCommOperationException e) {}
    }

    //seri ileti?im kanal? aç?lmas?
    public  CommPortIdentifier portId(String s)
    {    portId = PortChooser.select_port(null);            
                    try {
                        serialPort = (SerialPort)
                            portId.open(s, 2000);
                    } catch (gnu.io.PortInUseException e) {}
    return portId;
    } 
    public  void start() 
    {readThread = new Thread(this);
    readThread.start();
    }
    public void run() 
     {  
      int k=0;
	  while(true)
	  {	  
      try{
	  k++;	 	 
      Thread.sleep(1000); 
      try {
      inputStream = serialPort.getInputStream();
                    byte[] readBuffer = new byte[30];
                    long[] readBuffer1 = new long[30];
                    byte numBytes = (byte)inputStream.read(readBuffer);
                    //System.out.println("numBytes="+numBytes);
                    //String s= new String(readBuffer, "US-ASCII");
                    String s= new String(readBuffer);
                    int i=0;
                    for(i=0;i<numBytes;i++)
                    {  readBuffer1[i]=readBuffer[i]&0xFF; 
                     //readBuffer1[i]=readBuffer[i]; 
                    //System.out.print("i = "+i+"*");    
	                //System.out.printf("%x ",readBuffer[i]);
	                }
	                //System.out.println();
      System.out.println(s);
      } catch (IOException e) {}
      } catch (InterruptedException e) {System.out.println("InterruptedException 1");}
      }
      }
      public static void main(String arg[])
      { Monitor m=new Monitor("ARDUNIO");
      m.start();}
}
