 import java.io.*;
import java.util.*;
import gnu.io.CommPortIdentifier; 
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent; 
import gnu.io.SerialPortEventListener; 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.awt.*;
import javax.swing.*;
import java.awt.Graphics;
import java.awt.*;
import java.awt.event.*;
public class Monitor9 implements Runnable
{   String s2="[\\x00-\\x20]*[+-]?(((((\\p{Digit}+)(\\.)?((\\p{Digit}+)?)([eE][+-]?(\\p{Digit}+))?)|(\\.((\\p{Digit}+))([eE][+-]?(\\p{Digit}+))?)|(((0[xX](\\p{XDigit}+)(\\.)?)|(0[xX](\\p{XDigit}+)?(\\.)(\\p{XDigit}+)))[pP][+-]?(\\p{Digit}+)))[fFdD]?))[\\x00-\\x20]*";              
	private static final long serialVersionUID = 8257386587L; 
	public Thread readThread;             // zaman içinde çalışma değişkeni
    public  CommPortIdentifier portId;    // seri bağlantı kanal numaraı
    public  SerialPort serialPort;        // seri kanal
    public  OutputStream outputStream;    // çıktı kanalı
    public  InputStream inputStream;      // girdi kanalı
    public  int sleep1;                   // readThread bekleme zamanı milisaniye
    public textP d1;
    int angle;
    public Monitor9(String s)
    { portId=portId(s);
    System.out.println("portID is read");
    angle=0;
      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) {}
                    text();                   }

    //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(200); 
       byte[] readBuffer = new byte[8];
       byte numBytes; 
       String s="";
       try {      inputStream = serialPort.getInputStream();
                   outputStream = serialPort.getOutputStream();
             outputStream.write(d1.signal);;d1.jtf.setText("Ardinuo'dan geri okunan açı = "+d1.signal);
             Thread.sleep(1000); 
              numBytes = (byte)inputStream.read(readBuffer);   
		                 s= new String(readBuffer);  
		                 s=s.trim();	
		                 angle=Integer.parseInt(s);	                 	     
                        d1.jtf.setText("Ardinuo'dan geri okunan açı = "+angle);      
      } catch (IOException e) {}
      } catch (InterruptedException e) {System.out.println("InterruptedException 1");}
      }
      }
public void text()
{d1=new textP();
 FrameGraphic.plot("ARDUINO servo sensor",d1);
}
      public static void main(String arg[])
      { Monitor9 m=new Monitor9("ARDUNIO");
       m.start();
       }
}
