'10 juillet 2019
' ' Trame avec New compteur Linky :
' *C021961176859*;*W00000017193*;*K17.193*;*E 27.06*;*A00002*;*G00002*;*T 19.88*;*H=1731*;*R=MCHHH*;


' 09 mai 2019
' Rajout PICTime  HHMM de la RTC  pour comparer avec heure PC
' et eventuellement faire une MAJRTC
' labelx="Pictime :"+AX$

'06 mai 2019
' reinit BlueTooth via zone emission
' $$$     envoi
 ' C <CR>  envoi

'05-mai 2019
' modif  pour affich Temper exterieur


 '01-mai-2019
 ' modif MAJ RTC au format   A$="U;30;04;19;20;12;06;#"  was A$="#U;10;11;18;20;12;06;&"
 ' modif voyant_Arret -> voyant Stop Rouge cumulus

' test 30 avril 2019  ( trame EDF simulée)
' Application MikroC
' Directory "C:\_MikroC\_MesProjets_MikroC\_18F46K22_EDF_Info_et_Controle_Chauffage_2019"
' Source "_18F46K22_EDF_Info_et_Controle_Chauffage_RN41_"
' Projet "18F46K22_EDF_Info_et_Controle_Chauffage_1905.mcppi"
' Eeprom "18F46K22_EDF_Info_et_Controle_Chauffage_Eeprom_190504.ihex"
' Hardware "..."
' FOSC_INTERNE  "PIC18F46k22_FOSC_interne_8x4-32MHZ.cfgsch"
' POWER_SUPPLY  " 3,6V to 5V"
' FOSC "32MHz"
'
'circuits imprimés  MCU  18F46K22 + Interface fil pilotes
' RN41-EEEC slave sur carte MCU
' VBRAY + script  18F46K22_EDFinfo_BT_190430.tsc sur UART2  carte MCU
' COM16 renomé COM2 ( breadboard + CH340+RN41-DB49
' Remis programme de chauff dans EEprom du PIC
' CHAUFF 511 170 234 491 491 507 319 003 003 087 501 507 234 234 234 234 234 298 319 060 063 032 235 490
 


' modif EDF_BT.ini
' format EDF_BT .INI
' 35		' top windows
' 716	 	' left Window
' 4	 	'19200 bds
' 0		' NO parity
' 1	 	' Nb datas 8
' 0	 	' 1 stop
' 1	 	' COM#  numero de COM-1 (donc COM2)
' 55114000 ' dernier relevé EDF   was 52958000
' 0.175	 ' cout  KWH   (was 0,161!)


'20-01-2019
' affichage OK etat des cdes par images BMP
' Qimage probleme: passage variables locales   dim cc$  et  dim c1$ en GLOBAL'

'17-01-
' rajout indicateur graphique etat radiateurs via QImage
' BAD ....

'16-01-2019
' envoi profil de chauff seulement apres retour sur Frame pincipale EDF_BT

'15-01-2019
' envoi profil de chauffe byet par byte

'14-01-2019
' envoi profilchauff  string

'12-01-2019
' gestion de 2 fenetrees ( 2 applis)
' EDF_BT et QGrid data table de consignes fils pilotes


 '09-01-2019


' 26-12-2018
' rajout  \DATAS pour chemin des fichiers de donnees
' modif : validation des parametre Last_Rele et prix Kwh validé par coche  "Valid"

''16-12-2018
  ' rajout jour calendaire
  ' recup de clui ci dans Joursemaine()

 '15-12-2018
 ' modif pour Grafique Glissant

'14-12-2018
' rajout temperature moyenne avec KWH journalier
' avec simulation :  Vbray_Script_Simulation_envoi_Trame_EDF_181214.tsc

' en reel
' Const LongMini=100

'08/12/2018
'reelle
'*C050222059498*;*W00050714221*;*K    1549.221*;*R00011*;*A00005*,*G00005,00011*;*T  11.8*;*O  49165000*
'ou simulée avec TRAME FIXE
'Vbray_macro_simule_1_Trame_EDF_181207.tmf
'MACRO
'C050222059498*;*W00051714221*;*K    1549.221*;*R00011*;*A00005*,*G00005,00011*;*T  11.8*;*O  49165000*#013


'Edfinfo_Vbray_scipt_Trame_simulee_181208.tsc
'simulee
'*C050222059498*;*W00049904164*;*K 739.164*;*R19*;*A00009*,*G00009,19*;*T19.0*;*O 49165000*


$Apptype GUI
Application.Title = "EdfInfo_via_BT"

$TYPECHECK ON
$OPTIMIZE ON
$INCLUDE "D:\RapidQ\_BT_Edfinfo\inc\My_QGridEx_190117.inc"
$Include "D:\RapidQ\_BT_Edfinfo\inc\Rapidq2.inc"

$INCLUDE "D:\RapidQ\_BT_Edfinfo\inc\MyQChart.inc"

' convertisseur image -> icone
' http://image.convertstandard.com/ToICO32x32.aspx
 $option Icon "icon\mylogo.ICO"
 '   ICON file required 766 bytes.
 

'----- Constants
$IFNDEF TRUE
  $DEFINE TRUE 1
$ENDIF
$IFNDEF FALSE
  $DEFINE FALSE 0
$ENDIF
 
 $RESOURCE LEDR_OFF as "D:\RapidQ\_BT_Edfinfo\_BMP\Voyant_Stop.bmp"        'cumulus
 $RESOURCE LEDR_ON  as "D:\RapidQ\_BT_Edfinfo\_BMP\Voyant_Marche.bmp"      'cumulus
 $RESOURCE ARRET as "D:\RapidQ\_BT_Edfinfo\_BMP\ARRET.bmp"
 $RESOURCE CONFORT  as "D:\RapidQ\_BT_Edfinfo\_BMP\CONFORT.bmp"
 $RESOURCE ECO  as "D:\RapidQ\_BT_Edfinfo\_BMP\ECO.bmp"
 $RESOURCE HGEL  as "D:\RapidQ\_BT_Edfinfo\_BMP\HGEL.bmp"
 

  Const Version= "11-07-2019 (Win10)"


  '01-12-2018
  ' rajout stockage Temperature amb1 journaliere
  ' usage de valeur "dernier releve EDF" pour afficher la conso en KWH
  ' dernier relevé EDF , en fin de EDF_BT.Ini
  ' remplacé Msgbox bloquant par un label affiché pendant 5sec

 '23-11-2018  rajour QCHART  Grafique Amperes
 ' et stockage Amps moyen par minute

 ' 21-11-2018
 ' 4 plantage ecran bleu de windows avec probleme de Signature driver COM Prolific
 ' impossible de redemerrer
 ' apres mise hirs tension pendant 10mn => redemarrage OK   driver prolifics OK ???
 ' Reception OK sur COM3 .. mais impossible avec COM16, 17,19
 '

' 17-11-2018 ne marche plus !!
' redvient OK en triturant le port COM3, driver, passage sur COM17 puis retour sur COM3 => OK
' pas clair !!!!

'16-11

' melasse dans les Qbutton
' rajout option +CR ou +LF sur envoi message
' recup valeur Temp Ambiante et amperes ..OK


' trame emise par PIC16F1847+RN416DB49 (esclave)
' trame PIC16F Edfinfo : longueur = 103 caracteres, émises toutes les 1,2 sec
' reçue coté PC par RN41-EEEC  (maitre)
'*C050222059498*;*W00050714221*;*K    1549.221*;*R00011*;*A00005*,*G00005,00011*;*T  11.8*;*O  49165000*

'Problemo 13-11-2018
' Que des zeros dans l'echange de datas ????
' verif avec SDA Anlayser  Stat et Stop bit OK mais Datas toutes à 0 ??
' recherche de bug ..en long et enlarge ....rien
' test direct  cables croisés :  COM3 TX-> COM17 RX  et COM17 TX-> COM3 RX
'  entre les 2 terminaux COM3 et COM17 ..OK
'  mais suite à une MAJ de windows (intempestive et impossible à bloquer)
' check des drivers Prolifics ..  un driver sur COM3 a été modifié par une
' version prolific  pilote du 12/07/2018  au lieu de pilote 17/07/2009
' modif manuelle du pilote et redemearrage
' test Echange datas entre programme (COM3) et terminal COM17 ..OK
'

'rev 12 nov 2018
' correction bug sur reception
' rajout timeout reception apres 15 secondes ( 30x500mS)
' rajout ruban progression buffer reception
' Rajout commande MAJ RTC .. au format trame appli PIC18F
'reste PB sur DayofWeek

'rev 11 nov 2018

'fichier *.ini pour setup COM

'177    ' Top window
'1237   ' left Window
'4      ' 19200 bds
'0      ' NO parity
'1      ' Nb datas 8
'0      ' 1 stop
'2      ' COM3



 '----- Win API Function Declarations
DECLARE FUNCTION SetWindow LIB "user32" ALIAS "SetWindowLongA"_
                 (hwnd AS LONG, nIndex AS LONG, dwNewLong AS LONG) AS LONG
'----- SUBs
Sub SetMin (Form as QForm)          'Allow RQ App to Minimize to Taskbar
  Setwindow(Form.Handle, -8, 0)
  Setwindow(Application.Handle, -8, Form.Handle)
End Sub



Declare Function OPENCOM Lib "Dll\RSCOM.dll" alias "OPENCOM"(ByVal OpenString$) As Integer  'ouvre le port
Declare Sub CLOSECOM Lib "Dll\RSCOM.dll"alias "CLOSECOM" ()                                 'fermer le port com
Declare Sub TXD Lib "Dll\RSCOM.dll" alias "TXD" (ByVal b%)      'broche 3 envoie, Emission de données
Declare Sub DTR Lib "Dll\RSCOM.dll" alias "DTR" (ByVal b%)      'broche 4 envoie, Terminal prêt.
Declare Sub RTS Lib "Dll\RSCOM.dll" alias "RTS" (ByVal b%)      'broche 7 envoie, Demande d'emmission
Declare Function DCD Lib "Dll\RSCOM.dll" alias "DCD" As Integer 'broche 1 lecture, Dectection de porteuse
Declare Function DSR Lib "Dll\RSCOM.dll" alias "DSR" As Integer 'broche 6 lecture, Emmission prête
Declare Function CTS Lib "Dll\RSCOM.dll" alias "CTS" As Integer 'broche 8 lecture, Prêt à emettre
Declare Function RI Lib "Dll\RSCOM.dll"  alias "RI"  As Integer 'broche 9 lecture, Indicateur de sonnerie
Declare Sub CLEARBUFFER Lib "Dll\RSCOM.dll" alias "CLEARBUFFER"()             'Nettoye le buffer en entrée
Declare Function INBUFFER Lib "Dll\RSCOM.dll" alias "INBUFFER" () As Integer  'Nombre de caractère dans le buffer d'entré
Declare Function OUTBUFFER Lib "Dll\RSCOM.dll" alias "OUTBUFFER" () As Integer'Nombre de caractère dans le buffer de sortie ?
Declare Sub BUFFERSIZE Lib "Dll\RSCOM.dll" alias "BUFFERSIZE" (ByVal b%)      'Change la taille du buffer Taille du buffer
Declare Sub DELAY Lib "Dll\RSCOM.dll" alias "DELAY" (ByVal ms As Double)     'delai en micro seconde // duree d'un bit
Declare Function INPUTS Lib "Dll\RSCOM.dll" alias "INPUTS" () As Integer           '?
Declare Sub REALTIME Lib "Dll\RSCOM.dll" alias "REALTIME" ()                       '?
Declare Function READSTRING Lib "Dll\RSCOM.dll" alias "READSTRING" () As String    'Relire une chaine
Declare Function READBYTE Lib "Dll\RSCOM.dll" alias "READBYTE" As Integer          'Lire un caratère
Declare Sub NORMALTIME Lib "Dll\RSCOM.dll" alias "NORMALTIME" ()
Declare Sub SENDBYTE Lib "Dll\RSCOM.dll" alias "SENDBYTE" (ByVal Dat%) 'Envoyer un caratère
Declare Sub SENDSTRING Lib "Dll\RSCOM.dll" Alias "SENDSTRING" (ByVal Dat$)'Envoyer une chaine
Declare Sub TIMEINIT Lib "Dll\RSCOM.dll" alias "TIMEINIT" ()                 'Reinit le temps
Declare Sub TIMEOUTS Lib "Dll\RSCOM.dll" alias "TIMEOUTS" (ByVal b%)  'depassement du temps ?

