Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

VB code for GSM interfacing

Status
Not open for further replies.

djc

Advanced Member level 1
Joined
Jan 27, 2013
Messages
402
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,298
Location
India
Activity points
4,554
Hello,

I am totally new for VB. Can anyone please guide me on the code for VB for serial communication and GSM interfacing.
 

Hello,

I am totally new for VB. Can anyone please guide me on the code for VB for serial communication and GSM interfacing.

The set of command called ATI commands . same as used with serial modems of fax modem


hope it helps
Zeyad
 

if you are using Visual Studio VB it has a SerialPort component in the toolbox which allows you to communicate with a COM; port
 

Thanx zeyad n horace....

However i am searching for the syntax used for writing such code. I have worked with AT commands. But not getting how to implement it in VB. I am using VB6.
 

Thanx zeyad n horace....

However i am searching for the syntax used for writing such code. I have worked with AT commands. But not getting how to implement it in VB. I am using VB6.
you can use a SerialPort object so
Code:
        SerialPort1.Write("AT+CSQ\n")

I don't know if the SerialPort object exists in VB6

however, if you do a web search for "VB serial port" it will give you plenty of links
 

I am getting error 424 object required in first line of code, that is which port to be open. If I comment that, next ine wil give error. Whats wrong in the code,

Code Visual Basic - [expand]
1
2
3
4
5
6
7
8
Private Sub Form_Load()
   MSCom1.CommPort = 4   'comm port no.--------------error-----------------
   MSCom1.Settings = "9600,n,8,1"
   MSCom1.RThreshold = 24    'no. of chr to recive
   MSCom1.InputLen = 0  '  no. of chr on which oncomm  event fires
    
   MSCom1.PortOpen = True  'open comm port
End Sub



And in this code i am getting an error, compile error sub or function not defined

Code Visual Basic - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Private Sub Form_Load() --------------------------------error------------------------
   Dim strValue As String ' define Buffer value from Modem
   MSCom1.CommPort = 4   'comm port no.
   MSCom1.Settings = "9600,n,8,1"
   MSCom1.RThreshold = 24    'no. of chr to recive
   MSCom1.InputLen = 0  '  no. of chr on which oncomm  event fires
    
   MSCom1.PortOpen = True  'open comm port
   MSComm1.Output = "ATV1Q0" & Chr&(13) 'send AT command to Modem
              Do
              DoEvents
              strValue = strValue & MSComm1.Input    ' get input data of Com Port
              Loop Until InStr(strValue, "OK" & vbCrLf)
              Text1.Text = strValue
              MSComm1.PortOpen = False 'Close port
End Sub

 
Last edited:

Thanx zeyad n horace....

However i am searching for the syntax used for writing such code. I have worked with AT commands. But not getting how to implement it in VB. I am using VB6.

View attachment mscomm.bmp

thanx for your comment
right click on the tool box and chose components
select microsoft comm 6
then ok
in toolbox an ocx will appear (phone icon)
in msdn if you install it there a project called "VBTERM" IN FOLDER "C:\Program Files\Microsoft Visual Studio\MSDN\2001OCT\1033\SAMPLES\VB98\Mscomm\"

AT COMMANDS SHOULD BE SEND AS STRING

HOPE IT HELPS
Zeyad
 

Thanx Zeyad,

I chose microsoft comm6 and put phone icon on form. I am not getting what you want to install me. I have no msdn library installed.
 

Thanx Zeyad,

I chose microsoft comm6 and put phone icon on form. I am not getting what you want to install me. I have no msdn library installed.

here the example shown in vb6(attached)
first when you attach your gsm through usb to serial or serial get the com port number from device manger
and then run project and from com port menu chose properties and set the port number from combobox then the baud rate to 9600 , no parity,8bit ,1 stop bit then ok
from comport chose open port
type your at commands

hope it helps
Zeyad
 

Attachments

  • Mscomm.rar
    13.1 KB · Views: 82

Thanx zeyad,

I have succeeded in sending and receiving message to GSM module. However can you please tell me what is the AT command to read SIM number when call is received and VB6 coding logic to get exactly only SIM number and get rid of rest of the data.

