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
 
I don't exactly get the problem but:

If you know how many bytes can be expected, it might be easier to wait until that number of bytes has been arrived in the input buffer, then read the buffer and then check the data.

If it's just a problem of comparing "6A" with the data, you can do this like Instr(Buffer$, Chr(106)).

Greetings,
Rick
 
Thanks for getting back with me. I tried that but I get the same results. I guess I don't understand how to read the buffer.

When I add Text1 = Buffer$ to the loop I get a nothing in Text1 box.

Do
DoEvents
Buffer$ = Buffer$ & MSComm1.Input
Text1 = Buffer$

Loop Until InStr(Buffer$, Chr(106))

But I am watching the serial port with a serial port monitor and I can see the read request and the answer (6A). I just can't see it in my program, so the loop never stops.
 
Try to set the RThreshold to 1 and then see if a COMM event occurs (i think with ID comEvRecv, but not sure anymore; long time ago...) when the 0x60 arrives at the port.

And are you sure the comm settings are correct?

Greetings,
Rick
 
No change. I believe the settings are correct as I can see the port open, the first data being sent (Chr(&H4) + Chr(&H66)) and the answer being read 0x6A in the serial port monitor program. I just can't display or use the data sent back from the buffer to then see if it is the right answer and then finish the rest of the program.
 
Here is what I am seeing in one window of the port monitoring program.

Port opened by process "VB6.EXE" (PID: 4008)

Request: 11/18/2003 9:11:18 AM.421875064 (+32.5312500000 seconds)

04 06

Answer: 11/18/2003 9:11:18 AM.437500064 (+0.0156250000 seconds)

6A
 
OK, when I add this line in the loop I get 0 all the time.

Text1 = MSComm1.InBufferCount

So it seems that it is not reading the buffer. So....
 
Try adding a second command button and then change the code to this

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
MSComm1.PortOpen = False
End Sub

Private Sub Command2_Click()
MSComm1.PortOpen = True
Command2.Caption = MSComm1.Input
MSComm1.PortOpen = False
End Sub

And see what the caption on the Commmand2 command button is.

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

[cheers]
 
OK, the caption on the second command button is blank.

Watching the port monitor I can see the request go out and the port close when clicking on the first command button.

Clicking the second command button shows the port opening and then closing. But nothing getting read.

I think this maybe a timing issue? When I added MSComm1.InBufferCount I got 0, but if I put a delay before it it now shows 1.
 
My bad, try this

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
' MSComm1.PortOpen = False
End Sub

Private Sub Command2_Click()
Dim iX As Long

' MSComm1.PortOpen = True
Command2.Caption = MSComm1.Input
MSComm1.PortOpen = False
End Sub

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

[cheers]
 
All right! that worked, it showed a lower case i. Thanks now I will see if I can get it to work in my code. Thanks!
 
How about something like this

Option Explicit
Dim buffer As String

Private Sub Command1_Click()
Dim test

test = Chr(&H4) + Chr(&H66)
If MSComm1.PortOpen = False Then MSComm1.PortOpen = True
MSComm1.Output = test
End Sub

Private Sub Command2_Click()
buffer = vbNullString
End Sub

Private Sub Form_Load()
MSComm1.CommPort = 3
MSComm1.Settings = "4800,N,8,1"
MSComm1.RThreshold = 1
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
buffer = buffer & MSComm1.Input
Command1.Caption = buffer
End Select
End Sub

Let me know if that is what you are looking for. [spin]

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

[cheers]
 
Sorry, make sure you change the CommPort property back to 1. Note that the second command button clears the buffer.

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

[cheers]
 
Close, here is what I have so far. It turns on and off the lamp, but I can't get the answer (checksum) codes to show up in the text box. (2 command and one text box).

The way it works is that you send the CM11a two bytes. It sums them up and sends you a checksum. If correct you send the CM11a 0x00. Then the CM11a sends 0x55 stating that it is in the ready mode. You then send it the codes to turn on, off or dim the light. It then sends back a checksum again. If correct you send it 0x00. It completes the action and then sends back 0x55.

So what I am trying to do is to get these checksums to check them before I send the next command. Right now I am just using a delay to make sure the CM11a sends back the answer before I give it then next command. I have no way of knowing if the ckecksum is correct except that I am watching it via a serial port monitor.