'Declare Function TIMEREAD Lib "Dll\RSCOM.dll" alias "TIMEREAD" () As Double  'donne le temps d'ouverture du port ?
Declare sub Activer_Port_COM()

Declare Function Trim(Datum As String) As String
Declare Function JourSemaine() AS INTEGER
Declare Sub Initialise()



declare SUB JourCalendaire
'declare Sub Decortique_Date(x$ as string)


Declare sub Message(Situation as single)
Declare Sub EDF_BTShow(Sender as QForm)
Declare Sub EDF_BTClose(Action AS INTEGER)

'  4 boutons
Declare Sub Send_ButtonClick(Sender as QCoolBtn)
Declare Sub Help_buttonClick(Sender as QCoolBtn)
Declare Sub MAJ_RTCClick(Sender as QCoolBtn)

Declare Sub SortieClick(Sender as QButton)
Declare Sub RAZ_ReceptionClick(Sender as QButton)
Declare Sub Raz_Zone_EmissionClick(Sender as QButton)
Declare Sub Raz_Zone_DebugClick(Sender as QButton)

' choix parametre COM

Declare Sub Port_Combo1Change(Sender as QComboBox)
Declare Sub Speed_Combo2Change(Sender as QComboBox)
Declare Sub Parity_Combo3Change(Sender as QComboBox)
Declare Sub Nb_Datas_Combo4Change(Sender as QComboBox)
Declare Sub Nb_StopChange(Sender as QComboBox)

Declare SUB Read_COMx()
Declare SUB TimerOver()


Declare Sub Wh_NowClick(Sender as QLabel)
Declare Sub AmperesClick(Sender as QLabel)
Declare Sub Origine_CompteurClick(Sender as QLabel)



'================
Declare Sub EDF_BTResize (SENDER As QFORM)
declare Sub DrawOrig()
Declare Sub Initialise()
Declare Sub Save_EDFinfo_to_File



Declare Sub CoolBtn2Click(Sender as QCoolBtn)

Declare Sub RadioButton1Click(Sender as QRadioButton)
Declare Sub RadioButton2Click(Sender as QRadioButton)
'Declare Sub CheckPrixClick(Sender as QCheckBox)
Declare Sub Dernier_ReleveChange(Sender as QEdit)
Declare Sub Edit1_Prix_KWHChange(Sender as QEdit)
Declare Sub Edit1_Prix_KWHKeyPress(Key AS BYTE)

' =====================================================

' @@@ 18-01

'----- Form Event SUB Declarations
Declare Sub btnOnClick (SENDER As QBUTTON)
Declare Sub mnuEditClick (Sender AS QMenuItem)
Declare Sub mnuFileClick (Sender AS QMenuItem)
Declare Sub Form2Resize (SENDER As QFORM)
Declare Sub Form2Close
Declare Sub CheckSync


Declare Sub btCalculClick (SENDER As QBUTTON)

Declare Sub Recup_Date ()
Declare Sub Rafraichit_Icones



  Const Logiciel = "EDF_BT"
'  Const Vir = "EDF_BT.vir"
  Const Exe = "EDF_BT.exe"
  Const Ini = "EDF_BT.ini"
' Const Son = "Son\Alerte.wav"

Const LongMini=90
Const LongMaxi=120
const TIMEOUT_Trame=500

dim month$(12) as string
dim month(12) as integer    ' libell‚ et nb de jours des mois
dim JourSem$(8) as string              ' Jours de la semaine
data Lundi,Mardi,Mercredi,Jeudi,Vendredi,Samedi,Dimanche,Lundi
data "        ",0,"Janvier",31,"Fevrier",28,"Mars",31,"Avril",30,"Mai",31
data "Jun",30,"July",31,"Aout",31,"Septembre",30,"Octobre",31,"Novembre",30
data "Decembre",31


  Dim String_Comm As String
 ' DIM Information  AS STRING
  Dim Variable$
  Dim x as integer

  Dim infoCom As Integer
  Dim Com as string
  Dim Baud as string
  Dim Parite as string
  Dim Nb_Datas as string
  dim Nb_Stop  as string

  Dim Chemin as string
  Dim Deja as single
  Dim Fichier as QFilestream

  Dim Nombre as integer
  Dim A$ as string
 ' Dim B1$ as string
 
  dim cc$
  dim c1$
 
  DIM C$ as string
  dim Nbytes as integer
  dim Jt as integer         ' Timer1 count

  DIM dd$ as string
   DIM DOW$ as string
    Dim JourSem as integer
     Dim mm as integer
      Dim dd as integer
        Dim yy as integer


  DIM  i as integer
   Dim j as integer
    Dim k as integer
     Dim L1 as long
      dim L2 as integer



  Dim Trame_EDF$ as String
  Dim NumCompteur$ as string
  Dim NbWattActuel as long
  dim Last_Releve as single
  DIm OrigineCompteur as Long
  Dim Prix_KWH as single

' pour graphiques
  Dim Table(600) as single
  Dim Table_T(600) as single

  dim HH as integer
  dim Mn as integer
  dim ss as integer
  dim Nb_Trame as integer
  Dim Nb_Tics as integer
  dim Old_Nb_Mn as integer
  dim Nb_Mn as integer
  dim Old_HH as integer
  dim Numjour as integer  ' jour calendaire
 ' dim simulation as integer
            
  Dim KWh as Single
  Dim Amps as single
  Dim Amps_Moyen as single
  Dim TemperAmb as single        ' temper instantannée
  Dim TemperExt as single
  Dim Temper_Moyen as single      ' temper.moyenne sur 1 minute
  
  'valeur horaires
  Dim Nb_mes_Temp as integer
  DIm Temper_Heure as single


 DEFINT maxV = 6001
 DIM xdata(0 TO maxV) AS SINGLE
 DIM ydata(0 TO maxV) AS SINGLE
 DIM tdata(0 TO maxV) AS SINGLE
 DIM th AS SINGLE
 DIM n AS INTEGER
 DEFINT maxc = 10           'max columns
 DEFINT maxr = 20           'max rows
 DIM myTime  AS SINGLE
 
 Dim Index_Chart as Integer

'pour stockage dans fichiers
 Dim Amps_Store(1441) as single    ' 1 journéee de stockage
 Dim Temper_Store(1441) as single
 Dim KWH_Day_Store(25) as single
 Dim KWH_Year_Store(8786) as single   ' 24x366
 Dim OldNbWatt as  single
 Dim Delta_KWH as  single
 Dim Temper_Day(24) as single
 dim Datum$ as string

 Dim Once as integer
 Dim Drapeau_Save as integer
 Dim AllReady_Running as integer
 
 dim ProgChauff$
 dim FileChauff$

  
  'Dim Images(6) as QImage
  'Dim Image6 as QIMAGE
  'Dim Image5 as QImage
  
  'Images(6).BMP = "D:\RapidQ\_BT_Edfinfo\_BMP\LedR_off.bmp"
  ' Images(5).BMP= "D:\RapidQ\_BT_Edfinfo\_BMP\LedR_on.BMP"
  ' Images(4).BMP= "D:\RapidQ\_BT_Edfinfo\_BMP\ARRET.bmp"
  '   Images(3).BMP= "D:\RapidQ\_BT_Edfinfo\_BMP\CONFORT.bmp"
  '    Images(2).BMP= "D:\RapidQ\_BT_Edfinfo\_BMP\ECO.bmp"
  '     Images(1).BMP= "D:\RapidQ\_BT_Edfinfo\_BMP\HGEL.bmp"




Declare Sub CheckPrixClick(Sender as QCheckBox)
Declare Sub CheckBox1_TableClick(Sender as QCheckBox)

Declare SUB Read_COMx()





'================================

Create EDF_BT as QForm
      Caption = "EDF_BT"
      Top     = 96
      Left    = 225
      Width   = 1068
      Height  = 968
      OnShow = EDF_BTShow
      OnClose = EDF_BTClose
      BorderStyle = 1
      DelBorderIcons = 2
   '   Icon = "icon\mylogo.ico"
     ' ShowHint = True
END CREATE

 Create StatusBar1 AS QStatusBar
    parent=EDF_BT
      SizeGrip = False
      SimplePanel = True
    End Create

' voir   //D:/RapidQ/examples/qchart/QChart.htm
Create Graph1 As QCHART                 'Create a copy of the new object
     Parent  = EDF_BT
     Top =300
     Left =50
     Height=450
     Width=1000
     OnPaint = Graph1.PaintChart
     cols=4     'Set .Data.ColCount to the number of XY pairs * 2
    End Create'    Graph1



Create Groupe1 as QGroupBox
      Parent  = EDF_BT
      Top     = 32
      Left    = 8
      Width   = 201
      Height  = 81
      Caption = "Port de communication inconnu"
      Color   = -2147483648
End Create

Create Send_Button as QCoolBtn
      Parent  = Groupe1
      Top     = 16
      Left    = 16
      Width   = 92
      Height  = 25
      Caption = "Send Msg"
      OnClick = Send_ButtonClick
      Font.Name = "MS Sans Serif"
      Font.Color = -2147483640
      Font.Size = 10
      Font.AddStyles fsBold
      Enabled = False
End Create

Create MAJ_RTC as QCoolBtn
      Parent  = Groupe1
      Top     = 48
      Left    = 16
      Width   = 92
      Height  = 25
      Caption = "MAJ RTC"
       OnClick = MAJ_RTCClick
       Font.Name = "MS Sans Serif"
      Font.Color = -2147483640
      Font.Size = 12
      Font.AddStyles fsBold
      Enabled = False
End Create


Create Help_button as QCoolBtn
      Parent  = Groupe1
      Top     = 48
      Left    = 112
      Width   = 76
      Height  = 26
      Caption = "help"
      OnClick = Help_buttonClick
      Font.Name = "MS Sans Serif"
      Font.Color = -2147483640
      Font.Size = 10
      Font.AddStyles fsBold
End Create
Create Sortie as QButton
      Parent  = Groupe1
      Top     = 18
      Left    = 114
      Width   = 77
      Height  = 24
      Caption = "Sortie"
      OnClick = SortieClick
      Align = alNone
      Font.Name = "MS Sans Serif"
      Font.Color = -2147483640
      Font.Size = 8
      Font.AddStyles fsBold
