# Mscomm (incomplete transmission)



## bradle95 (Jul 27, 2003)

I am trying to use the mscomm function with vb6 and am receiving incomplete data. Just as a quick test, I programmed a microcontroller to send a111, then a112, then a113.... A lot of the time I will get the entire string, but often only part of the string. Why is this? It only transmits every 0.25 seconds with this test, so I don't see the problem.


----------



## Aftab (Oct 15, 2003)

Paste your OnComm() event code in here and we shall have a look. Also write what the values of the following properties are:

InputLen
InBufferSize
RThreshold

Finally, are you working with just the one mscomm object or do you have a variable (within a class module for example) to which you assign the comm object.


----------



## bradle95 (Jul 27, 2003)

Being brand new with VB, I just put in very simple code. I am not sure how to use the input buffer (which I'm sure that I will probably want to do later on when I speed up the transmission rate, so if you can explain how to use it I would appreciate it), but with transmissions this few and far between, I'm not sure why I would even neet it.

Private Sub Form_Load()

With MSComm1
.CommPort = 13
.InputLen = 0
.RThreshold = 1
.SThreshold = 1
End With
MSComm1.PortOpen = True

End Sub

Private Sub MSComm1_OnComm()
Text1.Text = MSComm1.Input
End Sub


----------



## bradle95 (Jul 27, 2003)

One other thing for my end result transmissions, they will be in the form of byte identifier, byte one, byte two. Three bytes total if that will affect how the input buffer should be configured.


----------



## Aftab (Oct 15, 2003)

Are you sure your communication settings are the same for both ends i.e 9600,n,8,1. Try connecting to your device with Hyperterminal and see if you get the entire string.

As a test try the following code. As you are sending a 4 byte string see if all 4 bytes are arriving.

Private Sub MSComm1_OnComm()

Static Buffer as string 

Buffer = Buffer & MSComm1.Input

if Len(Buffer) = 4 then
text1 = Buffer
Buffer = ""
endif

End Sub


----------



## bradle95 (Jul 27, 2003)

I checked to see that all the data was being transmitted with WinWedge, it was. I will try the code you listed above to see if that helps.


----------



## bradle95 (Jul 27, 2003)

The code that you sent me works as long as there are 4 characters in the transmission. (If there are three or less, or 5 or more, an oncomm event is not fired. It will not always be the case that 4 characters will be transmitted, however, there will always be exacty 3 bytes in each transmission, is there a way to configure the mscomm to register this?


----------



## Aftab (Oct 15, 2003)

Is there an terminating character that you can check for to see if it's the end of the string. If there is, the following code should do it.

Change the MSComm1.InputLen property to 1 (read 1 character at a time)

Private Sub MSComm1_OnComm()

Static Buffer as string 
Dim strChar as string 

strChar = MSComm1.input
Buffer = Buffer & strChar

if strChar = chr(13) then
text1 = Buffer
Buffer = ""
endif

End Sub

I have used chr(13) in my example as the terminating character, you would have to replace that with the right character.

If there is no end-of-transmission character then try the following.

Change the MSComm1.InputLen property to 1 (read 1 character at a time)

Private Sub MSComm1_OnComm()

Static Buffer as string 
Dim strChar as string 

With MSComm1

If .CommEvent = comEvReceive Then
Do
strChar = .Input
Buffer = Buffer & strChar
DoEvents
Loop Until (.InBufferCount = 0)
Text1 = Buffer
Buffer = ""
End If

End With

End Sub

I apologize if there are any errors in my code but I can't test it. Remember to set the MSComm1.InputLen property value to 1 (either in Design Mode or in the form_load event) regardless of which of the above two methods you go for. This tells the control to read in 1 character at a time from the receive buffer to the .input property.


----------



## bradle95 (Jul 27, 2003)

The first example that you gave me worked fine for the first transmission, but only gives me a blank text box for everyone after.

I went back into my firware and changed the programming so that I would transmit 6 characters every time no matter how large a number (up to 16 bit), so it sends 001234, rather than 1234. Using the tip you gave about len(buffer), it seems to be work. Thanks for the help.


----------