Hope that helps explain better on what I am doing.

Option Explicit
Private Sub Form_Load()
MSComm1.CommPort = 1
MSComm1.Settings = "4800,N,8,1"
MSComm1.InputLen = 0
End Sub
Private Sub Command1_Click()
Dim code, code1, code2, x
Dim Buffer

code = Chr(&H4) + Chr(&H66) 'Init CM11a
code1 = Chr(&H0)
code2 = Chr(&H6) + Chr(&H62) 'Turn On Code

If MSComm1.PortOpen = False Then MSComm1.PortOpen = True

MSComm1.Output = code

For x = 1 To 10000000
Next x
MSComm1.Output = code1

For x = 1 To 100000000
Next x
MSComm1.Output = code2

For x = 1 To 10000000
Next x
MSComm1.Output = code1


End Sub

Private Sub Command2_Click()
Dim code, code1, code2, x
Dim Buffer$

code = Chr(&H4) + Chr(&H66) 'Init CM11a
code1 = Chr(&H0)
code2 = Chr(&H6) + Chr(&H63) 'Turn Off Code

If MSComm1.PortOpen = False Then MSComm1.PortOpen = True

MSComm1.Output = code

For x = 1 To 10000000
Next x
MSComm1.Output = code1

For x = 1 To 100000000
Next x
MSComm1.Output = code2

For x = 1 To 10000000
Next x
MSComm1.Output = code1

End Sub

Private Sub MSComm1_OnComm()
Select Case MSComm1.CommEvent
Case comEvReceive
Buffer = Buffer & MSComm1.Input
Text1 = Buffer
'Command1.Caption = buffer
End Select
End Sub
Private Sub Form_Unload(Cancel As Integer)
If MSComm1.PortOpen Then MSComm1.PortOpen = False
End Sub
 
How about this

Option Explicit

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 cmdAcknowledge_Click()
FlushBuffer
If MSComm1.PortOpen = False Then MSComm1.PortOpen = True
MSComm1.Output = m_strAckCode
End Sub
'-----------------------------------------------------------------------------------------------------------------------
Private Sub cmdFlush_Click()
FlushBuffer
lstResponse.Clear
End Sub
'-----------------------------------------------------------------------------------------------------------------------
Private Sub cmdInitialize_Click()
FlushBuffer
If MSComm1.PortOpen = False Then MSComm1.PortOpen = True
MSComm1.Output = m_strInitCode
End Sub
'-----------------------------------------------------------------------------------------------------------------------
Private Sub cmdOff_Click()
FlushBuffer
If MSComm1.PortOpen = False Then MSComm1.PortOpen = True
MSComm1.Output = m_strOffCode
End Sub
'-----------------------------------------------------------------------------------------------------------------------
Private Sub cmdOn_Click()
FlushBuffer
If MSComm1.PortOpen = False Then MSComm1.PortOpen = True
MSComm1.Output = m_strOnCode
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
End Sub
'-----------------------------------------------------------------------------------------------------------------------
Private Sub FlushBuffer()
m_strBuffer = vbNullString
End Sub
'-----------------------------------------------------------------------------------------------------------------------

Note there are 5 Command Buttons(cmdInitialize, cmdAcknowledge, cmdOn, cmdOff, and cmdFlush) and a ListBox(lstResponse). Each button will send the corresponding code and the list box will display the response. [spin]

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

[cheers]
 
Can't get the CM11a ack code 0x55. The Init code sends and I receive the checksum. Click the ack button it sends the 0x00, but the CM11a never sends back 0x55. Doubled checked with other code to make sure Cm11a working.

I'll keep working with what you gave me...
 
OK, back to the timing issue. If I click the init, ack, on, ack buttons in that order and fast enough it all works. So there is nothing wrong with you coding it's just a timing issue. The CM11a just times out waiting.

Thanks for all your help.
 
A couple of questions for you. Does the CM11a protocol always use 2 bytes of data? Does the protocol state how the checksum is calculated? The reason I ask is if it always uses 2 bytes of data you can change the "RThreshold" property to "2" then you could perform the checksum validation in the "OnComm()" event and send the responses there. Does that make sense? [flip]

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

[cheers]
 
Ok that helps. One more question. Do you want to send the "On" and "Off" commands in the same sequence? I am assuming you are looking to send one or the other, is that correct?

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

[cheers]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top