End Create

' COM Setup
Create Port_Combo1 as QComboBox
      Parent  = EDF_BT
      Top     = 8
      Left    = 8
      Width   = 81
      Height  = 22
      Text    = ""
      Color   = &HFFFFFF
      Font.Name = "MS Sans Serif"
      Font.Color = -2147483640
      Font.Size = 10
      AddItems  "Com1","Com2","Com3","Com4","Com5","Com6",_
      "Com7","Com8","Com9"
      OnChange = Port_Combo1Change
      Style = csOwnerDrawFixed
End Create
Create Speed_Combo2 as QComboBox
      Parent  = EDF_BT
      Top     = 8
      Left    = 96
      Width   = 65
      Height  = 22
      Text    = ""
      Color   = &HFFFFFF
      AddItems  "1200",_
      "2400",_
      "4800",_
      "9600",_
      "19200",_
      "38400",_
      "57600",_
      "115200"
      OnChange = Speed_Combo2Change
      Style = csOwnerDrawFixed
      Font.Name = "MS Sans Serif"
      Font.Color = -2147483640
      Font.Size = 10
End Create
Create Parity_Combo3 as QComboBox
      Parent  = EDF_BT
      Top     = 8
      Left    = 168
      Width   = 41
      Height  = 22
      Text    = ""
      Color   = &HFFFFFF
      Style = csOwnerDrawFixed
      AddItems  "N",_
        "O",_
        "E",_
        "M",_
        "S"
      Font.Name = "MS Sans Serif"
      Font.Color = -2147483640
      Font.Size = 10
      OnChange = Parity_Combo3Change
End Create
Create Nb_Datas_Combo4 as QComboBox
      Parent  = EDF_BT
      Top     = 8
      Left    = 216
      Width   = 41
      Height  = 22
      Text    = ""
      Color   = &HFFFFFF
      Font.Name = "MS Sans Serif"
      Font.Color = -2147483640
      Font.Size = 10
      Style = csOwnerDrawFixed
      AddItems  ("7","8","9")
      OnChange = Nb_Datas_Combo4Change
End Create
Create Nb_Stop_Combo5 as QComboBox
      Parent  = EDF_BT
      Top     = 8
      Left    = 264
      Width   = 49
      Height  = 22
      Text    = ""
      Color   = &HFFFFFF
      AddItems ("1","1.5","2")
      Style = csOwnerDrawFixed
      Font.Name = "MS Sans Serif"
      Font.Color = -2147483640
      Font.Size = 10
      OnChange = Nb_StopChange
End Create

Create Zone_Emission as QRichEdit
      Parent  = EDF_BT
      Top     = 142
      Left    = 8
      Width   = 296
      Height  = 25
      Color   = &HFFFFFF
      PlainText = True
      Align = alNone
      HideScrollBars = False
      ScrollBars = ssVertical
      Hint = ""
    '  OnChange = Zone_EmissionChange
      ShowHint = True
      Font.Name = "MS Sans Serif"
      Font.Color = 16711680
      Font.Size = 8
  End Create

Create Label1 as QLabel
      Parent  = EDF_BT
      Top     = 8
      Left    = 336
      Width   = 76
      Height  = 13
      Caption = "Time elapsed"
      Color   = &HF0F0F0
      Font.Name = "MS Sans Serif"
      Font.Color = -2147483640
      Font.Size = 8
      Font.AddStyles fsBold
End Create

Create Timer1 as QTimer
      Enabled = False
      Interval = 1000

End Create



Create Zone_Reception as QRichEdit
      Parent  = EDF_BT
      Top     = 192
      Left    = 496
      Width   = 537
      Height  = 25
      Color   = -2147483624
    '  OnChange = Zone_ReceptionChange
      PlainText = True
      ScrollBars = ssVertical
      HideScrollBars = False
      ReadOnly = False
      ShowHint = True
      Font.Name = "MS Sans Serif"
      Font.Color = 16711680
      Font.Size = 8
End Create




Create RX_Gauge1 as QGauge
      Parent  = EDF_BT
      Top     = 160
      Left    = 528
      Width   = 177
      Height  = 25
      Color   = &HF0F0F0
      Max = 128
      Position = 64
      ForeColor = 16711680
End Create

Create Raz_Zone_Emission as QButton
      Parent  = EDF_BT
      Top     = 112
      Left    = 8
      Width   = 129
      Height  = 25
      Caption = "Raz buffer emission"
        OnClick = Raz_Zone_EmissionClick
 End Create

Create Zone_Debug as QRichEdit
      Parent  = EDF_BT
      Top     = 192
      Left    = 8
      Width   = 473
      Height  = 25
      Color   = &HFFFFFF
      ScrollBars = ssVertical
      HideScrollBars = False
      PlainText = True
End Create


Create Labelx as QLabel
      Parent  = EDF_BT
      Top     = 88
      Left    = 216
      Width   = 120
      Height  = 21
      Caption = "PicTime"
      Color   = &HF0F0F0
End Create


Create DateEnCours as QLabel
      Parent  = EDF_BT
      Top     = 8
      Left    = 512
      Width   = 23
      Height  = 13
      Caption = "Date"
      Color   = &HF0F0F0
End Create

Create TimeEnCours as QLabel
      Parent  = EDF_BT
      Top     = 8
      Left    = 600
      Width   = 23
      Height  = 13
      Caption = "Time"
      Color   = &HF0F0F0
End Create

Create Label7_wod as QLabel
      Parent  = EDF_BT
      Top     = 8
      Left    = 448
      Width   = 25
      Height  = 13
      Caption = "WoD"
      Color   = 16777215
      AutoSize = False
End Create
Create Raz_Zone_Debug as QButton
      Parent  = EDF_BT
      Top     = 168
      Left    = 8
      Width   = 113
      Height  = 25
      Caption = "Raz_Zone_Debug"
       OnClick = Raz_Zone_DebugClick
End Create
Create RAZ_Reception as QButton
      Parent  = EDF_BT
      Top     = 160
      Left    = 720
      Width   = 105
      Height  = 25
      Caption = "RAZ_Reception"
      OnClick = RAZ_ReceptionClick
End Create
Create GroupBox1 as QGroupBox
      Parent  = EDF_BT
      Top     = 40
      Left    = 216
      Width   = 169
      Height  = 41
      Caption = "Numero de compteur"
      Color   = &HF0F0F0
End Create
Create Num_Compteur as QLabel
      Parent  = GroupBox1
      Top     = 14
      Left    = 10
      Width   = 134
      Height  = 24
      Caption = "Num_Compteur"
      Color   = &HF0F0F0
      Font.Name = "MS Sans Serif"
      Font.Color = 0
      Font.Size = 14
End Create
Create GroupBox2 as QGroupBox
      Parent  = EDF_BT
      Top     = 32
      Left    = 400
      Width   = 145
      Height  = 41
      Caption = "Temp Amb"
      Color   = &HF0F0F0
End Create
Create Label_TAmb as QLabel
      Parent  = GroupBox2
      Top     = 14
      Left    = 6
      Width   = 155
      Height  = 24
      Caption = " 19.99"
      Color   = &HF0F0F0
      Font.Name = "MS Sans Serif"
      Font.Color = -2147483640
      Font.Size = 14
End Create
Create GroupBox3 as QGroupBox
      Parent  = EDF_BT
      Top     = 88
      Left    = 336
      Width   = 185
      Height  = 57
      Caption = "Nb de W/H en cours"
      Color   = 65280
      ShowHint = False
      Font.Name = "MS Sans Serif"
      Font.Color = -2147483640
      Font.Size = 10
      Font.AddStyles fsBold
End Create
Create Wh_Now as QLabel
      Parent  = GroupBox3
      Top     = 14
      Left    = 14
      Width   = 144
      Height  = 37
      Caption = "00000000"
      Color   = 65280
      Font.Name = "Arial"
      Font.Color = 0
      Font.Size = 24
      Font.AddStyles  fsBold
      LowerColor = 536870911
      Cursor = -21
      ShowHint = True
End Create
Create GroupBox4 as QGroupBox
      Parent  = EDF_BT
      Top     = 88
      Left    = 672
      Width   = 185
      Height  = 65
      Caption = "  (Kwh)"
      Color   = 65535
      ShowHint = False
      Font.Name = "MS Sans Serif"
      Font.Color = 0
      Font.Size = 10
      Font.AddStyles fsBold
End Create
Create Conso_KWH as QLabel
      Parent  = GroupBox4
      Top     = 15
      Left    = 7
      Width   = 167
      Height  = 43
      Caption = "Cons_KWH"
      Color   = 65535
      Font.Name = "Arial Narrow"
      Font.Color = 0
      Font.Size = 28
      Font.AddStyles  fsBold
      Align = alNone
      ShowHint = True
End Create
Create GroupBox5 as QGroupBox
      Parent  = EDF_BT
      Top     = 16
      Left    = 760
      Width   = 73
      Height  = 65
      Caption = "Amperes"
      Color   = 16777215
End Create
Create Amperes as QLabel
      Parent  = GroupBox5
      Top     = 20
      Left    = 12
      Width   = 40
      Height  = 30
      Caption = "A"
      Color   = &HFFFFFF
      Font.Name = "NI7SEG"
      Font.Color = 16711680
      Font.Size = 24
      Font.AddStyles fsBold
End Create
Create GroupBox7 as QGroupBox
      Parent  = EDF_BT
      Top     = 88
      Left    = 528
      Width   = 137
      Height  = 65
      Caption = " Temper.Exterieure "
      Color   = 8421504
      Font.Name = "MS Sans Serif"
      Font.Color = 65535
      Font.Size = 8
End Create
Create Label_TempExt as QLabel
      Parent  = GroupBox7
      Top     = 16
      Left    = 16
      Width   = 86
      Height  = 41
      Caption = "19.99"
      Color   = 8421504
      Font.Name = "Arial"
      Font.Color = 65280
      Font.Size = 26
      Font.AddStyles  fsBold
      Alignment = taRightJustify
End Create
Create Add_CR as QCheckBox
      Parent  = EDF_BT
      Top     = 112
      Left    = 144
      Width   = 73
      Height  = 25
      Caption = "Add_CR"
    '  OnClick = Add_CRClick
End Create
Create Add_LF as QCheckBox
      Parent  = EDF_BT
      Top     = 112
      Left    = 216
      Width   = 65
      Height  = 25
      Caption = "Add_LF"
  '    OnClick = Add_LFClick
End Create

Create Visu_Trame as QCheckBox
      Parent  = EDF_BT
      Top     = 160
      Left    = 848
      Width   = 89
      Height  = 25
      Caption = "Visu_Trame"
      OnClick = visu_TrameClick
End Create


Create Simulation as QCheckBox
      Parent  = EDF_BT
      Top     = 160
      Left    = 952
      Width   = 73
      Height  = 25
      Caption = "Simulation"
End Create
Create Label5 as QLabel
      Parent  = EDF_BT
      Top     = 378
      Left    = 690
      Width   = 168
      Height  = 29
      Caption = "Sauve Fichier "
      Color   = &HF0F0F0
      Transparent = False
      Enabled = False
      Visible = False
      Font.Name = "MS Sans Serif"
      Font.Color = -2147483640
      Font.Size = 18
      Font.AddStyles fsBold
