Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mscomm problem 1

Status
Not open for further replies.

mrrrl

MIS
Dec 26, 2001
179
US
I am tring to commuicate with a cm11a (x10) unit. I am to send it two bytes (test) and then it sends me back the checksum of those. But I can't figure out how to compare the data sent back from the cm11a to get it to then close the port.

TIA

Private Sub Command1_Click()
Dim test
Dim Buffer$
test = Chr(&H4) + Chr(&H66)

Dim Instring As String
MSComm1.CommPort = 1
MSComm1.Settings = "4800,N,8,1"
MSComm1.InputLen = 0
MSComm1.PortOpen = True
MSComm1.Output = test
Do
DoEvents
Buffer$ = Buffer$ & MSComm1.Input
****Here is my problem area********************
Loop Until InStr(Buffer$, ?????????)

'Read the "6A" response data in the serial port.
' Close the serial port if "6A".
************************************************
MSComm1.PortOpen = False

End Sub
 
Ultimately this is going to be a module or maybe an activex component if I get futher, in a larger program. It will be sent the house and device code and also any extended command info i.e. dimming. It would then complete the operation and write the actions and results in a log file.

Also here is what I have done to over come the timing issues. I now have 3 buttons on, off and flush.

Option Explicit
Private Declare Function Sleep Lib "kernel32" (ByVal SleepTime As Long) As Long
Private m_strInitCode As String
Private m_strAckCode As String
Private m_strOnCode As String
Private m_strOffCode As String
Private m_strBuffer As String
'**********************************************************

Private Sub cmdFlush_Click()
FlushBuffer
lstResponse.Clear
End Sub
'**********************************************************

Private Sub cmdOn_Click()
Dim x
FlushBuffer
If MSComm1.PortOpen = False Then MSComm1.PortOpen = True
MSComm1.Output = m_strInitCode

Sleep 1000
MSComm1.Output = m_strAckCode
Sleep 1000
MSComm1.Output = m_strOnCode
Sleep 1000
MSComm1.Output = m_strAckCode


End Sub
'**********************************************************

Private Sub cmdOff_Click()
Dim x
FlushBuffer
If MSComm1.PortOpen = False Then MSComm1.PortOpen = True
MSComm1.Output = m_strInitCode
Sleep 1000
MSComm1.Output = m_strAckCode
Sleep 1000
MSComm1.Output = m_strOffCode
Sleep 1000
MSComm1.Output = m_strAckCode
End Sub
'**********************************************************


Private Sub Form_Load()
MSComm1.CommPort = 1
MSComm1.Settings = "4800,N,8,1"
MSComm1.RThreshold = 1
m_strInitCode = Chr$(&H4) & Chr$(&H66)
m_strAckCode = Chr$(&H0)
m_strOnCode = Chr$(&H6) & Chr$(&H62)
m_strOffCode = Chr$(&H6) & Chr$(&H63)
End Sub
'**********************************************************

Private Sub Form_Unload(Cancel As Integer)
If MSComm1.PortOpen Then MSComm1.PortOpen = False
End Sub
'**********************************************************

Private Sub MSComm1_OnComm()
Select Case MSComm1.CommEvent
Case comEvReceive
m_strBuffer = m_strBuffer & MSComm1.Input
End Select
lstResponse.AddItem m_strBuffer
Text1 = m_strBuffer
End Sub
'**********************************************************

Private Sub FlushBuffer()
m_strBuffer = vbNullString
End Sub

 
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 [spin]

If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
OK, got it to work found three items I had to change.

If sBuffer = m_strCheckSum Then to
If sBuffer = m_strChkSum Then

and

m_strOnCode = sTmp & Chr$(&H62)
m_strOffCode = sTmp & Chr$(&H63) to

m_strOnCode = Chr$(&H6) & Chr$(&H62)
m_strOffCode = Chr$(&H6) & Chr$(&H63)

This one because this corresponds to code A1. Which I will have to change to accept all house and device codes.

It worked great and wow, is much faster in response time in turning the lamp on and off.

I really appreciate all the time you spent with me on this, I learned alot and hopefully I peaked your interest in CM11a/X10 home automation/control in the process.
 
Great :-D

Sorry about the the typos :-(, I just threw out some things. The sTmp was just to save some conversion time and for some reason I thought all the commands used the &H4. Just a few other ideas you may want to implement. You could verify the checksum and if there is an error in it, attempt to re-send the command. You could do so by adding to the OnComm event. Maybe something like this:

Private Sub MSComm1_OnComm()
Dim sBuffer As String
Static iTries as Long
'
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_strChkSum And iTries < 5 Then
SendAcknowledge 'Check sum is ok, send an ack
iTries = 0
Else
'Some Code Here to re-send command
iTries = iTries + 1
Exit Sub
End If
If iTries Then MsgBox &quot;Error&quot;
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

You most likely should also add some error handling as I did not add any. By the way my primary job focus deals with PLCs and the implementing automation controls for door access, camera call ups, intercoms, etc. Good Luck [Peace]

If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
That code will never show the error message as the &quot;Exit Sub&quot; will leave the procedure. Sorry about that, but hopefully you get the idea. If you need more help with tring to implement that let me know and I will see if I can come up with a better solution. Good Luck. [spin]

If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
OK I am trying to condense this code down so that I can use it as a module so all that is required is input the values of the house code, device code and command.

My problem is with the MSComm.OnComm. I am not that familiar with it, but it appears that it only fires when you are exiting or out of a sub routine. Is there a way to fire it in a sub routine?

Foada, also if you want to answer this offline, it's getting kind of large, my email is rlance@theeyeinstitute.com.

TIA
 
I will drop you an email. [spin]

If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
Hi mrrrl,

As this thread is a number of weeks old now, I don't know how far you have got with your project.
I have just taken delivery of my CM12U (same as CM11 but UK version) and I am going to be going through the same steps as you have done here.
I suppose my one advantage is that I have some experience of the MSComm control.
I have been scouring the find an ActiveX control to save me doing the bulk of the basic stuff, but it looks like I am going to have to do it anyway.

I will be creating quite a &quot;pretty&quot; application, giving the ability for the end user to add a house &quot;map&quot; to the application and to add devices in their relative locations. Ultimately my new house (which I hopefully move to next week) will be completely X10 controlled, including adding X10 to my home designed security system.

Regards
Jim
 
Hi jimfranklin,

I have not worked on it for so time due to the holidays and work. I have gotten a little further in the project. I also could not find an ActiveX component that would work.

If you would like to work on a project together, at least until we can get the CM11a/CM12u module working that would be great.

Let me know, my email is a couple of replies up.

mrrrl
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top