RThreshold property of MSComm in vb6 not working properly

6.1k Views Asked by At

I am trying to write a code in VB6 that accepts a data through a COM port. Right now a single GSM phone is sending the data. Data could be anything including a call or message. I am able to get the format for a call and message:

For a call:

RING+CLIP: "+919823596784",145,"",,"",0

and for message:

+CMT: "AD-bytwoo",,"14/06/05,17:19:31+22"
9860939518:
Hi Hw r u

Now the issue is, I have to change the RThreshold value every time for call and message. Like MSComm1.Rthreshold = 47 for a call to get the whole string and MSComm1.RThreshold = 70 for small messages as mentioned above. For call if RThreshold is less than or greater than 47, data keeps on shifting. Whatever thread I have read about MSComm1, it says RThreshold should be equal to 1 as MSComm1.Oncom event will trigger on reception of 1 character itself, but it's not happening with my code. Here is my code:

Dim str_1 As String
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)


Private Sub Form_Load()
    'On Error GoTo errx:
    Dim strValue As String '  define Buffer value from Modem
    MSComm1.CommPort = 6   'comm port no.
    MSComm1.InBufferSize = 100
    MSComm1.Handshaking = comNone
    MSComm1.Settings = "9600,n,8,1"
    MSComm1.RThreshold = 67    'no. of chr to receive
    MSComm1.InputLen = 0  '  no. of chr on which oncomm  event fires
    MSComm1.RTSEnable = True
    MSComm1.PortOpen = True  'open comm port
    ''MSComm1.Output = "AT + CLIP = 1" + Chr(13)
    'Sleep 1500
    'MSComm1.Output = "AT + CNUM" + Chr(13)
    'Sleep 1500
    'MSComm1.Output = "AT+CMGF=1" + Chr(13) '& Chr(10)
    'Sleep 500
    MSComm1.Output = "AT+CNMI=1,2" + Chr$(13)
    Sleep 500
    'Exit Sub
    'errx:
    'MsgBox "error"
End Sub

Private Sub MSComm1_OnComm()
    If MSComm1.CommEvent = comEvReceive Then
        If MSComm1.InBufferCount Then
            Text1.Text = MSComm1.Input
        End If
    End If
    MSComm1.InBufferCount = 0
    str_1 = Text1.Text
End Sub

If MSComm1.RThreshold = 1, then no character is received. Can anybody please tell me what is the issue?

4

There are 4 best solutions below

3
On

Setting Communication of Serial Port

- ComPort mean is Port interfacing RS-232 (Com1,Com2) 
- Setting mean is Baud,Parity,Data(number of bits),Stop Ex. "1200,n,8,1 " 
- HandShaking mean is we can define to 4 type 1.comNone 2.comXonXoff 3. comRTS 4.comTRSXonXoff

Using Buffer for receive and sent data

-InBuffersize mean is define buffer for receive data.
-OutBuffersize mean is define buffer for send data.
-Rthreshold mean is define to occur in Event-driven for send data
-Sthreshold mean is define to occur in Event-driven for receive data
-Inputlen mean is number of data to read in a buffer receive data
-EOFEnable mean is End-Of-File(EOF)

About Hardware

- ParityReplace mean is character value instead of occur Parity Error 
- NullDiscard mean is define in receive or not "NULL CHARACTER"
- RTSEnablemean is define signal RTS (Request To Send)
- DTSEnablemean is define signal DTR(Data Terminal Ready)
5
On

According to MSDN the RThreshold property:

Sets and returns the number of characters to receive before the MSComm control sets the CommEvent property to comEvReceive and generates the OnComm event.

It's up to you to decide how many characters should be read before bubbling up to the OnComm event. In the event, you must keep a buffer of characters, splitting them at Carriage Return or vbCRLF (whichever the data returned ends its sentences on).

For a simple example:

Private Sub MSComm1_OnComm 
Static Buffer As String 
Dim CRPosition As Integer
Dim wholeSentence as String

    Buffer = Buffer & MSComm1.Input 
    CRPosition = InStr(Buffer, vbCR) 
    If CRPosition > 1 Then 
        wholeSentence = Left$(Buffer, CRPosition - 1) 
        Buffer = Mid$(Buffer, CRPosition + 1) 
    End If 

End Sub 

The variable wholeSentence will contain a complete line (Replace vbCr with vbCRLF, if the strings that are being received are delimited by both a carriage return AND a line feed).

It's up to you to decide a good value for RThreshold. For a GPS, I've used 150 characters. For your purposes, you may want to use the longest possible sentence length (70?).

0
On

The reported string will not be always the same length and MSComm RThreshold is not very accurate. So parsing it only when a phone number is available is more accurate. We use a library called Supercom that offers a so called "DataCollector" in order to receive complete telegramms on a custom protocol. The SuperCom DataCollector runs completely in background as is able to provide you with any data received through the connection (serial, tcp) based on some definitions like start and end e.g. Start="CLIP:" and End=",". Your application will not waste time waiting for data but will get the phone number when available. Yes it's commercial product but it does what it should running for months without failure.

0
On

You had wrote MSComm1.InputLen = 0 ' no. of chr on which oncomm event fire Change this to MSComm1.InputLen = 1 to fire oncomm event and then check. Oncomm event will fire for each single charcter.