End Create


Create GroupBox6 as QGroupBox
      Parent  = EDF_BT
      Top     = 16
      Left    = 864
      Width   = 169
      Height  = 137
      Caption = "EDF  constante"
      Color   = 16776960
End Create
Create Label6 as QLabel
      Parent  = GroupBox6
      Top     = 34
      Left    = 106
      Width   = 52
      Height  = 16
      Caption = "€/ KWH"
      Color   = 16776960
      Font.Name = "MS Sans Serif"
      Font.Color = 0
      Font.Size = 10
      Font.AddStyles fsBold
      LowerColor = 16776960
End Create
Create Dernier_Releve as QEdit
      Parent  = GroupBox6
      Top     = 96
      Left    = 10
      Width   = 145
      Height  = 45
      Text    = "Dernier_Releve"
      Color   = &HC0C0C0
End Create
Create GroupBox8 as QGroupBox
      Parent  = EDF_BT
      Top     = 24
      Left    = 552
      Width   = 193
      Height  = 57
      Caption = "Facture Euro"
      Color   = 65535
      Font.Name = "MS Sans Serif"
      Font.Color = 16711680
      Font.Size = 10
      Font.AddStyles fsBold
End Create
Create Conso_Euro as QLabel
      Parent  = GroupBox8
      Top     = 16
      Left    = 10
      Width   = 155
      Height  = 36
      Caption = "Conso_Euro"
      Color   = 65535
      Font.Name = "MS Sans Serif"
      Font.Color = 16711680
      Font.Size = 23
      Font.AddStyles  fsBold
End Create
Create Label2 as QLabel
      Parent  = GroupBox6
      Top     = 72
      Left    = 8
      Width   = 90
      Height  = 13
      Caption = "Dernier relevé EDF"
      Color   = &HFFFF00
End Create
Create Edit1_Prix_KWH as QEdit
      Parent  = GroupBox6
      Top     = 24
      Left    = 16
      Width   = 81
      Height  = 28
      Text    = "Edit1_Prix_KWH"
      Color   = &HFFFFFF
      Font.Name = "MS Sans Serif"
      Font.Color = -2147483640
      Font.Size = 12
      Font.AddStyles fsBold

End Create



Create Numero_Jour as QLabel
      Parent  = EDF_BT
      Top     = 8
      Left    = 680
      Width   = 63
      Height  = 13
      Caption = "Numero_Jour"
      Color   = &HF0F0F0
End Create
Create CheckPrix as QCheckBox
      Parent  = GroupBox6
      Top     = 56
      Left    = 104
      Width   = 49
      Height  = 17
      Caption = "Valid"
      OnClick = CheckPrixClick
End Create
Create CheckBox1_Table as QCheckBox
      Parent  = EDF_BT
      Top     = 152
      Left    = 352
      Width   = 113
      Height  = 33
      Caption = "Table Pilotage"
      Font.Name = "MS Sans Serif"
      Font.Color = -2147483640
      Font.Size = 8
      Font.AddStyles fsBold
      OnClick = CheckBox1_TableClick
End Create
Create Label3 as QLabel
      Parent  = EDF_BT
      Top     = 915
      Left    = 8
      Width   = 400
      Height  = 18
      Caption = "status"
      Color   = -2147483648
End Create
'----------------------------------
Create Label4_1 as QLabel
      Parent  = EDF_BT
      Top     = 360
      Left    = 920
      Width   = 40
      Height  = 13
      Caption = "Cumulus"
      Color   = &HF0F0F0
End Create


Create Label4_2 as QLabel
      Parent  = EDF_BT
      Top     = 390
      Left    = 920
      Width   = 60
      Height  = 16
      Caption = "Chauff.SOL"
      Color   = &HF0F0F0
End Create
Create Label4_3 as QLABEL
       Parent  = EDF_BT
      Top     = 420
      Left    = 920
      Width   = 60
      Height  = 16
      Caption = "SDB"
      Color   = &HF0F0F0
End Create
Create Label4_5 as QLabel
       Parent  = EDF_BT
      Top     = 450
      Left    = 920
      Width   = 58
      Height  = 13
      Caption = "CH2-Bureau"
      Color   = &HF0F0F0
End Create
Create Label4_6 as QLabel
       Parent  = EDF_BT
      Top     = 480
      Left    = 920
      Width   = 21
      Height  = 13
      Caption = "CH1"
      Color   = &HF0F0F0
End Create

Create Codage as QLabel
       Parent  = EDF_BT
      Top     = 520
      Left    = 920
      Width   = 50
      Height  = 13
      Caption = "Codage"
      Color   = &HF0F0FF
End Create

Create Image1_1 as QImage
     Parent  = EDF_BT
      Top     = 350
      Left    = 980
      Width   = 25
      Height  = 25
     ' Autosize=True
     ' BMPHandle=LEDR_Off
      BMP = "D:\RapidQ\_BT_Edfinfo\_BMP\Voyant_Stop.bmp"
End Create
Create Image1_2 as QImage
       Parent  = EDF_BT
      Top     = 380
      Left    = 980
      Width   = 25
      Height  = 25
     ' Autosize=True
     ' BMPHandle=ARRET
       BMP = "D:\RapidQ\_BT_Edfinfo\_BMP\ARRET.bmp"
End Create
Create Image1_3 as QImage
      Parent  = EDF_BT
      Top     = 410
      Left    = 980
      Width   = 25
      Height  = 25
     ' Autosize=True
     ' BMPHandle=ECO
       BMP = "D:\RapidQ\_BT_Edfinfo\_BMP\ECO.bmp"
End Create
Create Image1_4 as QImage
      Parent  = EDF_BT
      Top     = 440
      Left    = 980
      Width   = 25
      Height  = 25
    '  BMPHandle=HGEL
      BMP = "D:\RapidQ\_BT_Edfinfo\_BMP\HGEL.bmp"
End Create
Create Image1_5 as QImage
     Parent  = EDF_BT
      Top     = 470
      Left    = 980
      Width   = 25
      Height  = 25
    '  BMPHandle=CONFORT
      BMP = "D:\RapidQ\_BT_Edfinfo\_BMP\CONFORT.bmp"
End Create



'----- Define the main form
Create Form2 As QFORMex
  Center
  Width = 1000
  Height = 500
  Top = 200
  Left =200
  Caption = "Progr.Radiateurs Fils Pilotes"
  OnResize = Form2Resize
  OnClose = Form2Close
  Transparent=255   ' =0 totally transparent  =127  50%
  Visible=0
end create

    
    Create btnEdit As QBUTTON
    parent=Form2
      Caption = "Enter Row 0"
      Width = 100
      Top = 0
       Left=1
      OnClick = btnOnClick
    End Create
    Create btnSelect As QBUTTON
     parent=Form2
      Caption = "Enter SelMode"
      Width = 100
      Top = 25
       Left=1
      OnClick = btnOnClick
    End Create
    Create Grid As QGridEx
     parent=Form2
      Align = 4
      TOP=  5
      left= 10
      Width = Form2.ClientWidth - btnEdit.Width
      Height = Form2.ClientHeight
    End Create
    Create StatusBar AS QStatusBar
    parent=Form2
      SizeGrip = False
      SimplePanel = True
    End Create

     Create btCalcul As QBUTTON
     parent=Form2
      Caption = "Calculs"
      Width = 100
      Top = 100
      Left=1
      Enabled=False
      OnClick = btCalculClick
    End Create




CREATE mnuMain as QMAINMENU
  Parent = Form2

'----- File Menu
  CREATE mnuFile as QMENUITEM
    Caption = "&File"
    Checked = FALSE
    Enabled = 1
    Visible = 1
    CREATE mnuFileNew as QMENUITEM
      Caption = "&New"
      Checked = 0
      Enabled = 1
      Visible = 1
      OnClick = mnuFileClick
    END CREATE
    CREATE mnuFileOpen as QMENUITEM
      Caption = "&Open..."
      Checked = 0
      Enabled = 1
      Visible = 1
      OnClick = mnuFileClick
    END CREATE
    CREATE mnuFileSep1 as QMENUITEM
      Caption = "-"
      Checked = 0
      Enabled = 0
      Visible = 1
    END CREATE
    CREATE mnuFileSave as QMENUITEM
      Caption = "&Save..."
      Checked = 0
      Enabled = 1
      Visible = 1
      OnClick = mnuFileClick
    END CREATE
    CREATE mnuFileSaveAs as QMENUITEM
      Caption = "Save &As..."
      Checked = 0
      Enabled = 1
      Visible = 1
      OnClick = mnuFileClick
    END CREATE
    CREATE mnuFileSep2 as QMENUITEM
      Caption = "-"
      Checked = 0
      Enabled = 0
      Visible = 1
    END CREATE
    CREATE mnuFilePrint as QMENUITEM
      Caption = "&Print..."
      Checked = 0
      Enabled = 1
      Visible = 1
      OnClick = mnuFileClick
    END CREATE
    CREATE mnuFileSep3 as QMENUITEM
      Caption = "-"
      Checked = 0
      Enabled = 0
      Visible = 1
    END CREATE
    CREATE mnuFileExit as QMENUITEM
      Caption = "E&xit"
      Checked = 0
      Enabled = 1
      Visible = 1
      OnClick = mnuFileClick
    END CREATE
  END CREATE

'----- Edit Menu
  CREATE mnuEdit as QMENUITEM
    Caption = "&Edit"
    Enabled = 1
    Visible = 1
    CREATE mnuEditCut as QMENUITEM
      Caption = "C&ut"
      Enabled = 0
      Visible = 1
      OnClick = mnuEditClick
      Shortcut = "Ctrl+x"
    END CREATE
    CREATE mnuEditCopy as QMENUITEM
      Caption = "&Copy"
      Enabled = 0
      Visible = 1
      OnClick = mnuEditClick
      Shortcut = "Ctrl+c"
    END CREATE
    CREATE mnuEditPaste as QMENUITEM
      Caption = "&Paste"
      Enabled = 0
      Visible = 1
      OnClick = mnuEditClick
      Shortcut = "Ctrl+v"
    END CREATE
    CREATE mnuEditDelete as QMENUITEM
      Caption = "&Delete"
      Enabled = 0
      Visible = 1
      OnClick = mnuEditClick
    END CREATE
    CREATE mnuEditSep0 as QMENUITEM
      Caption = "-"
      Enabled = 0
      Visible = 1
    END CREATE
    CREATE mnuEditSelAll as QMENUITEM
      Caption = "&Select All"
      Enabled = 0
      Visible = 1
      OnClick = mnuEditClick
      Shortcut = "Ctrl+a"
    END CREATE
    CREATE mnuEditSep1 as QMENUITEM
      Caption = "-"
      Enabled = 0
      Visible = 1
    END CREATE
    CREATE mnuEditFillDown as QMENUITEM
      Caption = "&Fill Down"
      Enabled = 0
      Visible = 1
      OnClick = mnuEditClick
      Shortcut = "Ctrl+d"
    END CREATE
    CREATE mnuEditFillRight as QMENUITEM
      Caption = "Fill &Right"
      Enabled = 0
      Visible = 1
      OnClick = mnuEditClick
      Shortcut = "Ctrl+r"
    END CREATE
    CREATE mnuEditFillSeries as QMENUITEM
      Caption = "Fill S&eries..."
      Enabled = 0
      Visible = 1
      OnClick = mnuEditClick
    END CREATE
    CREATE mnuEditSort as QMENUITEM
      Caption = "S&ort..."
      Enabled = 1
      Visible = 1
      OnClick = mnuEditClick
    END CREATE
    CREATE mnuEditSep2 as QMENUITEM
      Caption = "-"
      Enabled = 0
      Visible = 1
    END CREATE
    CREATE mnuEditSelMode as QMENUITEM
      Caption = "Select &Mode"
      Enabled = 1
      Visible = 1
      OnClick = mnuEditClick
      Shortcut = "F2"
    END CREATE
    CREATE mnuEditRow0 as QMENUITEM
      Caption = "Edit Col&umn Labels"
      Enabled = 1
      Visible = 1
      OnClick = mnuEditClick
      Shortcut = "F5"
    END CREATE
  END CREATE