hey zeyad ,
i solved the issue, i got the commands. Now dats i am receiving is in the format
+CLIP: "+918554888728",145,"",,"",0. How to extract mobile number from this data in VB6.

I am actually getting whole command and data from modem like this
AT+CLIR=1OKAT+CLIP=1OKRING+CLIP: "+919823596784",145,"",,"",0
 
Last edited:

i solved the issue, i got the commands. Now dats i am receiving is in the format
+CLIP: "+918554888728",145,"",,"",0. How to extract mobile number from this data in VB6.

I am actually getting whole command and data from modem like this
AT+CLIR=1OKAT+CLIP=1OKRING+CLIP: "+919823596784",145,"",,"",0
you could use the string manipulation methods, e.g. InStr to look for the +
Code:
http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.strings.instr.aspx
then look for the next "

or you could parse the string for the substring you require
https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.strings.instr.aspx
 

Why i am getting echo of sent AT command. Here is the code

Code Visual Basic - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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.Settings = "9600,n,8,1"
   MSComm1.RThreshold = 24    'no. of chr to recive
   MSComm1.InputLen = 0  '  no. of chr on which oncomm  event fires
    
   MSComm1.PortOpen = True  'open comm port
   
Exit Sub
errx:
 MsgBox "error"
 
 
End Sub
 
Private Sub Command1_Click()
'MSComm1.Output = "AT" + Chr(13) + vbCrLf
'Sleep 1000
'MSComm1.Output = "AT+CMGDA=DEL ALL" + Chr(13) + vbCrLf
'Sleep 1000
'MSComm1.Output = "AT+FCLASS=8" + Chr(13)
'MSComm1.Output = "ATE0" + Chr(13)+ vbCrLf
'Sleep 200
'MSComm1.Output = "ATS0=1" + Chr(13)
'Sleep 200
'MSComm1.Output = "AT+CMGF=1" + Chr(13) '& Chr(10)
'Sleep 500
'MSComm1.Output = "AT+CMGR" + Chr(13) '& Chr(10)
'MSComm1.Output = "AT+CMGS=9860939518" + vbCrLf + Chr(13)
'Sleep 1000
'MSComm1.Output = "Test message---1" + Chr(26)
'Sleep 2000
'MSComm1.Output = "AT+CMGR" + Chr(13) '& Chr(10)
 MSComm1.Output = "AT+CLIR=1" + Chr(13)
 Sleep 1000
 MSComm1.Output = "AT+CLIP=1" + Chr(13)
 sleep 1000
Text1.Text = MSComm1.Input
 
 
End Sub




I don't know how much this string manipulation function is useful, because system on which i am going to implement it is little complicated. There are 100 phones connected and i have to get the status of each phone continuously. Now string with phone no and other data is going to appear so many times. So every time for 100 phones how would i get exact phone no i wanted.
 
Last edited:

if you are receiving the string
AT+CLIR=1OKAT+CLIP=1OKRING+CLIP: "+919823596784",145,"",,"",0

