PowerAnt can be programmed using standard component mscomm32.ocx. This component is created in Microsoft company but not included in any version of MS Windows. May be the reason of this is some errors. (Officially known bug: Memory leak in open/close of RS-232 port). Typically RS-232 port is open at start of the program and close at the end of program. It's make this bug is not so dangerous.
Download mscomm32.ocx (mscomm.zip, 51KB) unzip component and copy it to folder C:\Windows\System.
To tweak this component you must put some keys to MS Windows registry (In the Windows world we do not like easy ways;-)):
REGEDIT4 [HKEY_CLASSES_ROOT\Licenses\4250E830-6AC2-11cf-8ADB-00AA00C00905] @ = "kjljvjjjoquqmjjjvpqqkqmqykypoqjquoun"To do that - just double click in file mscomm.reg (stored into archive mscomm.zip). The RegEdit ask you - would you like to make some changes with System Registry? Your right answer is "Yes".
Run MS Access, create the form. In constructor mode in the form create the Button 0 button and Field3 text field. Push right mouse button on Button 0 and choice menu item "Events processing". The Microsoft Visual Basic editor is opens.
Right this you need to do somesing magical in Microsoft Visual Basic editor. Open menu Tools/References. In window push the button Browse.... Find in filesystem mscomm32.ocx (folder C:\Windows\System). In the window "References" in pane "Available References" you can see checked string "Microsoft Comm Control 6.0".
Copy the followed program source in to editor:
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
Save the program and close the Visual Basic editor
Command 14?? is executed if you press Button 0 button, and device answer is putted into Field3.