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!

MSCOMM Error - Oncomm does not refire after partial retrieval

Status
Not open for further replies.

atunplatino

Technical User
Mar 23, 2005
7
US
I am using VB6's MSCOMM (mode binary) to interface to the serial port.
The specific question is; Has anybody else run into a problem where OnComm does not fire when there is data waiting in the receive buffer?

Here is my broader application:
When attempting a simple loopback test of sending out 100 bytes of data, I consistently receive back a truncated number of bytes (98). WHen I step through the program, I find that I have read only the first 98 bytes and the final 2 bytes remain in the buffer. But OnComm does not fire. Also of interest is I get the same number of bytes truncated (2) regardless of the number I send out.

Any Help greatly appreciated.
Thanks.
-atunplatino
 
There is an RThreshold property on the MSComm control. According to help...

Setting RThreshold to 1, for example, causes the MSComm control to generate the OnComm event every time a single character is placed in the receive buffer.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Thanks for the help George.

My RThreshold is indeed set to 1.

This is the problem: MsComm fires one time and retrieves all data but 2 bytes. Then if I do a watch on the inbuffer, it still contains 2 bytes of data but MsComm does not fire again despite what MSDN claims.

PS: also contrary to MSDN, OnComm does not fire everytime a single character is placed in the buffer: it fires once and retrieves the first (n-2) bytes that I send where (n) is the total number of bytes sent.
 
Do you have some code you can post? I have a null modem cable and multiple serial ports. I would be willing to take a look at it for you. I usually work in "text" mode, not "binary" (as you state in your original post).

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Ok; in text mode I do not experience any problems looping back long paragraphs without errors. But here is some of my binary mode code anyway:

BTW this code does not include the basic mscomm settings like baud, open port, etc...


First the send button
Private Sub test1_Click()
Dim mytest(0) As Byte
Dim j, i As Integer
Dim vartest As Variant
i = 0
mytest(0) = 0
For i = 1 To 5
For j = 1 To 5 'attempts to send a total of 25 bytes,
'one by one through the loop
mytest(0) = j 'single byte byte array
vartest = mytest() 'byte array assigned to variant
MSComm1.Output = vartest 'variant placed in outbuffer
'DoEvents
Next j
Next i
End Sub


Next my OnComm Code

Private Sub MSComm1_OnComm()
Dim invariant As Variant

Dim n As Integer

n = MSComm1.InBufferCount - 1

'assign the incoming data to a variant variable
invariant = MSComm1.Input

MsgBox (Inbuffercount minus 1 is " & n)

'dimension a byte array
Dim inbyte() As Byte


'assign the variant data to the byte array
inbyte = invariant

'Display data in textbox 'text2' ("RECIEVING")
Dim i As Integer
For i = 0 To n
Text2.Text = Text2.Text & inbyte(i)
Next i

End Sub
 
I was playing around a little. And I noticed that my data was getting truncated also. For me, this seems to happen when I send lots of data.

Changing the InBufferSize improved this a little. Also, speeding up the transmission rate improved the problem too.

For example, when I send 10,000 bytes through the cable, my machine would truncate to 4662 bytes when I had the baud rate set to 9600. After I increased the baud rate to 38400, the problem went away.

Hope this helps.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
you don't really need to mess around with variants. Here's the code I was playing with. Hope it helps.

Code:
Private Sub Command1_Click()
    
    Dim arData(9999) As Byte
    Dim i As Long
    
    For i = 0 To 9999
        arData(i) = CInt(Rnd() * 255)
    Next
    
    comSnd.Output = arData
        
End Sub

Private Sub comRcv_OnComm()
    
    Dim arData() As Byte
        
    arData = comRcv.Input
    lblCount.Caption = Val(lblCount.Caption) + UBound(arData) + 1
    
End Sub

Private Sub Form_Load()
        
    With comSnd
        .SThreshold = 1
        .Settings = "38400,n,8,1"
        .CommPort = 5
        .InputMode = comInputModeBinary
        .PortOpen = True
    End With
    
    With comRcv
        .RThreshold = 1
        .Settings = "38400,n,8,1"
        .CommPort = 6
        .InputMode = comInputModeBinary
        .InputLen = 0
        ' make sure this port is open
        .PortOpen = True
    End With
    
End Sub



-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Ok I have a working fix to the problem which is this: at the end of my OnComm routine, I simply check mscomm1.InBufferCount and if it has anything in it, I manually Call MsComm1_Oncomm again. Thus I do receive all the data but not neatly in a convenient byte array; in a textbox that I must then read from. It works; but is non- ideal.

I think the truncation you are experiencing is unrelated b/c the problem I have will truncate 2 bytes even if I'm only sending 9. My HT P4 should be able to handle 9 lousy bytes!!

Good point about not needing Variants; I have already eliminated my abuse of them.


Thank you again; let me know if anything else comes to mind.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top