	         Screen 19
	
	Dim RXL As uByte  
    Dim RXH As uByte  
	Dim Wert As uShort
    Dim Werte (10000)As uShort
    Dim Werte_Temp (10000) As uShort
    Dim key As String
    Dim Temp As uShort 
    Dim Temp2 As uShort
    
    Dim Classes(1000) As uShort
    Dim Class_size As uByte

    Dim Token (10000)As uByte 
    Dim As String file_name
    Dim As Integer file_handle
    Dim As String ComPort
    
    Toleranz = 6
    
Input_:     
    Input "Bitte Com Port eingeben ",co   
    Cls
    
Start:	cnt = 0 :Rest_Zeiger = 0 :Zeiger = 0 :Class_size =0

    
    
    ComPort = "COM"& Str(co) &":38400,N,8,1,CS0,DS0,CD0,RS,BIN,ME"

	If (Open Com ( COMPort For Input As #1 ) <> 0 ) Then
   	Print "Fehler: Der Port konnte nicht geoeffnet werden!"
  	Goto Input_
	End If 
	
	Print"Taste an der Fernbedienung druecken, danach die SpaceTaste zum Auswerten"	 
'-----------Werte einlesen---------------------------------------	
Getloop:    If Eof (1) =0  Then: Get #1,,RXH: Goto Get_Low : End If 
            key = Inkey$
            If key = Chr$(32) Then: Close 1
            Goto Auswerten 
            End If
            If key = Chr$(27) Then:End:End If   
            Goto Getloop 
             
Get_Low:    If Eof (1) =0  Then: Get #1,,RXL:Goto Raus :End If
            key = Inkey$
            If key = Chr$(32) Then:Close 1
            Goto Auswerten 
            End If 
            If key = Chr$(27) Then:End:End If
            Goto Get_Low
            
Raus:       If RXH = 255 Then:cnt =0               :End If
            Werte(cnt)=256*RXH+RXL :cnt = cnt +1
            Print".";
            Goto getloop
            
'_________________________________________________________________            
Auswerten:  For x = 0 To cnt-1
            Werte_Temp(x)=Werte(x)
            Next x
            
            Gosub  sortieren  
        
Search_loop:            
            Temp_CNT = cnt-zeiger-1
            Gosub search
            If cnt-zeiger-1  >0 Then Goto  Search_loop :End If
            Classes(Class_size)=Werte(0)
           
            Gosub print_Classes
            Gosub Set_Tokens 
            Gosub Tabelle
            Gosub IR_Dump
            Goto Start
            

'-----------Werte sortieren-------------------------------------                         
sortieren:  For y = 0 To cnt-1
            For x = y To cnt-1
            If Werte_Temp(x)< Werte_Temp(y) Then
            Temp =  Werte_Temp(x)
            Werte_Temp(x) = Werte_Temp(y)
            Werte_Temp(y) = Temp
            End If
            Next x
            Next y
            Return
'---------------------------------------------------------------            
search:     Summe = 0 :a=0
            Low = Werte_Temp(0)-Toleranz
            high = Werte_Temp(0)+2*Toleranz
             ' Print low , high 
              'Print"------------------------"
            For x= 0 To Temp_cnt-1 'alle Werte
            If Werte_Temp(x) < high Then
                If Werte_Temp(x) >low Then:
                zeiger = zeiger+1 :a=a+1:Summe = Summe + Werte_Temp(x)
                End If
            End If
            Next x 
            Durchschnitt = Summe /a
            Classes(Class_size)= Durchschnitt
            Class_size = Class_size +1
            
            For x = 0 To cnt-zeiger-1
            Werte_Temp(x) = Werte_Temp(zeiger+x)
            Next x
            Rest_Zeiger =Rest_Zeiger + cnt-zeiger           
            Return
'---------------print Classes-------------------------------------------------
print_Classes:  Cls
                Color 15
                Print"Token Zaehler", "  Zeit"
                For y = 0 To Class_size
                Color 14-y
                Print y;"     ";Classes(y),
                If (Classes(y)-0.1)*32 < 1000 Then: Print Using "#####.#"; (Classes(y)-0.1)*32;
                Print " uS": End If
                If (Classes(y)-0.1)*32 > 999  Then: Print Using "#####.#"; (Classes(y)-0.1)*32/1000; 
                Print " mS": End If
                Next y
                Color 15 
                Print "_____________________________________________________________________________________________"
                Return
'-------------Token Liste erstellen-------------------------------------------                
Set_Tokens:   For x = 0 To cnt
              For y = 0 To Class_size 
              Temp = Classes(y)+Toleranz
              If Werte(x)< Temp Then
              Token(x) = y: Goto next_Token
              End If
              Next y
next_Token:   Next x
              Return
              
'---------------------------------------------------------------               
Tabelle:        a=14    
                b=0 :c=0
                For x = 0 To  cnt   Step 2
                Color a-Token(X)
                Print Token(X);" "; 
                Color a-Token(X+1)                 
                Print Token(X+1);" ";
                Color 15
                Print " ";
                b=b+1 
                If b = 19 Then:b=0:Print
                For d = c To c+18
                Print Using "###";c*2;
                Print "  ";:
                c=c+1:Next d 
                Print
                Print "_____________________________________________________________________________________________"
                End If
                Next x 
                Print
                Return
                
'---------------IR Dump erstellen--------------------------------------
IR_Dump:                
  	file_name = "IR_Dump.asm"  
   	file_handle = FreeFile()  
   	If( Open( file_name For Output As #file_handle ) ) Then
   	Print "ERROR: opening file " ; file_name
    	End -1:End If
    	    'Print #file_handle,".equ IR_max = ";(Classes(Class_size-1)/256)+2
    	    'Print #file_handle,Chr$(9);"Obergrenze"
            Print #file_handle,"IR_Tab:";Chr$(9);".db ";
                                         
            'For y = 0 To Class_size
            'If  Classes(y)>255 Then Goto next_high :End If
            'Print #file_handle, Classes(y)+Toleranz;
            'Next y 
            
            For y = 0 To Class_size
            If  Classes(y)+Toleranz <256 Then: Print #file_handle, Classes(y)+Toleranz;",";
            Else Goto Last_Low 
            End If 
            Next y        
Last_Low:   For x= y To Class_size-2
            Print #file_handle, Int(Classes(x)/256)+1;",";
            Next x
            a=x And 1
            If a=1 Then: Print #file_handle, Int(Classes(x)/256)+1 :End If
            If a=0 Then: Print #file_handle, Int(Classes(x)/256)+1;",0" :End If
         
                       
             Print #file_handle,".EQU";Chr$(9); "IR_Low_size  = ";y 
             Print #file_handle,".EQU";Chr$(9); "IR_High_size = ";x
            'Print #file_handle,"-----------------------------------"
            
            'Print #file_handle,Chr$(9);";High Anteil"
            'Print #file_handle,Chr$(9);";  ";
            'For x = y To Class_size-1
            'Print #file_handle,Using"####";x;
            'Next x
            'Print #file_handle," "

            'Print #file_handle,"IR_Tab_H:";Chr$(9);
            'Print #file_handle,".db ";
            
            'a=0 
            'For x = y To Class_size-1
            'Print #file_handle, Using"###";Int(Classes(x)/256);
            'If x < Class_size-1 Then: Print #file_handle,","; :End If
            'a=a+1:Next x
            'temp = a And 1 
            'If temp = 1 Then: Print #file_handle,",0" :End If  
            
            'Print #file_handle,".EQU IR_MAX = ";a
            Print #file_handle," "
'---------- Messwerte ins File schreiben  --------------------------------------------
            Print #file_handle, "Ir_Werte:"; 
            For x = 0 To cnt-1
            
            Print #file_handle, Chr$(9);".dw ";"  ";
            w = Werte(x)
            If w <16   Then:  Print #file_handle, "0x000" ; Hex(w) :Goto next_w:End If 
            If w <256  Then:  Print #file_handle, "0x00"  ; Hex(w) :Goto next_w:End If   
            If w <4096 Then:  Print #file_handle, "0x0"   ; Hex(w) :Goto next_w:End If    
                              Print #file_handle, "0x"    ; Hex(w)           
next_w:     Next x 
            
            Close #file_handle
            Return
            
            
            
                             