1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
| 'Déclaration des variables
Dim colonne1f1 As Range
Dim colonne1f2 As Range
Dim colonne2f1 As Range
Dim colonne2f2 As Range
Public Lecture As Boolean
Public TestCom As Boolean
Public construction_message As String
Public Trame_lecture As String
Public Trame_reception As String
Public MotDeb As String
Public MotFin As String
Public Recupmot As String
Public i As Integer
Public NbrMot As Integer
Public Bfin As Integer
Public suite As Integer
Public suitefin As Integer
Public watchdog
Public EvenementHorl As Boolean
' -------------------------------
' | Initialisation du Userform |
' -------------------------------
Private Sub UserForm_Initialize()
ProgressBar1.Visible = False
Bp_stop.Enabled = False 'Interdire le bouton stop
Call effacement
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
End If
End Sub
' -------------------------
' | Test de communication |
' -------------------------
Sub Bp_test_Click()
On Error GoTo erreur 'gestion d'erreur
TestCom = True 'Flag de test de communication.
NbrMot = 1 'test du premier mot
MotDeb = 1
suite = 1
i = 1
'****** Paramétrage de la communcation *********
If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
MSComm1.CommPort = selection_port_serie.Value 'Paramétrage du port
MSComm1.Settings = "9600,E,7,2" 'Paramètrage de la communication
'MSComm1.Handshaking = comRTS
MSComm1.RThreshold = 1
MSComm1.SThreshold = 1
'MSComm1.InputMode = comInputModeText
MSComm1.InputLen = 0
MSComm1.PortOpen = True 'Ouverture du port
'***********************************************
suitefin = 0
Lecture = True
Call trame
Exit Sub
erreur: MsgBox "Le port séléctionné est invalide", vbOKOnly, "Attention"
End Sub
'**********************
'* Demande de Lecture *
'**********************
Sub Reception_Click()
On Error GoTo erreur 'gestion d'erreur
Call présentation
NbrMot = 72
MotDeb = 5008
If MotDeb + NbrMot > 9999 Then
MsgBox "Vous ne pouvez pas lire les mots au dessus de D9998", vbOKOnly, "Saisie Incorrect"
ProgressBar1.Value = 0
ProgressBar1.Visible = False
Exit Sub
End If
'***** Paramétrage communication ***********************
If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
MSComm1.CommPort = selection_port_serie.Value 'Paramétrage du port
MSComm1.Settings = "9600,E,7,2" 'Paramètrage de la communication
'MSComm1.Handshaking = comRTS
MSComm1.RThreshold = 1
MSComm1.SThreshold = 1
'MSComm1.InputMode = comInputModeText
MSComm1.InputLen = 0
MSComm1.PortOpen = True 'Ouverture du port
'*******************************************************
ProgressBar1.Visible = True 'Apparition de la barre d'avancement
ProgressBar1.Min = 0 'Initialisation des paramètres min,max de la barre d'avancement
ProgressBar1.Max = NbrMot
suitefin = 0
suite = MotDeb
Lecture = True
EvenementHorl = True
Dbt = Timer 'Gestion du Timer
watchdog = Timer 'watchdog de securité
i = 1 'selection de la premier cellule à écrire
While (EvenementHorl = True) ' Début boucle Timer
'***** Gestion du temps max d'inactivité lors de la communication *****
If watchdog > (Timer + 5000) Then ' ( Timer + Temps d'attente) si le watchdog s'enclenche alors on affiche un message d'erreur et on retourne a la configuration
MsgBox "Le temps est dépassé", vbOKOnly, "Erreur de communication"
ProgressBar1.Visible = False 'on cache la barre de progression
ProgressBar1.Value = 0 'la barre de progression est réinitialisé à 0
MSComm1.PortOpen = False 'Fermeture du port COM
Exit Sub
Else
watchdog = watchdog + 0.075
End If
'**********************************************************************
'***** Gestion du timer d'evenement ***********************************
If Timer > Dbt + 0.1 And Timer < Dbt + 0.2 And Timer < (watchdog + 300) Then
DoEvents
Else
DoEvents
Call trame
End If
If Timer > Dbt + 0.2 Then Dbt = Timer
'*********************************************************************
Wend 'Fin boucle du timer
Exit Sub 'on quitte le sub
'***** Gestion d'une erreur éventuelle **********
erreur: MsgBox "Port COM invalide ou problème interne"
'************************************************
End Sub
Sub trame()
If suitefin > NbrMot Then
EvenementHorl = False 'Arret de l'evenement horloge
MSComm1.PortOpen = False 'Fermeture du port com
ProgressBar1.Value = 0 'Init de la barre de progression
ProgressBar1.Visible = False 'On cache la barre de progression
End If
'********** test mot fin ************************************************
If (NbrMot - suitefin) >= 30 Then MotFin = "0030" 'Si il reste plus de 30 mot à lire on en lit 30
If (NbrMot - suitefin) < 30 Then
If (NbrMot - suitefin) < 10 Then MotFin = "000" & (NbrMot - suitefin)
If (NbrMot - suitefin) >= 10 Then MotFin = "00" & (NbrMot - suitefin)
End If
'************************************************************************
'********** Mise en Forme de MotDeb *************************************
If suite < 10 Then MotDeb = "000" & suite
If suite > 10 And suite < 100 Then MotDeb = "00" & suite
If suite > 100 And suite < 1000 Then MotDeb = "0" & suite
If suite > 1000 Then MotDeb = suite
'************************************************************************
Trame_lecture = "@" + "00RD" + MotDeb + MotFin 'Trame lecture 30 mots maxi
q = 0 'initialisation
For boucle = 1 To Len(Trame_lecture) 'Calcul_fin_de_Trame [FCS de (trame_lecture)]
q = q Xor asc(Mid(Trame_lecture, boucle, 1))
Next boucle
fcs = Hex(q) 'Conversion en Hexa "fcs"
Trame_lecture = Trame_lecture + fcs + "*" 'assemblage de la trame lecture
Trame_reception = "@" + "00WD" + "9999" + "FFFF" 'Trame d'acusé reception : FFFF au dm3100
q = 0 'initialisation
For boucle = 1 To Len(Trame_reception) 'Calcul_fin_de_Trame [FCS de (trame_lecture)]
q = q Xor asc(Mid(Trame_reception, boucle, 1))
Next boucle
fcs = Hex(q) 'Conversion en Hexa "fcs"
Trame_reception = Trame_reception + fcs + "*" 'assemblage de la trame reception
Call Communication
End Sub
Sub Communication()
If Lecture = True Then 'Gestion de la demande de lecture
construction_message = ""
If MSComm1.PortOpen = True Then MSComm1.Output = Trame_lecture + chr(13) 'Envoi trame lecture
Lecture = False 'Reset Demande Lecture
End If
Call mise_en_page
End Sub
Sub MSComm1_OnComm()
watchdog = Timer 'Initialisation du watchdog
If MSComm1.InBufferCount <> 0 Then 'Ecoute buffer réception
construction_message = construction_message + MSComm1.Input 'Reception Buffer
End If
If InStr(construction_message, "*") <> 0 Then 'Teste fin trame
TypeTrame = Mid(construction_message, 4, 2) 'On sépare le type de trame du reste de la trame (RD)
If (TypeTrame = "RD" And Mid(construction_message, 6, 2) = "00") Then 'test retour de la lecture de données
If (NbrMot - suitefin) >= 30 Then Bfin = 124
If (NbrMot - suitefin) < 30 Then Bfin = 4 + ((NbrMot - suitefin) * 4)
suitefin = suitefin + 30 'On ajoute 30 à la valeur de MotFin
suite = suite + 30 'On ajoute 30 à la valeur de MotDeb
'***** Test communication reussi ? **************
If (TestCom = True) Then 'Vérification test reussi
MsgBox "La communication réussie", vbOKOnly, "Test communication"
TestCom = False
MSComm1.PortOpen = False
Exit Sub
End If
'************************************************
'***** Séparation des valeurs des mots et ecriture dans une cellule *****
Worksheets("réception").Select
For f = 8 To Bfin
Recupmot = Mid(construction_message, f, 4) 'récupération du mot
Cells(i, 1).Select 'selection de la cellule
Selection.HorizontalAlignment = xlCenter
Selection.NumberFormat = "@" 'cellule selectionné formater en format texte
Cells(i, 1) = Recupmot 'On écris la valeur du mot dans la cellule séléctionnée
i = i + 1 'On change de cellule
f = f + 3 'On passe au prochain mot de la trame reçu
If ProgressBar1.Value + 1 < ProgressBar1.Max Then ProgressBar1.Value = ProgressBar1.Value + 1 'mise a jour de la barre de progression si elle n'est pas à fond
Next f
'************************************************************************
construction_message = ""
If MSComm1.PortOpen = True Then MSComm1.Output = Trame_reception + chr(13) 'envoi trame de l'accusé réception
Else
Lecture = True 'Autorisation de lecture
End If
End If
End Sub
Private Sub Bp_stop_Click()
EvenementHorl = False
ProgressBar1.Value = 0
ProgressBar1.Visible = False
End Sub
Private Sub selection_port_serie_Change()
'-----===== Séléction du port =====-----
Select Case (selection_port_serie.Value)
Case (1): Numport.Text = "COM 1"
Case (2): Numport.Text = "COM 2"
Case (3): Numport.Text = "COM 3"
Case (4): Numport.Text = "COM 4"
Case (5): Numport.Text = "COM 5"
Case Else: selection_port_serie.Value = 1
End Select
'----------------------------------------
End Sub
' ----------------
' | Mise en page |
' ----------------
Sub présentation()
Worksheets("rapport").Select
[D2] = "Bernay Automation"
Range("B2:F2").Select
Selection.Merge
Selection.Interior.ColorIndex = 3
Selection.Font.Size = 18
Selection.Font.ColorIndex = 6
Selection.HorizontalAlignment = xlCenter
[D5] = "Contrôle de candence"
Range("B5:D5").Select
Selection.Merge
Selection.Interior.ColorIndex = 4
Selection.Font.Size = 12
Selection.HorizontalAlignment = xlCenter
[B7] = "Numéro d'affaire:"
Range("B7:E7").Select
Selection.Interior.ColorIndex = 37
Selection.Font.Size = 12
Selection.HorizontalAlignment = xlCenter
Range("B7:C7").Merge
[B45] = "Nb de pièces comptées:"
[G45] = "pcs"
Range("B45:G45").Select
Selection.Interior.ColorIndex = 37
Selection.Font.Size = 12
Selection.HorizontalAlignment = xlCenter
Range("B45:D45").Merge
[B47] = "cadence moyenne:"
[G47] = "pcs/min"
Range("B47:G47").Select
Selection.Interior.ColorIndex = 37
Selection.Font.Size = 12
Selection.HorizontalAlignment = xlCenter
Range("B47:D47").Merge
Range("E47:F47").Merge
[B49] = "Temps de contrôle:"
[G49] = "min"
Range("B49:G49").Select
Selection.Interior.ColorIndex = 37
Selection.Font.Size = 12
Selection.HorizontalAlignment = xlCenter
Range("B49:D49").Merge
Range("E49:F49").Merge
End Sub
Private Sub mise_en_page() 'mise en page des valeurs après réception
Worksheets("réception").Select 'remise en page du paquet de données recues
Set colonne1f2 = Worksheets("réception").Range("A13:A42")
Set colonne2f2 = Worksheets("réception").Range("A43:A72")
Worksheets("rapport").Select
[D7] = Worksheets("réception").Range("E1") 'numero d'affaire
[E45] = Worksheets("réception").Range("E3") 'Nb de pièces comptées
[E47] = Worksheets("réception").Range("A7") 'cadence moyenne
[E49] = Worksheets("réception").Range("A8") 'Temps de contrôle
[D7] = Worksheets("réception").Range("A2") 'numéro d'affaire
Range("D7").Select
Selection.HorizontalAlignment = xlRight
[E7] = Worksheets("réception").Range("A1")
Range("E7").Select
Selection.HorizontalAlignment = xlLeft
[E45] = Worksheets("réception").Range("A12") 'numéro d'affaire
Range("E45").Select
Selection.HorizontalAlignment = xlRight
[F45] = Worksheets("réception").Range("A11")
Range("F45").Select
Selection.HorizontalAlignment = xlLeft
Set colonne1f1 = Range("C10:C39")
Set colonne2f1 = Range("F10:F39")
colonne1f1.Value = colonne1f2.Value
colonne2f1.Value = colonne2f2.Value
[F5] = Date
End Sub
Private Sub Raz_Click()
Call effacement
End Sub
' ---------------------------
' | Effacement des feuilles |
' ---------------------------
Private Sub effacement()
Worksheets("réception").Select
Columns.Clear 'supprime les valeurs affichées dans toutes les cellules de la feuille "réception"
Worksheets("rapport").Select
Columns.Clear 'supprime les valeurs affichées dans toutes les cellules de la feuille "rapport"
End Sub
' --------------------------
' | Fermeture du Userform |
' --------------------------
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
'Application.SaveWorkspace 'Sauvegarde le fichier excel
'Application.Quit 'Ferme Excel
End Sub |
Partager