2

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 Answers 4

3

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?).

5
  • Thank you guru, however i want RThreshold to be 1 only. I dont want to change its value for call and message. I am totally new to VB and I have this giant task to complete. SO I don't know anything how each function of VB6 behaves exactly. Continuous reading and experimentation is going on. Jun 6, 2014 at 6:46
  • I tried your code, it's working fine. However every time it skips some characters or spaces in the received string. What cud be the issue. Jun 6, 2014 at 7:58
  • 1
    @user3678480: If you're new to VB, I strongly urge you to switch to a modern and supported programming language like VB.NET or C#. If you're just starting off, VB6 is definitely not the way to go. Jun 6, 2014 at 12:46
  • 1
    Actually vast parts of .Net are now just as "legacy" as VB6. So that sort of comment is of even less value (if possible) than it was 5 years ago. The advice isn't bad or wrong, but the reason given is extemely weak.
    – Bob77
    Jun 6, 2014 at 16:00
  • Whatever platform I choose, it doesn't matter, because i have to do anything on my own. No expertise is available with me. So i have to stick to VB6 and go on.Can you guys suggest me that how to parse the message so that i can extract the phone number from incoming string and keep on deleting previous string so that whenever message or call comes text box should not display previous data. Jun 7, 2014 at 6:48
1

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

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.

-1

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)
3
  • 2
    This doesn't answer the question, rather, it gives a definition of a number of the common properties.
    – Deanna
    Nov 13, 2015 at 15:00
  • MSComm1.InputLen = 0 ' no. of chr on which oncomm event fires Change this to MSComm1.InputLen = 1 and then check
    – nkpandit
    Nov 16, 2015 at 7:12
  • They'd already tried that, as started in their question, but couldn't get it to work right. This answer all doesn't provide anything extra.
    – Deanna
    Nov 16, 2015 at 7:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.