END CREATE


'====================================================================




Sub DrawOrig
  Dim j  As Integer
  Dim i  As Integer

    'draw original data set
      With Graph1
        .Initialize                             'Set defaults
        .ChartType = ctXY                       'XY Scatter Chart
        .ChartStyle = csLines                   'just Lines, use csBoth for lines and Points
        .MainTitle.Text = "EDF Info Data"
        .XTitle.Text = "Nb Trames EDF"
        .YTitle.Text = "Amperes"
        .ChartBorder=1
        .AxisBorder=True
        .GridColor=&H00FF00
        .PlotAreaColor=&HF0F0F0
        .Cols = 1                               'Number of series
        .Rows = n-1
        .XAxis.color=.Colors(0)
         .Series(1).COLOR=.Colors(5)                          'Number of data points per series
        REDIM Graph1.XYData(.Cols,.Rows,2)
        .DoLegend = false
        .Series(1).LineWidth = 3
        .Series(1).LineStyle=lsSolid
        .Missing=0
         For j = 0 To .Rows
           .XYData(1,j,1) =j
           .XYData(1,j,2) =0
          next j
         .AxisBorder=TRUE
         .XAxis.AutoScale=False
         .YAxis.AutoScale=False
         .YAxis.Max = 40.0
         .XAxis.Max = n     ' 6000 pour ~2H00
          .DrawChart (False)    'Draw it!     '

      End With


  END SUB





 Sub Initialise()
   Label_TAmb.Caption="00.00"
  NumCompteur$= "021961176859"
  Num_Compteur.Caption=NumCompteur$
  NbWattActuel=0
  'TemperAmb1=0.0     ' LM35DZ  analogique
  TemperExt=19.99    ' DS18B20  OWS
  
  KWh=0.0           ' ecart watt.h en kw.h
 ' Trame_EDF$="......................................................................................................."
  Trame_EDF$=""
  NbWattActuel=0
  OldNbWatt=0
  Once=0
   Amps=0
  Amps_Moyen=0
  Temper_Moyen=0
  Nb_Trame=0
  Index_Chart=0
  Conso_KWh.Caption="0000"
  Label_Tamb.caption="19.99"
  WH_Now.caption="0000"
  for i=0 to 600
     Table(i)=0
  next i
  for Nb_Mn=0 to 1440
     Amps_Store(Nb_Mn)=0
     Temper_Store(Nb_Mn)=0
  next Nb_Mn
  Nb_Mn=0
  Graph1.ClearAll
  n=600
  EDF_BT.Caption="EDF_BT Version : "+Version
  Drapeau_Save=0
  Simulation.Checked=False
  Numero_jour.caption=0
  
  
    HH = val(left$(Time$,2))
    Mn = val(Mid$(Time$,4,2))
    ss = val(right$(Time$,2))
    Nb_Mn=HH*60+Mn
    Old_Nb_Mn=Nb_Mn
    Old_HH=HH
    
    
 ' init tables
 for j=0 to 7:read JourSem$(J):next j
 for j=0 to 12
    read  Month$(j)
    Read month(j)
 next j

  
With EDF_BT
      .Top     = 5
      .Left    = 5
      .Width   =1045
      .Height  = 760
END with

 with Graph1
     .Top =230
     .Left =10
     .Height=450
     .Width=900
 End with

 with Label3
    .Top  = 910
    .Left    = 30
   .Width   = 600
   .Height  = 18
 end with

 with Label5
    .Top  = 496
    .Left    = 680
   .Width   = 328
   .Height  = 117
   .Caption = "Sauve Fichier Data "
   .Color   = &HF0F0F0
   .Transparent = False
   .Enabled = False
   .visible=False
 end with
  
  With Dernier_Releve
   .Top  = 90
    .Left    = 10
   .Width   =145
   .Height  =60
   .text = "Dernier releve EDF "
   .Enabled = True
   .visible=True
   .Font.Size = 22
   .Font.AddStyles fsBold
   .font.Color   = 12632256
   .OnChange = Dernier_ReleveChange
   .Font.Name = "MS Sans Serif"
   .Font.Color = 16711680
   .Font.AddStyles  fsBold
   .AutoSize = False
  end with
  
  '========================
  WITH Grid
      'Label columns w/letters
 .Cell(0,1) = "Cumulus"
 .Cell(0,2) = "Salon  "
 .Cell(0,3) = "SDB    "
 .Cell(0,4) = "CH3-CH2"
 .Cell(0,5) = "CH1    "
 .Cell(0,6) = "?"
 END with

 SetMin(Form2)
 Form2.Visible=0
 
' Application.Hinthidepause=1000 ' 1 sec
'Form2.Showhint=1
'Form2.hint=" hello chaps !"
 
Grid.Initialize
AllReady_Running=0
  
End Sub



SUB TimerOver()
 dim Iz as integer
 dim NBytes as integer
 Nb_Tics=Nb_Tics+1
 'Communication
  Jt = Jt + 1
  Label1.Caption = "Time ealpsed = " +STR$(jt)

   if Simulation.checked=False then   '1
    NBytes=INBUFFER()
    RX_Gauge1.Position= NBytes
    if ( (NBytes>1) and (NBytes<LongMini)) then
     A$=ReadString()
     Zone_Reception.AddString  (A$)
    end if
 else '1
    NBytes=104
    if (jt>28) then jt=0
 end if

  
  ' 1 seule trame dans le buffer !
  if ( (NBytes>LongMini)  and (NBytes < LongMaxi )) then
 '  if ( (NBytes>70)  and (NBytes < 104 )) then

    Label3.Caption = "Bytes in buffer: " + STR$(NBytes)
  'Lecture buffer COM
    if Simulation.checked=False then
       Read_COMx()
       else
      Amps=  Index_Chart*0.05 + rnd(200)*0.01
      Amperes.caption=format$("%2.1f",Amps)
    end if
    Index_Chart=Index_Chart+1
    JourSem=Joursemaine()
    'JourCalendaire()
    Numero_Jour.caption="Jc="+str$(Numjour)
    TimeEnCours.Caption=Time$
    dow$=mid$(dd$,(JourSem*3)+1,3)
    DateEnCours.Caption=str$(dd)+"/"+str$(mm)+"/"+ str$(yy)
    Label7_wod.caption=Dow$

     HH = val(left$(Time$,2))
     Mn = val(Mid$(Time$,4,2))
     ss = val(right$(Time$,2))
     Nb_Mn=HH*60+Mn
     ZOne_Debug.text=str$(Nb_mn) +chr$(13)
     if (Nb_Mn=0) then Drapeau_Save=0
     Temper_Moyen=Temper_Moyen+TemperExt
     Amps_Moyen=Amps_Moyen+Amps
     Nb_Trame=Nb_Trame+1

    if (Nb_Mn<> Old_Nb_Mn) then
       Old_Nb_Mn=Nb_Mn
       if (Nb_Trame>2) Then
          Amps_Store(Nb_Mn)=Amps_Moyen/Nb_Trame
          Temper_Store(Nb_Mn)=  Temper_Moyen/Nb_Trame
         'rajout temp moyenne horaire
          Temper_Heure=Temper_Heure+  Temper_Store(Nb_Mn)
          Nb_mes_Temp=Nb_mes_Temp+1
       else
          Amps_Store(Nb_Mn)=0
          Temper_Store(Nb_Mn)=0
       end if
       Nb_Trame=0
       Amps_Moyen=0
       Temper_Moyen=0
      end if
      
' memo KWH
      
      if (HH <> Old_HH) then
         Prix_Kwh=val(Edit1_Prix_Kwh.text)        ' modif 26-12-2018
         Delta_KWH= (NbWattActuel-OldNbWatt)/1000
         OldNbWatt= NbWattActuel
         KWH_Day_Store(OLD_HH)= Delta_KWH
         if (Nb_mes_temp>0) then
         Temper_Day(OLD_HH)= Temper_Heure/Nb_mes_Temp
         end if
         Old_HH=HH
         Temper_Heure=0
         Nb_mes_Temp=0
        end if
      

      ' sauvegarde vers 23H59:56 sec
      if (Nb_Mn>1438) and (ss>50) and (Drapeau_Save=0) then

       Timer1.enabled=False

    ' cas particulier de minuit
        ZOne_Debug.text="Sauve KWH"
       Delta_KWH= (NbWattActuel-OldNbWatt)/1000
       OldNbWatt= NbWattActuel
       KWH_Day_Store(23)= Delta_KWH

       Save_EDFinfo_to_File
       Drapeau_Save=1
       while val(right$(Time$,2))>50
        'attend passage à 00 sec
       wend
       Old_HH=0
       Nb_Mn=0
        Timer1.enabled=True
       end if
      
    '  ZOne_Debug.text=ZOne_Debug.text+ str$(Amps_Moyen) +chr$(13)

    ' taille à etendre !!
    if (Index_Chart>599) then
      ' Index_Chart=599
       Once=1
       end if

     if (Once=1) then
        with Graph1
           Index_Chart=599
           .Series(1).COLOR=.Colors(5)
           .Series(2).COLOR=.Colors(7)
           for i=1 to 599
              Table(i-1)=Table(i)
             .XYData(2,i-1,2)= Table_T(i-1)
             .XYData(1,i-1,2)= Table(i-1)
           next i
           Table(599)=Amps
           Table_T(599)=TemperExt
            .XYData(1,599,2)= Table(599)
            .XYData(2,599,2)= Table_T(599)
           .DrawChart (false)
        end with
     else
        with Graph1
        .Series(1).COLOR=.Colors(5)
        .Series(2).COLOR=.Colors(7)
          Table(Index_Chart)=Amps
          Table_T(Index_Chart)=TemperExt
         .XYData(1,Index_Chart,2)= Table(Index_Chart)
         .XYData(2,Index_Chart,2)= Table_T(Index_Chart)
         .DrawChart (false)
        end with
     end if
   else
     if (jt>30) then
         Zone_Reception.AddString ("TimeOut reception")
         jt=0
         CLEARBUFFER
       end if
   else
   Read_COMx()
   Label3.Caption = "PB,NBytes=" + STR$(NBytes)
       
  end if

  if (  Nb_Tics  mod 4=0) then


    mnuEditSelMode.Checked = Grid.SelMode
    mnuEditCut.Enabled = Grid.SelMode
    mnuEditCopy.Enabled = Grid.SelMode
    mnuEditPaste.Enabled = Grid.SelExists
    mnuEditDelete.Enabled = Grid.SelMode
    mnuEditSelAll.Enabled = Grid.SelMode
    mnuEditFillDown.Enabled = Grid.SelRange.bRow > Grid.SelRange.tRow
    mnuEditFillRight.Enabled = Grid.SelRange.rCol > Grid.SelRange.lCol
    mnuEditFillSeries.Enabled = Grid.SelMode
    If mnuEditSelMode.Checked = True Then                   'Out of Sync
      btnSelect.Caption = "Exit SelMode"
    End If
    If mnuEditSelMode.Checked = False Then                  'Out of Sync
      btnSelect.Caption = "Enter SelMode"
    End If

    mnuEditRow0.Checked = Grid.EditColLabels
    If mnuEditRow0.Checked = True Then                      'Out of Sync
      btnEdit.Caption = "Exit Row 0"
    End If
    If mnuEditRow0.Checked = False Then                     'Out of Sync
      btnEdit.Caption = "Enter Row 0"
    End If


   End if



 END SUB
 

 