you can extract the number so
Code:
   Sub Main()
        Dim Test As String = "AT+CLIR=1OKAT+CLIP=1OKRING+CLIP: ""+919823596784"",145,"""",,"""",0"""
        Console.WriteLine("input data {0}", Test)
        Dim Pos As Integer
        Pos = InStr(Test, """+") + 1
        Console.WriteLine("Start of number  {0}", Pos)
        Dim Number As String = Mid(Test, Pos)
        Console.WriteLine("Number {0}", Number)
        Pos = InStr(Number, """") - 1
        Number = Mid(Number, 1, Pos)
        Console.WriteLine("Number {0}", Number) 
    End Sub
1 extract the substring starting with "+
2 extract the substring ending in "

when run gives
Code:
input data AT+CLIR=1OKAT+CLIP=1OKRING+CLIP: "+919823596784",145,"",,"",0"
Start of number  35
Number +919823596784",145,"",,"",0"
Number +919823596784
Press any key to continue . . .

you could do it in one statement
Code:
        Number = Mid(Test, InStr(Test, """+") + 1, InStr(InStr(Test, """+") + 1, Test, """") - 1 - InStr(Test, """+"))
        Console.WriteLine("Number {0}", Number)

I am sure the VB experts could give you a more elegant solution
 
Last edited:

Hi,

One more issue is that every time i am not getting the same string as i mentioned earlier. Moreover with Mid and Instr functions I am not getting the exact position of the phone number. Means if +91 starts at position 15 i am getting no 19. If position is 23 i am getting 35 like this. I don't understand why it is happening. I would be grateful to you if you suggest me some VB6 expert.
 

strange, if the code sometimes extracts the correct data and sometimes not

post some sample data, e.g. the full text received such as
AT+CLIR=1OKAT+CLIP=1OKRING+CLIP: "+919823596784",145,"",,"",0"

and the data you manage to extract such as 919823596784 and the code you are using
 

Thanx zeyad,

I have succeeded in sending and receiving message to GSM module. However can you please tell me what is the AT command to read SIM number when call is received and VB6 coding logic to get exactly only SIM number and get rid of rest of the data.

hey zeyad ,
i solved the issue, i got the commands. Now dats i am receiving is in the format
+CLIP: "+918554888728",145,"",,"",0. How to extract mobile number from this data in VB6.

I am actually getting whole command and data from modem like this
AT+CLIR=1OKAT+CLIP=1OKRING+CLIP: "+919823596784",145,"",,"",0
Here is a link for reference for Gsm AT commands
**broken link removed**
and
https://www.sparkfun.com/datasheets/Cellular Modules/AT_Commands_Reference_Guide_r0.pdf

Hope it helps
Zeyad
 

strange, if the code sometimes extracts the correct data and sometimes not

post some sample dataRING+CLIP: "+919823596784",145,"",,"",0"

Hi horace,
I changed my code little bit now. I am receiving data on Mscomm receive event. SO the data i am getting now is
RING+CLIP: "+919823596784",145,"",,"",0"
However this data shifts sometimes to the left by 1 or 2 bit every time ring comes. SO number is also shifted. SO necessarily i don't get whole number at a time. it may be +91, or +919823, or 784 ---data----+9198235........... all in haphazard manner. Here is the code
Code:
Dim str 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.Settings = "9600,n,8,1"
   MSComm1.RThreshold = 47    'no. of chr to recive
   MSComm1.InputLen = 0  '  no. of chr on which oncomm  event fires
    
   MSComm1.PortOpen = True  'open comm port
   Exit Sub
   errx:
 MsgBox "error"
End Sub

Private Sub MSComm1_OnComm()
Select Case MSComm1.CommEvent

'Events
Case comEvReceive
'Case comEvRing
Text1.Text = MSComm1.Input
str = Text1.Text
Text2.Text = InStr(str, "+91")
Case Else

End Select

End Sub
 

Although I have written a few programs in VB (mainly UDP and TCP client/server) I am not a VB programmer and have never used VB6 and MSComm control

however, it looks as though you may be reading a few characters at a time from MScomm1 and looking for "+91" - you may find it you may not

what you need to do is to read a complete line - you can do this by reading characters and adding them to a String until you find the end of line (\n in C) - you can then look for "+91"
Code:
Text2.Text = InStr(str, "+91")

perhaps our VB experts could advise
 

Hi horace,

Sorry for the ate reply. I was busy in another VB issues. As i told you earlier, the string i receive is
RING+CLIP: "+919823596784",145,"",,"",0", with No Carrier signal sometimes and AT + clip command sometimes. I have to check 2-3 events in my program such as whether i am receiving a call or getting a message. For that i was thinking of checking starting bits of received string such as RING+CLIP, for incoming call. But as i told, data keeps on getting shifted from left to right. Can you tell me the logic of separating the characters and then checking it.
 

it looks as though when you receive a line of text from the modem you need to examine the start of the line to see what information is contained.
e.g.
Code:
InStr(str, "RING+CLIP:")
if InStr returns 1 it starts with the text RING+CLIP: if 0 it does not
by having a sequence of tests you can determine the type of message received and take appropriate action

still not sure about the shifting - it sounds like you are loosing data - what is the size of the MSComm receive buffer - could you make it larger?
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top