Dark Bit Factory & Gravity
PROGRAMMING => Freebasic => Topic started by: Hezad on December 16, 2008
-
Heya ! I post it here since it concerns freebasic coding but has nothing to do with demo making ! (I thought it could interest some people though, therefore I'm posting it).
I described the thing on the FB forums so I permit myself to copy the original post here !
'hope you'll enjoy it :D
'' Start of copy
Heya :)
Long time I didn't post here ! I'm currently working on an application which interacts with a cellphone.
For now, it gathers some informations (even the IMSI number) and shows in real-time the signal reception quality.
Are planned :
- a dialer (start/stop a dial on the phone from a PC command)
- a Sms sender (Once I'll get how the Nokia AT commands about SMS works)
- More informations to gather.
How it works ?
The Nokia (my cellphone) is connected to the PC as a modem through USB. It's easy to see which COM port is associated with the modem (in windows : Connexions > Nokia Modem / properties)
As any modem, it can be controlled with AT commands. But the more interesting here is that the Nokia Cellphones AT command set is available everywhere on the net. And in those commands, there are some really interesting ones (Dial of course, but also a lot of commands to gather informations about the phone, the SIM card, ...)
A Command Set :
http://www.activexperts.com/xmstoolkit/atcommands/nokia/ (http://www.activexperts.com/xmstoolkit/atcommands/nokia/)
(very Ugly) Code :
'' Code by Hezad
'' WebSite : Hezad.no-ip.org
''
''
''
''
Function SignalQuality(ByVal inval as string) as integer '' return number between 0 and 20
if right(inval,1)="," then inval = "0" & Left(Inval,1)
if inval = "99" then return -1
return Val(inval)
end function
Dim shared as integer ComPort, RecPow
Dim shared as string inval,OperatorCode,IMSICode,IMEICOde,ManuInfo,ModelInfo,SMSCAdress
Dim shared as single t
Input "COM Port >",ComPort
Open Com "COM" & ComPort & ":115200" as #1
Print
Print "Gathering CellPhone Informations ..."
Print
'' Operator Code
Print "Operator Code ..."
Do
Print #1,"AT+COPS?" & chr(13)
t=timer
Do
if left(inVal,6)="+COPS:" then OperatorCode = mid(inVal,13,5)
Line Input #1, inVal
Loop until (inval="" and OperatorCode <> "") or multikey(&h01) or timer-t>3
Loop until OperatorCode<>"" or multikey(&h01)
inval = ""
'' IMSI
Print "IMSI number ..."
Do
Print #1,"AT+CIMI" & chr(13)
t=timer
Do
if left(inVal,2)<>"AT" and inval<>"" and left(inval,2)<>"OK" then IMSICode = inval
Line Input #1, inVal
Loop until (inval="" and IMSICode <> "") or multikey(&h01) or timer-t>3
Loop until IMSICode <> "" or multikey(&h01)
inval = ""
'' IMEI
Print "IMEI number ..."
Do
Print #1,"AT+CGSN" & chr(13)
t=timer
Do
if left(inVal,2)<>"AT" and inval<>"" and left(inval,2)<>"OK" then IMEICode = inval
Line Input #1, inVal
Loop until (inval="" and IMEICode<>"") or multikey(&h01) or timer-t>3
Loop until IMEICode<>"" or multikey(&h01)
inval = ""
'' Manufacturer
Print "Manufacturer Info ..."
Do
Print #1,"AT+CGMI" & chr(13)
t=timer
Do
if left(inVal,2)<>"AT" and inval<>"" and left(inval,2)<>"OK" then ManuInfo = inval
Line Input #1, inVal
Loop until (inval="" and ManuInfo<>"") or multikey(&h01) or timer-t>3
Loop until ManuInfo<>"" or multikey(&h01)
inval = ""
'' Model
Print "Model Info ..."
Do
Print #1,"AT+CGMM" & chr(13)
t=timer
Do
if left(inVal,2)<>"AT" and inval<>"" and left(inval,2)<>"OK" then ModelInfo = inval
Line Input #1, inVal
Loop until (inval="" and ModelInfo<>"") or multikey(&h01) or timer-t>3
Loop until ModelInfo<>"" or Multikey(&h01)
inval = ""
'' SMSC Adress
Print "SMSC Adress ..."
Do
Print #1,"AT+CSCA?" & chr(13)
t=timer
Do
if left(inVal,6)="+CSCA:" then SMSCAdress = mid(inVal,9,12)
Line Input #1, inVal
Loop until (inval="" and SMSCAdress<>"") or multikey(&h01) or timer-t>3
Loop until SMSCAdress<>"" or multikey(&h01)
inval = ""
Screenres 640,480,16,2
WindowTitle "CellPhone Kommander"
'' Main program and realtime gathering
Do : cls
'' GUI coding
''terminal
Line(20,20)-(250,200),rgb(150,150,200),b
Line(21,21)-(249,199),rgb(80,80,100),bf
Line(20,15)-(105,25),rgb(80,80,100),bf
Draw String(30,18),"Terminal",rgb(150,150,200)
Draw String(35,40), "Manufacturer : " & ManuInfo
Draw String(35,50), "Model : " & ModelInfo
Draw String(35,60), "IMEI : " & IMEICode
'' Operator/Identity
Line(40+220,20)-(40+450,200),rgb(150,150,200),b
Line(40+221,21)-(40+449,199),rgb(80,80,100),bf
Line(40+220,15)-(420,25),rgb(80,80,100),bf
Draw String(40+230,18),"Operator/Identity",rgb(150,150,200)
Draw String(40+235,40), "Operator Code : " & OperatorCode
Draw String(40+235,50), "IMSI : " & IMSICode
Draw String(40+235,60), "SMSC Adress : " & SMSCAdress
'' Signal Quality GUI
Line(510,20)-(620,40),rgb(150,150,200),b
Line(511,21)-(619,39),rgb(80,80,100),bf
Line(510,15)-(570,25),rgb(80,80,100),bf
Dim as integer RecLen
if RecPow<>-1 and RecPow<>0 then
RecLen = 110*(RecPow/20)
Line(513,30)-(617,37),rgb(25,180,25),bf
Line(513,30)-(513+RecLen-6,37),rgb(50,250,50),bf
else
Draw String(512,28),"INIT or ERROR",rgb(255,70,70)
End if
Draw String(515,18),"Signal",rgb(150,150,200)
'' ==== Signal Quality calculating ====
Print #1,"AT+CSQ" & chr(13)
t=timer
Do
if left(inVal,5)="+CSQ:" then RecPow = SignalQuality(mid(inVal,7,2))
Line Input #1, inVal
Loop until (inval="" and timer-t>1) or multikey(&h01)
'' =====================================
sleep 1,1
Loop until multikey(&h01)
Close #1
(very Ugly) Screen :
(http://freefile.kristopherw.us/uploads/hezad/cellphone_kommander.jpg)
As you can see, I have a Nokia 5200 :)
'' End of Copy :D
( If I really want to continue this as a project, I may totally change the GUI and use IUP to make some "real" GUI :D )
-
Neat idea, I might have a go with that!
You could significantly simplify your code if you made a function which took the AT code as a parameter and returned the value as a string - at the moment you have the same code copied over and over, which is a pain if you decide to change anything :)
Jim
-
I am certain that you could make some money from this Hezad.
I've looked into enabling this website to send notifications to members by SMS too.
Using an outbound SMS gateway it would cost me about £10 for every 250 SMS messages I sent from this website with my scripts.
A desktop application that I could plug into an excell database of phone numbers and use the free messages I get from my mobile provider each month would be something I'd be interested in... Any thoughts to take it that way?
-
You could significantly simplify your code if you made a function which took the AT code as a parameter and returned the value as a string - at the moment you have the same code copied over and over, which is a pain if you decide to change anything Smiley
Sure ! This code was more a proof of concept than the beginning of a real project. I'll work on such a function and a real GUI then :D
I am certain that you could make some money from this Hezad.
Maybe with some ads on a website ^^ I guess it'll be Open Source :)
A desktop application that I could plug into an excell database of phone numbers and use the free messages I get from my mobile provider each month would be something I'd be interested in... Any thoughts to take it that way?
Hum.. For the SMS sending part, it's doable since the AT command set handles it :) (I didn't understand how it works yet though, but it's a matter of time I guess)
For the excel bridge I don't know .. I don't even wonder if it wouldn't be easier with mysql or xml than with Excel (but I have no experience in none of these so I don't really know)
Thanks for the idea :) I'll think about it !
And thanks for the kind words mates :)
-
Excel is something that seems to be a standard for this type of desktop application, most aps can import a list of phone numbers from an excel spreadsheet.
Mysql is interesting, because it would be cool if you could make your app talk to an sql database, I am not sure how to get freebasic to communicate with an sql database, I only know how to use mysql at the sql prompt and using php so I couldn't give you any pointers there.
I believe that you definately have a market for this though.
-
I'll check stuff about database and/or datasheets handling, it could be nice for sure :D
Anyway, I coded the functions to handle sending/receiving data from the phone (And in fact these are functions to communicate with any modem too) :
SendAT("AT+COPS?")
OperatorCode = ReceiveAT
-
You rule sir :)
-
The simplest way I know of accessing databases from BASIC is to use ODBC.
You need an ODBC driver specific to the database you're using (eg. mysql, or sqlserver).
You then create a link between the odbc driver and the database in Windows control panel.
In your code, you need a 'connection string' which will let you call connect.
Then you can run sql queries directly, each SELECT call creates a FieldSet, with a VARIANT result for each field.
If you need more details I can post them.
Jim
-
Well, for now I must work on different things before getting into database handling (GUI, Call/SMS/Status handling).
I'll read some stuff about that ODBC driver too, and see how I can use it. I'll permit myself to ask for details here if there's something I don't understand then :)
Thanks.
-
Just a little news, the program can now know the phone dial status (Ready, unavailable, Ringing, Call in progress).
I thought it was nice ^^
Now, before looking for new stuff to add, I'll start to work on the GUI.
-
That really is superb! Keep going with that! I would use it! :)