Creation menu et choix du port com
Bonjour à tous,
Pour le monitoring et la recherche d’un mot sur les données de passage dans un port com, j’ai essayé d’écrire des lignes de code mais voilà mes problèmes :
1)Le menu (pour le choix du port com à utiliser) m’affiche la liste de tous les ports com de 1 à 15 au lieu de la liste des ports com présents et pas ouverts (dans mon cas, que le port 2)
2)Quand je choisis une deuxième fois le même port (via le menu créé ) il me donne une erreur "port déjà ouvert"
Voici le code :
Code:
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
| Private Sub Form_Load()
Declaration des variables
Dim PortActif As Integer
Dim PremierChoix As Integer
Dim NumPort As Integer
PremierChoix = 0
PortActif = 0
Test des ports actifs et pas ouverts pour la création du menu
For NumPort = 1 To 15
On Error Resume Next
MSComm1.CommPort = NumPort
MSComm1.PortOpen = True
If Err.Number <> 8002 Or Err.Number <> 0 Then
Load choix(NumPort)
choix(NumPort).Caption = NumPort
choix(NumPort).Visible = True
MSComm1.PortOpen = False
End If
Next NumPort
End Sub
Private Sub choix_Click(ChoixPortActif As Integer)
Premier fois que on fait un choix du port com
If PremierChoix = 0 Then
PremierChoix = 1
PortActif = ChoixPortActif
choix(ChoixPortActif).Checked = True
MSComm1.CommPort = ChoixPortActif
MSComm1.PortOpen = True
End If
Choix suivant du port com et control si pas le même (choisir une deuxième fois le même port com)
If PremierChoix <> 0 And PortActif <> ChoixPortActif Then
choix(PortActif).Checked = False
MSComm1.CommPort = PortActif
MSComm1.PortOpen = False
PortActif = ChoixPortActif
choix(ChoixPortActif).Checked = True
MSComm1.CommPort = ChoixPortActif
MSComm1.PortOpen = True
End If
End Sub |