See how this treats you
Option Explicit
Private m_strInitCode As String
Private m_strOnCode As String
Private m_strOffCode As String
Private m_strAckCode As String
Private m_strReadyCode As String
Private m_strChkSum As String
Private m_eMode As PortState
Private Enum PortState
[New Command]
[Command Ok]
[Waiting]
End Enum
'-----------------------------------------------------------------------------------------------------------------------
Private Sub Form_Load()
With MSComm1 'Set up the comm port
.CommPort = 3
.Settings = "4800,N,8,1"
.RThreshold = 1
End With
InitializeCodes 'Sets all the Command Codes
End Sub
'-----------------------------------------------------------------------------------------------------------------------
Private Sub Form_Unload(Cancel As Integer)
If MSComm1.PortOpen Then MSComm1.PortOpen = False
End Sub
'-----------------------------------------------------------------------------------------------------------------------
Private Sub cmdInitialize_Click()
SendCommand m_strInitCode
End Sub
'-----------------------------------------------------------------------------------------------------------------------
Private Sub cmdOff_Click()
If m_eMode = Waiting Then SendCommand m_strOffCode
End Sub
'-----------------------------------------------------------------------------------------------------------------------
Private Sub cmdOn_Click()
If m_eMode = Waiting Then SendCommand m_strOnCode
End Sub
'-----------------------------------------------------------------------------------------------------------------------
Private Sub MSComm1_OnComm()
Dim sBuffer As String
'
If MSComm1.CommEvent = comEvReceive Then
sBuffer = MSComm1.Input 'Read the comm port input data
Select Case m_eMode
Case [New Command] 'We sent a new command, verify the check sum
If sBuffer = m_strCheckSum Then
SendAcknowledge 'Check sum is ok, send an ack
End If
Case [Command Ok] 'We have sent an ack, make sure the device is ready
If sBuffer = m_strReadyCode Then
m_eMode = Waiting 'Set the mode to wait
End If
End Select
lstResponse.AddItem sBuffer 'Display the comm port data to a list
End If
End Sub
'-----------------------------------------------------------------------------------------------------------------------
Private Sub SendCommand(ByVal sCode As String)
SetCheckSum sCode
If Not (MSComm1.PortOpen) Then MSComm1.PortOpen = True
m_eMode = [New Command]
MSComm1.Output = sCode
End Sub
'-----------------------------------------------------------------------------------------------------------------------
Private Sub SendAcknowledge()
If Not (MSComm1.PortOpen) Then MSComm1.PortOpen = True
m_eMode = [Command Ok]
MSComm1.Output = m_strAckCode
End Sub
'-----------------------------------------------------------------------------------------------------------------------
Private Sub SetCheckSum(ByVal sCode As String)
Dim arr() As Byte
Dim iX As Long
Dim iSum As Long
arr() = sCode
For iX = 0 To UBound(arr)
iSum = iSum + arr(iX)
Next
m_strChkSum = Chr$(iSum)
End Sub
'-----------------------------------------------------------------------------------------------------------------------
Private Sub InitializeCodes()
Dim sTmp As String
sTmp = Chr$(&H4)
m_strInitCode = sTmp & Chr$(&H66)
m_strOnCode = sTmp & Chr$(&H62)
m_strOffCode = sTmp & Chr$(&H63)
m_strAckCode = Chr$(&H0)
m_strReadyCode = Chr$(&H55)
End Sub
'-----------------------------------------------------------------------------------------------------------------------
Let me know how it works for you
If you choose to battle wits with the witless be prepared to lose.
![[cheers] [cheers] [cheers]](/data/assets/smilies/cheers.gif)