SUB Read_COMx()

  Dim j as integer
  DIm i as integer
  Dim Lg as integer
  DIm Ax$ as string
  dim mn$ as string
    jt=0
   Trame_EDF$=ReadString()
   Lg=len(Trame_EDF$)
   if (Visu_Trame.Checked = True) then
      Zone_Reception.AddString ( Trame_EDF$ )
      End if
'
' Trame avec ancien compteur ENERTEC
' *C050222059498*;*W00056328500*;*K1214.500*;*E  8.68*;*A00005*;*G00005*;*T  0.01*;*H=0811*;*R=SAEEE*;
' Trame avec New compteur Linky :
' *C021961176859*;*W00000017193*;*K17.193*;*E 27.06*;*A00002*;*G00002*;*T 19.88*;*H=1731*;*R=MCHHH*;
'
' traitement des datas reçues
  if (  (left$(Trame_EDF$,7)="*C02196") and (Lg>LongMini) and (Lg<LongMaxi) ) then

    i=0
    j=0
    while (Mid$(Trame_EDF$,i,1)<>";" )
      i=i+1
     if (i> Lg) then  exit while
    Wend
    NumCompteur$= Mid$(Trame_EDF$,j+2,i-3)
    Num_Compteur.caption=NumCompteur$

  ' Nb de watts en cours
    i=i+1
    j=i
      while (Mid$(Trame_EDF$,i,1)<>";" )
       i=i+1
     if (i> Lg) then  exit while
    Wend

    
    Ax$=Mid$(Trame_EDF$,j+2,i-4)
    NbWattActuel= val(Ax$)
    Ax$=Str$(NbWattActuel )
    Wh_Now.caption=Ax$

     Kwh=(NbWattActuel-Val(Dernier_Releve.text))/1000  ' en KWH
     if (Kwh<=0) then
     Conso_KWh.caption= "Erreur"
     else
     Conso_KWh.caption=format$("%8.3f",kwh)
     end if

     Conso_Euro.Caption= format$("%3.2f €",(Kwh*Prix_Kwh) )
     
    '----  recherche valeur Temper  -----

    i=i+1
    j=i
    while (Mid$(Trame_EDF$,i,1)<>"E" )
       i=i+1
     if (i> Lg) then  exit while
    Wend

    i=i+1
    j=i
    while (Mid$(Trame_EDF$,i,1)<>";" )
       i=i+1
      if (i> Lg) then  exit while
    Wend
    ';*E  8.68*;
    Ax$= Mid$(Trame_EDF$,j,i-j-1)   '   il n'y a plus °C dans la version 18F
    TemperExt=val(Ax$)
    Label_TempExt.caption=format$("%4.2f",TemperExt)


 ' --- recherche Amperes  ----
    i=i+1
    j=i
    while (Mid$(Trame_EDF$,i,1)<>"A" )
       i=i+1
     if (i> Lg) then  exit while
    Wend
    
  ' pass over "A"
    i=i+2
    j=i
    while (Mid$(Trame_EDF$,i,1)<>";" )
       i=i+1
      if (i> Lg) then  exit while
    Wend
    Ax$= Mid$(Trame_EDF$,j,i-j-1)  '@@@ correctif
    Amps=val(Ax$)
    Amperes.caption=str$(Amps)
    
    
    ' pass over ",T"
    i=i+1
    j=i
    while (Mid$(Trame_EDF$,i,1)<>"T" )
       i=i+1
      if (i> Lg) then  exit while
      doevents
    Wend


    i=i+1
    j=i
    while (Mid$(Trame_EDF$,i,1)<>";" )
       i=i+1
      if (i> Lg) then  exit while
      doevents
    Wend
    Ax$= Mid$(Trame_EDF$,j,i-j-1)   ' enlever  *    '@@@ correctif
     '   Label_TAmb.Caption=Ax$

    TemperAmb=val(Ax$)
    Label_TAmb.caption=format$("%4.2f",TemperAmb)
    

    ' added 04-05-2019
    ' pass over ",H"
    i=i+1
    j=i
    while (Mid$(Trame_EDF$,i,1)<>"H" )
       i=i+1
      if (i> Lg) then  exit while
      doevents
    Wend


    i=i+1
    j=i
    while (Mid$(Trame_EDF$,i,1)<>";" )
       i=i+1
      if (i> Lg) then  exit while
      doevents
    Wend
    Ax$=  Mid$(Trame_EDF$,j+1,i-j-2) ' enlever  *    '
   Labelx.caption="PicTime: " + Ax$  ' format HHMM


  ' @@@ 17-01-2019
  ' pass over "R="
    i=i+2
    j=i
    while (Mid$(Trame_EDF$,i,1)<>"R" )
       i=i+1
     if (i> Lg) then  exit while
     doevents
    Wend


    i=i+2
    j=i
    while (Mid$(Trame_EDF$,i,1)<>";" )
       i=i+1
      if (i> Lg) then  exit while
    Wend
    Ax$= Mid$(Trame_EDF$,j,i-j-1)   '@@@ correctif
    'Statusbar1.Simpletext= Ax$
   ' Codage.caption="Codage "+Ax$
    Codage.caption=Ax$

    Rafraichit_Icones


   else
     Ax$="Erreur sur trame "+chr$(13)
     Zone_Debug.text=Ax$
     CLEARBUFFER
   end if
 ' Trame_EDF$=""
END SUB




 EDF_BT.ShowModal
  DrawOrig
 
 

