forked from rapidsms/pygsm
-
Notifications
You must be signed in to change notification settings - Fork 3
/
pygsm_demo
executable file
·75 lines (61 loc) · 2.26 KB
/
pygsm_demo
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python
# vim: ai ts=4 sts=4 et sw=4
"""
This demo utility can be launched, for example, as:
pygsm_demo /dev/ttyUSB0 baudrate=115200 rtscts=1
/dev/ttyUSB0 = serial port where modem is attached
on windows, this could look like 'COM2'
baudate=115200 rtscts=1 = terminal settings
"""
import os
import sys
import time
if __name__ == "__main__":
if len(sys.argv) >= 2:
# put 'lib' in working path to grab pygsm from same dir
# for launch out of development directory
libdir = os.path.join(os.path.dirname(sys.argv[0]),'lib')
sys.path.insert(0,libdir)
from pygsm.gsmmodem import GsmModem
# the first argument is SERIAL PORT
# (required, since we have no autodetect yet)
port = sys.argv[1]
# all subsequent options are parsed as key=value
# pairs, to be passed on to GsmModem.__init__ as
# kwargs, to configure the serial connection
conf = dict([
arg.split("=", 1)
for arg in sys.argv[2:]
if arg.find("=") > -1
])
# dump the connection settings
print "pyGSM Demo App"
print " Port: %s" % (port)
print " Config: %r" % (conf)
print
# connect to the modem (this might hang
# if the connection settings are wrong)
print "Connecting to GSM Modem..."
modem = GsmModem(port=port, **conf)
# print "Checking airtime balance..."
# balance = modem.run_ussd("#123#")
# if balance is not None:
# print balance
print "Waiting for incoming messages..."
# check for new messages every two
# seconds for the rest of forever
while True:
msg = modem.next_message()
# we got a message! respond with
# something useless, as an example
if msg is not None:
print "Got Message: %r" % msg
msg.respond("Received: %d characters '%s'" %
(len(msg.text),msg.text))
# no messages? wait a couple
# of seconds and try again
else: time.sleep(2)
# the serial port must be provided
# we're not auto-detecting, yet
else:
print "Usage: python -m pygsm.gsmmodem PORT [OPTIONS]"