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 Monitor3 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ı numarası
    public  SerialPort serialPort;                // seri kanal
    public  OutputStream outputStream;    // çıktı kanalı
    public  InputStream inputStream;        // girdi kanal?
    public  int sleep1;                               // readThread bekleme zaman? milisaniye
    public tplot2D d3;
    public dials d1;
    double T1;
    //kontrol parametresi
    public Monitor3(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) {}
    dials();                   }

    //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(100); 
      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]);
	                }
	                if(s.matches(s2))
	                {  
		                T1=Double.parseDouble(s);}
	                else
	                {T1=0.0;}
	                double T2[]={T1};
	                d1.setData(T2);
	                System.out.print("T1="+T1);
      } catch (IOException e) {}
      } catch (InterruptedException e) {System.out.println("InterruptedException 1");}
      }
      }
public void dials()
{ double n1=Math.PI/2;
double n2=3.0*n1;
String st[]={" Voltage V "};
double td[]={0};
double tmin=0;
double tmax=5.0;
double tetamax=100;
double teta1_rad=Math.PI/4;
double teta2_rad=3.0*Math.PI/2.0+teta1_rad;
d1=new dials("Arduino analog read","Voltage V",st,td,tmin,tmax,teta1_rad,teta2_rad);
FrameGraphic.plot("Voltmetre ",d1);
}
      public static void main(String arg[])
      { Monitor3 m=new Monitor3("ARDUNIO");
       m.start();
       }
}