Sub EDF_BTShow(Sender as QForm)
 dim A1$ as string

 if (Allready_Running=0) then

 BUFFERSIZE(132)   ' > longueur trame  < 104 cars
 CLEARBUFFER
 Initialise()
 Trame_EDF$=""
 'n=maxV
 n=600
 ' dimanche=Day week #0
 dd$= "DimLunMarMerJeuVenSam"
 
 Nombre = 0
 Chemin  = left$(command$(0), rinstr(command$(0), "\"))
 IF FILEEXISTS(Chemin + "$$$$$.$$$" ) = 0 and Deja = 0 THEN
        Fichier.open(Chemin + "$$$$$.$$$" ,fmcreate)
        Fichier.close
        Deja = 1
  elseif Deja = 0 then
        messagebox("Le programme est déja chargé ou alors il vous faut _
                    détruire le fichier $$$$$.$$$ qui se trouve dans le _
                    meme répertoire.","Achtung!",0)
        Application.terminate
        end
  end if


 IF FILEEXISTS(Chemin + Ini ) <> 0 THEN
      Fichier.open(Chemin + Ini, fmopenread)
      A1$=  "Ouverture fichier "+chr$(13)+Chemin + Ini
      messagebox(A1$,"Init COM",0)

  DO
      Nombre = Nombre + 1
      Variable$ = trim((fichier.readline()))

    IF Nombre = 1 AND variable$ <> "" THEN
       IF VAL(variable$) < 40000 THEN
         EDF_BT.Top = VAL(variable$)
        END IF
    END IF

    IF Nombre = 2 AND variable$ <> "" THEN
       IF VAL(variable$) < 40000 THEN
          EDF_BT.left = VAL(variable$)
       END IF
    END IF

    IF Nombre = 3 AND variable$ <> "" THEN
       Speed_combo2.ItemIndex = val(Variable$)
       baud = Speed_combo2.Text
    end if

    IF Nombre = 4 AND variable$ <> "" THEN
       Parity_Combo3.ItemIndex = val(Variable$)
       Parite = Parity_Combo3.Text
    end if

    IF Nombre = 5 AND variable$ <> "" THEN
      NB_Datas_Combo4.ItemIndex = val(Variable$)
      Nb_datas = Nb_Datas_Combo4.Text
    end if

    IF Nombre = 6 AND variable$ <> "" THEN
      Nb_Stop_combo5.ItemIndex = val(Variable$)
      Nb_Stop = Nb_Stop_Combo5.Text
    end if

    if Nombre = 7 AND variable$ <> "" THEN
      Port_Combo1.ItemIndex = val(Variable$)
      Com = Port_Combo1.Text
     end if

      if Nombre = 8 AND variable$ <> "" THEN
        Dernier_Releve.text=Variable$
        Last_Releve=val(Dernier_Releve.text)
     end if

     if Nombre = 9 AND variable$ <> "" THEN
         Edit1_Prix_KWH.Text=Variable$
        Prix_KWH=val(Variable$)
     end if

     LOOP UNTIL    Fichier.Size = Fichier.Position


      if  Com <> "" and Baud <> "" And Parite <> "" and Nb_Datas <> "" and Nb_Stop <> "" then
        ' tous les parametres setup COM sont present
        ' donc activer le port COM

        Activer_Port_COM()

        Send_Button.enabled=True
        MAJ_RTC.enabled=True

        Zone_Emission.text="Pret à recevoir des données.."




        Timer1.interval=TIMEOUT_Trame
        Timer1.OnTimer= TimerOver
        HH = val(left$(Time$,2))
        Mn = val(Mid$(Time$,4,2))
        ss = val(right$(Time$,2))
        Nb_Mn=HH*60+Mn
        Old_Nb_Mn=Nb_Mn
      end if     '  if com

  Timer1.Enabled= True
  Fichier.close
  Nombre = 0
 END IF  ' file exist
  DrawOrig
  AllReady_Running=1
 end if
  
  
END sub

sub Recup_Date ()
 Datum$=str$(mm)
  if len(Datum$)<2 then
     Datum$="0"+ Datum$
     end if
  if dd<10 then
    Datum$=Datum$+"0""
  end if
  Datum$=Datum$+str$(dd)+".txt"
end sub



Sub Save_EDFinfo_to_File
  dim ia as integer
  dim C$ as string
  dim TT as long
  dim FileAnnuel$ as string
  dim FileAmps$ as string
  dim FileKwh$ as string
 ' dim Datum$ as string         passé en global
  dim FileName$ as string

  Label5.Enabled = True
  Label5.Visible= True

  FileAnnuel$="Datas\EDFinfo_2019.txt"
'  Datum$=str$(mm)
 ' if len(Datum$)<2 then
 '    Datum$="0"+ Datum$
 '    end if
 ' if dd<10 then
 '   Datum$=Datum$+"0""
 ' end if
 ' Datum$=Datum$+str$(dd)+".txt"

  Recup_Date()
  FileAmps$="Datas\EdfInfo_Amps_Temp_"+ Datum$


  IF FILEEXISTS(Chemin + FileAmps$) = 0 THEN
    Fichier.open(Chemin +FileAmps$,fmcreate)
    Fichier.close
  end if
  Fichier.open(Chemin + FileAmps$,fmopenwrite)
  C$= Date$+";"+Time$+";"
          Fichier.writeline(C$)
          for ia=0 to 1439
           C$= format$("%3.2f",Amps_Store(ia))+";" + format$("%3.2f",Temper_Store(ia))
           Fichier.WriteLine  C$
            Amps_Store(ia)=0      ' on le RAZ au fur et à mesure
            Temper_Store(ia)=0
          next ia
   Fichier.close
   

   FileKwh$="Datas\EdfInfo_KWH_TemP_"+ Datum$
   IF FILEEXISTS(Chemin + FileKwh$) = 0 THEN
    Fichier.open(Chemin +FileKwh$,fmcreate)
    Fichier.close
  end if
  Fichier.open(Chemin + FileKwh$,fmopenwrite)
  C$= Date$+";"+Time$+";"
          Fichier.writeline(C$)
          for ia=0 to 23
           C$= format$("%6.3f", KWH_Day_Store(ia)) + ";"+format$("%3.2f",Temper_Day(ia))
           Fichier.WriteLine  C$
           KWH_Day_Store(ia)=0      ' on le RAZ au fur et à mesure
           Temper_Day(ia)=0
          next ia
   Fichier.close
    Label5.Visible= False
   Label5.Enabled = False

 end sub



Sub Save_INI_file
 IF FILEEXISTS(Chemin + Ini) = 0 THEN
    Fichier.open(Chemin + Ini,fmcreate)
    Fichier.close
 ELSE
    KILL Chemin + Ini
    Fichier.open(Chemin + ini,fmcreate)
    Fichier.close
  END IF

 if Fileexists(Chemin + "$$$$$.$$$") <> 0 then
   kill Chemin + "$$$$$.$$$"
 end if

 Fichier.open(Chemin + Ini,fmopenwrite)
          Fichier.writeline(STR$(EDF_BT.top))
          Fichier.writeline(STR$(EDF_BT.left))

          Fichier.WriteLine  str$(Speed_Combo2.ItemIndex)
          Fichier.WriteLine  str$(Parity_Combo3.ItemIndex)
          Fichier.WriteLine  str$(Nb_Datas_Combo4.ItemIndex)
          Fichier.WriteLine  str$(Nb_Stop_Combo5.ItemIndex)
          Fichier.WriteLine  str$(Port_Combo1.ItemIndex)
          ' Dernier releve de facturation EDF
          Fichier.WriteLine  Dernier_Releve.text
          Fichier.WriteLine  format$("%4.3f",Prix_Kwh)

       Fichier.close
end sub


Sub EDF_BTClose(Action AS INTEGER)
   Save_INI_file
   closecom
    Application.terminate
end sub


Sub Message(Situation as single)
 select case situation
   case 1 :
      messagebox( "Probleme sur votre port " + Com,"COM Setup",0)
  case 2 :
       messagebox("Selectionner un port Com1, Com2, Com3, ..COM20","COM Setup",0)
  case 3 :
      messagebox("Configuration incomplete, PROBLEMO! ","Aie ! Aie",0)
  case 4 :
      messagebox("Bye Bye ..","Info",0)
  case 5:
   messagebox("Chargement de HELP"+chr$(13)+"..à suivre","Information",0)
 case 10 :
    messagebox("Non implanté","info",0)
 case else
 end select
end sub



sub Activer_Port_COM()
    CLEARBUFFER
    CLOSECOM
    String_Comm = Com & "," & Baud & "," & Parite & "," & Nb_Datas & "," & Nb_Stop
    L2=len(String_Comm )
   C$="Version "+ Version +chr$(13)+"chaine setup : "+chr$(13)+ string_Comm+chr$(13)+" Len=" + STR$(L2)_
    +chr$(13)+Dernier_Releve.text +chr$(13)+ Edit1_Prix_Kwh.text
    messagebox(C$,"Info. COM",0)
     infoCom = OPENCOM(String_Comm)
  If infoCom = 0 Then
    message 1
    Com = ""
    EDF_BT.caption = "Port de comm. non reconnu"
    Port_Combo1.ItemIndex = -1
 else
    Groupe1.caption = "Port ouvert : "+ com + ", " + baud +", "+  Parite + ", "+ Nb_Datas + ", " + Nb_Stop
    CLEARBUFFER
    TXD 0
    RTS 0
    DTR 0
    REALTIME
 end if

End sub


Sub Send_ButtonClick(Sender as QCoolBtn)   '

  if Com = "" or Baud = "" or Parite = "" or Nb_Datas = "" or Nb_Stop = "" then
     message 3
     exit sub
  end if
  A$=Zone_Emission.text
  SENDSTRING (A$)
   if  (Add_CR.checked=true)  then
      SENDBYTE (13)
   end if
   if (Add_LF.checked=TRUE) then
    SENDBYTE (10)
    end if
End Sub



Sub Port_Combo1Change(Sender as QComboBox)
    Com = trim(Port_Combo1.Text)
    Activer_Port_COM
End Sub


Sub Speed_Combo2Change(Sender as QComboBox)
     Baud = trim(Speed_Combo2.Text)
     Activer_Port_COM
End Sub


Sub Parity_Combo3Change(Sender as QComboBox)
     Parite = trim(Parity_combo3.Text)
     Activer_Port_COM
End Sub


Sub Nb_Datas_Combo4Change(Sender as QComboBox)
     Nb_Datas = trim(Nb_Datas_Combo4.Text)
     Activer_Port_COM
End Sub


Sub Nb_StopChange(Sender as QComboBox)
     Nb_Stop = trim(Nb_Stop_combo5.Text)
     Activer_Port_COM
End Sub


Function Trim(Datum As String) As String
  if Datum = "" then exit function
  Trim = LTrim$(RTrim$(Datum))
End Function



Sub Help_buttonClick(Sender as QCoolBtn)
    Zone_Emission.AddString ("Help")
    SENDSTRING  "Help"
     SENDBYTE (13)
End Sub
 

 
Sub MAJ_RTCClick(Sender as QCoolBtn)
 dim Ad$ as string
 Ad$="                               "
 if L2>0 then
    'was   A$="#U;10;11;18;20;12;06;&"
    'is 30-04-2019    A$="U;30;04;19;20;12;06;#"
    'Ad$="#U:"+mid$( Date$,4,2)
    Ad$="U;"+mid$( Date$,4,2)
    Ad$=Ad$+";"
    Ad$=Ad$+ left$(Date$,2)
     Ad$=Ad$+";"
    Ad$=Ad$+right$( Date$,2)
    Ad$=Ad$+";"
    Ad$=Ad$+left$( Time$,2)
     Ad$=Ad$+";"

    Ad$=Ad$+mid$(Time$,4,2)
     Zone_Emission.Addstring Ad$

    'copy dans zone debugging pour checking
    
    JourSem=JourSemaine()
    
    ' was Ad$=Ad$+";0"+Str$(JourSem)+";&"
    Ad$=Ad$+";0"+Str$(JourSem)+";#"
    SENDSTRING Ad$
    SENDBYTE 13
   ' SENDBYTE 10
    Zone_Emission.text=Ad$
    k=len(Ad$)
    Ad$=Ad$+chr$(13)+"L="+str$(k)
   ' Zone_Debug.Addstring= Ad$
    dow$=mid$(dd$,(JourSem*3)+1,3)
    Label7_wod.caption=Dow$

 end if
End Sub



Function JourSemaine() AS INTEGER
  dim c as integer
  dim n$ as string
  n$=date$
  result=-1
  mm = val(left$(n$,2))
  dd = val(mid$(n$,4,2))
  yy = val(right$(n$,4))
  Zone_Debug.text=str$(mm)+" "+str$(dd)+" "+str$(yy) +chr$(13)
  if ( (mm>0) AND (mm<13) AND (dd>0) and (dd<32) and (yy>2000) and (yy<2100)) then
            c=0
         '   c=c+(yy-1900)*365 + int((yy-1900)/4)
            Numjour=0
            for i=1 to mm-1
                select case i
                    case 1,3,5,7,8,10
                        c=c+31
                    case 2
                        c=c+28
                        if ((yy-1900) mod 4 = 0) then c=c+1
                    case 4,6,9,11
                        c=c+30
               end select
            next
            c=c+dd
            NumJour=c
            c=c+(yy-1900)*365 + int((yy-1900)/4)
      result = (c mod 7)
    '   Zone_Debug.text=chr$(13)+chr$(10)+"result="+str$(result) +chr$(13)
     else
    result=-1
  end if
  END FUNCTION
  
  


SUB JourCalendaire
Dim j as integer
if yy mod 4=0 then
   month(2)=29
   else
    month(2)=28
end if
j=0
Numjour=0
For j=1 to mm-1
 Numjour=Numjour+month(j)
Next j
Numjour=Numjour+dd
Numero_Jour.caption="Jc="+str$(Numjour)
end Sub

  

Sub Raz_Zone_DebugClick(Sender as QButton)
  Zone_Debug.text=""
End Sub

Sub RAZ_ReceptionClick(Sender as QButton)
  Zone_Reception.text=""
End Sub

Sub Raz_Zone_EmissionClick(Sender as QButton )
  Zone_Emission.text=""
End Sub



Sub SortieClick(Sender as QButton)
  Save_EDFinfo_to_File
  Message (4)
  EDF_BTClose(0)
  end
End Sub


Sub CoolBtn2Click(Sender as QCoolBtn)
DrawOrig
End Sub
 

 
Sub Dernier_ReleveChange(Sender as QEdit)

End Sub
 
Sub Edit1_Prix_KWHChange(Sender as QEdit)

End Sub

' modif 26-12-2018

 
Sub CheckPrixClick(Sender as QCheckBox)
 dim kw as single
 dim Lr as single
 if CheckPrix.checked=True then
  Zone_Debug.text=" modif valeurs"
   if len(Dernier_Releve.text)=8 then
       Lr=  val(Dernier_Releve.text)
       if (  lr>50850000 AND lr<99999000 ) then
           Last_Releve =Lr
       end if
   end if
   if len(Edit1_Prix_KWH.text)>3 then
     kw=  val(Edit1_Prix_Kwh.text)
      if (kw>0.10) AND (kw<0.25) then
        Prix_Kwh=kw
      end if
   end if
  CheckPrix.checked=False
  Save_EDFinfo_to_File
  Zone_Debug.text="sauvegardé "
  else
    Zone_Debug.text=""
 end if
End Sub
 
'Sub CheckBox1_TableClick(Sender as QCheckBox)
'  if Checkbox1_Table.checked=True then
'     Graph1.visible=False
'        Timer1.Enabled= False
'     else
'        Graph1.visible=True
'           Timer1.Enabled= True
'        end if
'
'End Sub



Sub CheckBox1_TableClick(Sender as QCheckBox)
dim i as integer
  if Checkbox1_Table.checked=True then
  '@@@  17-01-2019
   EDF_BT.visible=0
     SetWindowLong(Application.Handle,-8,Form2.Handle)
      Form2.visible=1
      Grid.Cell(0,6) = "?"
       MessageBox ("Charger ChauffProgrData.tab"+ chr$(13)+" et click sur Calculs","Consignes Fils Pilotes",0 )

     else

      Form2.visible=0
      SetWindowLong(Application.Handle,-8,EDF_BT.Handle)
      EDF_BT.visible=1
   '       for i=0 to len(ProgChauff$)-1
   '      SENDBYTE MID$(ProgChauff$,i+1,i)
   '       next i
    DOEvents
    
    SENDSTRING  ProgChauff$
    SENDBYTE 13
   Recup_Date()
  FileChauff$="Datas\Consigne_Chauff_Pilotes_"+ Datum$
  ShowMessage("save data to"+chemin+FileChauff$)
  IF FILEEXISTS(chemin + FileChauff$) = 0 THEN
    Fichier.open(chemin + FileChauff$,fmcreate)
    Fichier.close
  end if
  Fichier.open(chemin+FileChauff$,fmopenwrite)
 ' C$= Date$+";"+Time$+";"
  'Fichier.writeline C$
  Fichier.writeline ProgChauff$
  Fichier.close

   end if

End Sub

'============================
Sub btnOnClick (SENDER As QBUTTON)

  Dim i  As Integer                                     'Loop counters...
  Dim j  As Integer



  Select Case Sender.Caption             'Use Caption for "toggle trick"
    Case "Enter Row 0"                   'Careful!, using the popup
      Grid.ToggleEditTopRow              'can screw this up
      btnEdit.Caption = "Exit Row 0"     'toggle the
    Case "Exit Row 0"
      Grid.ToggleEditTopRow
      btnEdit.Caption = "Enter Row 0"    'captions!
    Case "Enter SelMode"
      Grid.ToggleSelMode
      btnSelect.Caption = "Exit SelMode"  'toggle the
    Case "Exit SelMode"
      Grid.ToggleSelMode
      btnSelect.Caption = "Enter SelMode" 'captions!
  End Select
  StatusBar.SimpleText = "      Min Row with Data = " + str$(Grid.MinDataRow) + _
                         "      Max Row with Data = " + str$(Grid.MaxDataRow) + _
                         "      Min Col with Data = " + str$(Grid.MinDataCol) + _
                         "      Max Col with Data = " + str$(Grid.MaxDataCol)
End Sub
'-------------------------------- File Menu ---------------------------------------------------
  SUB mnuFileClick (Sender AS QMenuItem)

    SELECT CASE Sender.Handle
      Case mnuFileNew.Handle
        If Grid.SelMode = True Then           'In Select mode
         btnOnClick(btnSelect)                'Turn it off
        End If
        If Grid.EditColLabels = True Then          'Edit Top Row is Active
          btnOnClick(btnEdit)                 'Turn it off
        End If
        Grid.Initialize

      Case mnuFileOpen.Handle
        If Grid.SelMode = True Then           'In Select mode
          btnOnClick(btnSelect)               'Turn it off
        End If
        If Grid.EditColLabels = True Then          'Edit Top Row is Active
          btnOnClick(btnEdit)                 'Turn it off
        End If
        Grid.Initialize
        Grid.LoadData
        Grid.Col = 1 : Grid.Row = 1
        btCalcul.Enabled=True

      Case mnuFileSave.Handle
        Grid.SaveData (False)
        Form2.Caption = "WinStatZ " + "[" + Grid.CurrFile + "]"

      Case mnuFileSaveAs.Handle
        Grid.SaveData (True)
        Form2.Caption = "WinStatZ " + "[" + Grid.CurrFile + "]"

      Case mnuFilePrint.Handle
        Grid.PrintGrid(PRINTER.PrinterIndex, poPortrait, 1, True)

      Case mnuFileExit.Handle
        Form2Close

    END SELECT

  END SUB

'----------------------------- Edit Menu ------------------------------------------------------
  SUB mnuEditClick (Sender AS QMenuItem)

    SELECT CASE Sender.Handle

      Case mnuEditCut.Handle
        If Grid.SelRange.lCol = 0 Then
          ShowMessage("Select a Range of Cells First")
        Else
         Grid.rngCut
        End If

      Case mnuEditCopy.Handle
        If Grid.SelRange.lCol = 0 Then
          ShowMessage("Select a Range of Cells First")
        Else
          Grid.rngCopy
        End If

      Case mnuEditPaste.Handle
        If Grid.SelExists = 0 Then
          ShowMessage("     No Data on Clipboard\n\nCut or Copy a Selection First")
        Else
          Grid.rngPaste
        End If

      Case mnuEditDelete.Handle
        If Grid.SelRange.lCol = 0 Then
          ShowMessage("Select a Range of Cells First")
        Else
          Grid.rngDelete
        End If

      Case mnuEditSelAll.Handle
        Grid.rngSelAll

      Case mnuEditFillDown.Handle
        Grid.rngFillDown

      Case mnuEditFillRight.Handle
        Grid.rngFillRight

      Case mnuEditFillSeries.Handle
        Grid.rngFillSeries

      Case mnuEditSort.Handle
        Grid.Sort

      Case mnuEditSelMode.Handle
        btnOnClick(btnSelect)

      Case mnuEditRow0.Handle
        btnOnClick(btnEdit)

    END SELECT

  END SUB
'-------------------------------------------------------------------------------------------
Sub Form2Resize (SENDER As QFORM)

  '----- Prevent initial resize exception
  If Form2.Visible = False Then Exit Sub
  'Plus anything else that needs doing in your form resize

  Grid.Width = Form2.ClientWidth- btnEdit.Width
' 18-01-2019
'    Grid.Height = Form2.ClientHeight
  Grid.Height = Form2.ClientHeight -100

End Sub

SUB CheckSync

    mnuEditSelMode.Checked = Grid.SelMode
    mnuEditCut.Enabled = Grid.SelMode
    mnuEditCopy.Enabled = Grid.SelMode
    mnuEditPaste.Enabled = Grid.SelExists
    mnuEditDelete.Enabled = Grid.SelMode
    mnuEditSelAll.Enabled = Grid.SelMode
    mnuEditFillDown.Enabled = Grid.SelRange.bRow > Grid.SelRange.tRow
    mnuEditFillRight.Enabled = Grid.SelRange.rCol > Grid.SelRange.lCol
    mnuEditFillSeries.Enabled = Grid.SelMode
    If mnuEditSelMode.Checked = True Then                   'Out of Sync
      btnSelect.Caption = "Exit SelMode"
    End If
    If mnuEditSelMode.Checked = False Then                  'Out of Sync
      btnSelect.Caption = "Enter SelMode"
    End If

    mnuEditRow0.Checked = Grid.EditColLabels
    If mnuEditRow0.Checked = True Then                      'Out of Sync
      btnEdit.Caption = "Exit Row 0"
    End If
    If mnuEditRow0.Checked = False Then                     'Out of Sync
      btnEdit.Caption = "Enter Row 0"
    End If

  End Sub



Sub btCalculClick (SENDER As QBUTTON)
  Dim i  As Integer                                     'Loop counters...
  Dim j  As Integer
  Dim k  As Integer
  dim P as integer
  dim  a$
  dim c$
  dim V as variant
  

  WITH Grid
      'Label columns w/letters
' .Cell(0,1) = "Cumulus"
' .Cell(0,2) = "Salon  "
' .Cell(0,3) = "SDB    "
' .Cell(0,4) = "CH3-CH2"
 '.Cell(0,5) = "CH1    "
 .Cell(0,6) = "?"
 END with
  ProgChauff$="CHAUFF;"

  p=0
  do
  Grid.Cell(0,6) = "Codage"
  k=0
  FOR j=1 TO 24
    k=0
    FOR i=0 TO 4
     V=Grid.Cell(j,5-i)
     p=i*2
      select case  V

      case "E"
         k=K+   ( 3 shl P )
      case "H"
         K=K +  (2 SHL p )
      case "A"
           k=K+ (1 shl P)  '
      case "C"
        ' rien
      case "M"
         K=k+256
      case "S"
        ' rien
         'K=K and &h00FF
       end select

      NEXT i
       v=str$(k)
       Grid.Cell(j,6)=V
       if (k<100) then  ProgChauff$=ProgChauff$+"0"
       if (k<10) then  ProgChauff$=ProgChauff$+"0"
       ProgChauff$=ProgChauff$+V+";"

  Next J

  StatusBar.Simpletext=ProgChauff$
  
  p= MessageBox ("Envoi --> au PIC"+ chr$(13),"Programme de chauffe",&h03 )
  P=(P AND &h0001) +48
  'SENDSTRING "P="
  'SENDBYTE p
  'SENDBYTE 13
   loop until p=48

  'SENDSTRING ProgChauff$
  'SENDBYTE 13

end sub


Sub Form2Close
  dim C$
  dim b1 as Byte
  dim i as integer
  Checkbox1_Table.checked=False
  DoEvents
End Sub



Sub Rafraichit_Icones
 ' dim cc$     '<-- à mettre en global
  'dim c1$     '<-- à mettre en global
   CC$=codage.caption
  if left$(cc$,1)="M" then image1_1.BMP="_BMP\Voyant_Marche.BMP"
  if left$(cc$,1)="S" then image1_1.BMP="_BMP\Voyant_Stop.BMP"  ' 30-04-2019
  'Zone_Emission.addstring cc$

  C1$=MID$(CC$,2,1)
    select case C1$
  case "A"
  image1_2.BMP="_BMP\ARRET.BMP "
  case "C"
  image1_2.BMP="_BMP\CONFORT.BMP"
  case "E"
  image1_2.BMP="_BMP\ECO.BMP"
  case "H"
  image1_2.BMP="_BMP\HGEL.BMP"
  end select

  C1$=MID$(CC$,3,1)
  select case C1$
  case "A"
  image1_3.BMP="_BMP\ARRET.BMP "
  case "C"
  image1_3.BMP="_BMP\CONFORT.BMP"
  case "E"
  image1_3.BMP="_BMP\ECO.BMP"
  case "H"
  image1_3.BMP="_BMP\HGEL.BMP"
  end select

   C1$=MID$(CC$,4,1)
  select case C1$
    case "A"
  image1_4.BMP="_BMP\ARRET.BMP "
  case "C"
  image1_4.BMP="_BMP\CONFORT.BMP"
  case "E"
  image1_4.BMP="_BMP\ECO.BMP"
  case "H"
  image1_4.BMP="_BMP\HGEL.BMP"
  end select

   C1$=MID$(CC$,5,1)
   select case C1$
   case "A"
  image1_5.BMP="_BMP\ARRET.BMP "
  case "C"
  image1_5.BMP="_BMP\CONFORT.BMP"
  case "E"
  image1_5.BMP="_BMP\ECO.BMP"
  case "H"
  image1_5.BMP="_BMP\HGEL.BMP"

  end select

end sub



Sub Visu_TrameClick(Sender as QCheckBox)
'Code here....
if Visu_Trame.checked=true then
Zone_Reception.left=200
Zone_Reception.Top=230
Zone_Reception.Width=600
Zone_Reception.Height=260
   else
   Zone_Reception.left=496
   Zone_Reception.Top=192
   Zone_Reception.Width=530
   Zone_Reception.Height=33
 end if


End Sub