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
|
Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMillisenconds As Long)
Private Sub CommandButton1_Click()
If MSComm1.PortOpen = True Then MSComm1.PortOpen = False ' si le bouton 1 = true alors on fait appel à la fonction test_liaison
Call test_liaison
End Sub
Sub test_liaison()
Dim trame_recue As String
Dim buffer As String
mouchard = 1
MSComm1.CommPort = 3 'Comm1.CommPort = Sheets("Test&Réglage & PV").Range("port").Value 'permet de recuperer le numero du port com
MSComm1.Settings = "9600,N,8,1" 'configuration liaison série ( tjrs la même chose ) ' 9600 bauds, pas de parité, 8 bits de données et 1 bit d'arrêt.
MSComm1.PortOpen = True 'ouverture du port pour debuter la communication
MSComm1.SThreshold = 1
MSComm1.InputLen = 0 ' lecture total du buffer
MSComm1.Output = ActiveSheet.Cells(6, 9).Value & vbCrLf ' envoi la saisie dans la cellule D2
Do
DoEvents 'indispensable sinon ça marche pas ( boucle de reception )
trame_recue = trame_recue & MSComm1.Input
Loop Until InStr(trame_recue, vbCrLf)
fin:
MSComm1.PortOpen = False 'on ferme le port afin de terminer la communication
If mouchard = 1 Then 'si une trame a bien été envoyée
MsgBox ("Opération terminée avec succès!") 'alors les caracteres sont bons et la communication est correcte
Else
MsgBox ("Test échoué... nombre de trame envoyée par le pc :" & mouchard) 'sinon pb dans les caracteres
End If
End Sub 'fin |
Partager