PowerAnt peut être programmer en utilisant les composants standard mscomm32.ocx. Ces composants sont créés par la société Microsoft mais ne sont pas inclus dans chaque version de Windows. Cela doit être la raison pour laquelle il y'a a quelques problèmes. (Les bug officiellement connu: Fuite mémoire à l'ouverture/fermeture du port RS-232 port). Typiquement, le port RS-232 est ouvert au démarrage du programme et fermé à la fin du programme. Ce qui fait que ce bug n'est pas si dangereux.
Téléchargez mscomm32.ocx (mscomm.zip, 51KB) dézippez le composant et copiez le dans le répertoire C:\Windows\System.
Pour rendre accessible ce composant, vous devez mettre quelques clés dans le registre Windows (Dans le monde Windows nous n'aimons pas les voies faciles;-)):
REGEDIT4 [HKEY_CLASSES_ROOT\Licenses\4250E830-6AC2-11cf-8ADB-00AA00C00905] @ = "kjljvjjjoquqmjjjvpqqkqmqykypoqjquoun"Pour ce faire, cliquez sur le fichier mscomm.reg (présente dans le l'archive mscomm.zip). Une fenêtre vous demande si souhaitez mettre à jour votre base de registre, vous devez répondre "Oui".
Excecutez MS Access, créer un formulaire. En mode "construction", créez le bouton Button 0 et la zone de texte Field3. Clique droit de la souris sur l'objet Button 0 puis selectionner "Procédure événementielle". L'éditeur Visual Basic de Microsoft est désormais ouvert.
Right this you need to do somesing magical in Microsoft Visual Basic editor. Allez dans le menu Outils/References. Cliquez maintenant sur le bouton Parcourir.... Chercher le fichier mscomm32.ocx (repertoire C:\Windows\System). Dans la fenêtre "References" dans la partie "References disponibles" vous pouvez voir la ligne "Microsoft Comm Control 6.0" cochée, cliquez sur ok.
Copiez le programme suivant dans l'éditeur:
Option Compare Database
Dim PwrAntComm As New MSComm
' When the "Button 0" is push, the command 14?? is executed
' and the device answer is putted into Field3
Private Sub Button0_Click()
' 2 - COM port bumber (COM1 = 1, COM2 = 2, ... )
PwrAntOpen (2)
' Ask device with address 14 - "who are you, and what you can do?".
Field3 = PwrAntCmd("14", "??")
End Sub
Sub Form_Unload(Cancel As Integer)
Call PwrAntClose
End Sub
' Open RS-232 port, if this port is not be opened
' PortNumber - Number of COM port (COM1 = 1, COM2 = 2, ... )
Sub PwrAntOpen(PortNumber As Integer)
If Not PwrAntComm.PortOpen = True Then
PwrAntComm.CommPort = PortNumber
PwrAntComm.Settings = "9600,N,8,1"
PwrAntComm.Handshaking = comNone
PwrAntComm.InputLen = 0
PwrAntComm.InBufferSize = 40
PwrAntComm.OutBufferSize = 40
PwrAntComm.PortOpen = True
PwrAntComm.RThreshold = 0
PwrAntComm.Output = Chr(27) ' Clear the receive buffer in all devices
End If
End Sub
' Close RS-232 port, if this port be opened
Sub PwrAntClose()
If PwrAntComm.PortOpen = True Then
PwrAntComm.PortOpen = False
End If
End Sub
' This function execute the given command and return the device answer.
' If something wrong (you talk with nonexisting device) then
' function is return the empty string. Maximum command execution
' time is limited to 2 sec.
Function PwrAntCmd(AntName As String, AntCmd As String) As String
Dim AntInStr As String
Dim TimeOut As Date
' Send command to device
PwrAntComm.Output = AntName & AntCmd & Chr(13)
' Read string by bytes from device and wait byte with code 0x0D
AntInStr = ""
TimeOut = Time
Do
dummy = DoEvents() ' Let system do something, we do not need freeze the system
If PwrAntComm.InBufferCount > 0 Then
AntInStr = AntInStr & PwrAntComm.Input
End If
Loop Until (Right(AntInStr, 1) = Chr(13)) Or (Time - TimeOut > 1)
' Remove 0x0D at the end of string
AntInStr = Left(AntInStr, Len(AntInStr) - 1)
PwrAntCmd = AntInStr
End Function
Sauvegarder le programme et fermer l'éditeur Visual Basic
Commande 14?? est executée si vous appuyez sur le bouton Button 0, et la réponse du module est inscrite dans la zone de texte Field3.