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

How to know # of bytes receive using mscomm control

Status
Not open for further replies.

JeffSabat

Programmer
Sep 15, 2003
32
PH
first of all i would like to say this site is really a help.
my problem is how will i know how bytes the control actually recieve. my program sends bytes to a terminal as commands and then the terminals sends bytes as response. so far, my program works because initially i know how many bytes i will receive but is there a way to know how many bytes exactly the control receive. I attached my code and any help,tips and suggestions would greatly be appreciated. Thank you very much.

Private Sub MSCOMM1_OnComm()
Select Case MSCOMM1.CommEvent
Case comEvReceive
myBuffer = MSCOMM1.Input
status = False
End Select
End Sub

Private Sub MyCommand_Click()
Dim InString As Variant
Dim dummy

Dim MyOption As Single
' Use COM1.
If MSCOMM1.PortOpen Then
MSCOMM1.PortOpen = False
End If
MSCOMM1.CommPort = 1
MSCOMM1.Settings = "38400,N,8,1"
' Open the port.
MSCOMM1.PortOpen = True
MSCOMM1.InBufferCount = 0
MSCOMM1.RThreshold = 14 ' i expect 14 bytes to receive
status = True
MSCOMM1.InputMode = comInputModeBinary



mystring(1) = &H1
mystring(2) = &H9
mystring(3) = &H0
mystring(4) = &H10
mystring(5) = &H82
mystring(6) = &H20
mystring(7) = &H6
mystring(8) = &HBC
mystring(9) = &H43

MSCOMM1.Output = mystring


Do
DoEvents
Loop While status

'code from cubeE101 thank you very much josh
Dim intc1 As Single
Dim strwork As String
Text1 = ""
If IsArray(myBuffer) Then
For intc1 = LBound(myBuffer) To UBound(myBuffer)
strwork = Right("0" & Hex$(myBuffer(intc1)), 2)
Text1 = Text1 & " " & strwork
Next
End If

End Sub
 
Put the received data into one string like
Dim myString as String (at head of form)

Include this in the on com routine
MyString=MyString & NewData
Then Len(MyString)( gives you the bytes)
Then dont forget to make MyString="" after you read it then either with the first bit of reception or the last character sometimes it takes a number of on com firings to get a complete packet
 
tnx tedsmith.
but i'm a bit confuse. How will my loop ends in the
do
doevents
loop while status

in my program, this loop will terminate when the comEvReceive event has been trigered.

Case comEvReceive
myBuffer = MSCOMM1.Input
status = False
End Select

I have set my RThreshold property to 14 because i expect to receive this number of bytes if my command send to the terminal was successful however, when it is not, i expect to receive 9 bytes only. This is the time that my program will hang because the comEvReceive will not be triggered at all.

MSCOMM1.RThreshold = 14 ' i expect 14 bytes to receive

Please enlighten me on this. Thank you very much.
 
Set the RThreshhold and Inputlen to 1 so the oncom event fires every character.
Buffer2 gets built up with each firing of the sub until the end of the message is reached.
You have to have some unique character transmitted to signfy the end (usually vbcr (13) or vblf (10) or both. I usually use 13.
Automatically get it if you say:
MScomm1.Output = TxData & Chr(13)

This is from a prog I wrote:

Dim Buffer2 as string,Flag as Integer,ReceivedData as String
_______________________________________
Private Sub MScomm1_OnComm()
'received signal back from LEDs
Flag = MSComm1.CommEvent
If Flag = comEvReceive Then 'data received OK
Do While MSComm1.InBufferCount
'clears any characters waiting in buffer
Buffer2 = Buffer2 & StrConv(MSComm1.Input, vbUnicode)
Loop
If Right(Buffer,1)=Chr(13) Then
'end of message
ReceivedData=Buffer2
Buffer2="" 'clear for next message
End if
End If
End Sub
 
tnx tedsmith for the reply. However, i have already solved the problem last monday, though it wasn't the best solution, yet it works and i've got no more time to re-code it since it was really needed ASAP for a product demo. Probably when i've got time, i'll try ur suggested solution yet right now, i'm busy with another project. If it interest you though how i got my solution, here's how i did it. I notice that there are 8 bytes received when the comEvReceive event is fired (probably because of the mscomm control setting) so what i did, since i know that the second byte contains the length of the packet i should be receiving from the terminal, my loop will only terminates when i have already received this number of bytes. I have some of the impt. codes below. I know it can still be improve and i have an idea in my mind but i have just no time to experiment for now. anyway, thank you very much for the help. :)

Dim sample(1 To 15) As Variant

Private Sub Command1_Click()
MSCOMM1.CommPort = 1
' 38400 baud, no parity, 8 data, and 1 stop bit.
MSCOMM1.Settings = "38400,N,8,1"

If MSCOMM1.PortOpen = False Then
MSCOMM1.PortOpen = True
End If

MSCOMM1.InBufferCount = 0

MSCOMM1.RThreshold = 1

MSCOMM1.InputMode = comInputModeBinary

j = 0
N_expect = 0
N_received = 0
Status_found = False
status = True

'mystring is a byte array that contains the packet to send
MSCOMM1.Output = mystring
Do
DoEvents
Loop While status

'process here
End Sub

Private Sub MSCOMM1_OnComm()

Select Case MSCOMM1.CommEvent
Case comEvReceive
j = j + 1
sample(j) = MSCOMM1.Input
If IsArray(sample(j)) Then
If Status_found = False Then
'1st 8 bytes received
If UBound(sample(j)) > 0 Then
'set n_expect = second byte of the packet
N_expect = sample(j)(1)
Status_found = True
End If
Else
End If
N_received =N_received +UBound(sample(j))+1
End If

If N_expect = N_received Then
status = False
End If
End Select
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top