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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MSCOMM Oddity 2

Status
Not open for further replies.

jake007

Programmer
Jun 19, 2003
166
US
Using MSCOMM to read a scale on com one, her eis the code:
Private Sub Command1_Click()

Dim value As String
Dim counter As Integer
counter = 0
frmMain.MSComm1.CommPort = 1
frmMain.MSComm1.Settings = "9600,E,7,1"
frmMain.MSComm1.InputLen = 7
frmMain.MSComm1.PortOpen = True

frmMain.MSComm1.Output = "W" & Chr$(13)
MsgBox value, vbCritical ***************
buffer$ = buffer$ & frmMain.MSComm1.Input
counter = counter + 1

txtIWeight.Text = Mid(buffer$, 2, 6)
txtIWeight.Text = Val(txtIWeight.Text)

frmMain.MSComm1.PortOpen = False

End Sub

The app reads the scale just fine and places the value in the textbox, however it will only work with the line marked with asterisks above. If I take out the msgbox line , I get a null or zero value return from the scale? Any Ideas? How is a MSGBOX and a MSCOMM input connected in this way?
 
try replacing that msgbox line with a 'doevents' line



-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
The msgbox merely provides a delay for the scale to respond.

Another approach would be to just send the message to the scale and wait for the On Com event for received data.
 
gmmastros, tried that, still have the same problem??

Sheco, can you elaborate on how to implement the On Com event?

Jake
 
You could monitor the InputBuffer to see if it is greater than zero. Or if you know that it will always be a certain length, then wait until all characters are there.

Code:
  Do
     DoEvents
  Loop Until frmMain.MSComm1.InputBuffer > 0)
  
  If (frmMain.MSComm1.InputBuffer > 0) then
     buffer = buffer & frmMain.MSComm1.Input
     ' Put the rest of your code here.
     
  End if

I used an If statement because you may want to put a time limit on the Do...Loop. If the time expires, the drop through the code and come back later.

HyperEngineer
If it ain't broke, it probably needs improvement.
 
Sheco has given you the answer you need. Use the OnComm event to trap when the scale responds.

And I would strongly suggest not using DoEvents in your code. Especially when dealing with things like comm control events.
 
Thanks for the help all. Took your advice Sheco & TheVampire, looks like the issue is resolved.

Jake
 
Ok, problem NOT solved:::

Here is revised code"

Private Sub Command1_Click()

Dim value As String
Dim counter As Integer
counter = 0
'frmMain.MSComm1.CommPort = 1
'frmMain.MSComm1.Settings = "9600,E,7,1"
'frmMain.MSComm1.InputLen = 7
'frmMain.MSComm1.PortOpen = True
frmMain.MSComm1.Output = "W" & Chr$(13)
'DoEvents
'MsgBox "John", vbCritical
buffer$ = buffer$ & frmMain.MSComm1.Input
txtIWeight.Text = Mid(buffer$, 2, 6)
txtIWeight.Text = Val(txtIWeight.Text)
'frmMain.MSComm1.PortOpen = False

End Sub

Private Sub Form_Load()
With MSComm1
.CommPort = 1
.Settings = "9600,E,7,1"
.InputLen = 7
.PortOpen = True
End With
End Sub

Private Sub Form_Unload(Cancel As Integer)
MSComm1.PortOpen = False
End Sub

Private Sub MSComm1_OnComm()
Dim inbuff As String
MsgBox "Comm Event"
Select Case MSComm1.CommEvent
'errors
'events
Case comEvReceive
inbuff = MSComm1.Input
txtIWeight.Text = inbuff
End Select
End Sub

If I un-comment the msbox on command1_click, everything works (I get the correct weight) No msgbox, no weight. BUT, the msgbox in the mscomm1_onComm event NEVER fires, no matter what, as if the com event is not even happening.

Any ideas?

jake
 
Thanks gmmastros, that did it. The RThreshold is set to zero by default, from the MSCOMM Refernce you quoted,

"Setting the RThreshold property to 0 (the default) disables generating the OnComm event when characters are received."

I set this to 7, which is the number of characters received, and all works fine.

Thanks again for everyones help.

Jake
 
I also recomend pulling that buffer$= line out of the click event. You want the OnComm event to handle the input buffer. At best, it's looking at the buffer too soon and getting nothing. At worst, it emptying the buffer before the OnComm event can even see it, so it may not get processed the way you think it will.
 
I agree with jasen ... the click event should only have code to send the data... not any code to read it.
 
Right, I will remove the excess code that I had in there. I was just trying different things to resolve the issue. Thanks for all the help

Jake
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top