-
Notifications
You must be signed in to change notification settings - Fork 0
/
new_iridiumNotTest.py
151 lines (131 loc) · 4.35 KB
/
new_iridiumNotTest.py
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import collections
import logging
import sys
import threading
import time
import serial
messageQueue = collections.deque([])
logger = logging.getLogger("iridium")
transmitting=0
debug = True
def setup(port):
global ser
ser = serial.Serial(port=port, baudrate = 19200, timeout = 15)
ser.flush()
sendCommand("AT")
ser.readline().decode('UTF-8') # get the empty line
resp = ser.readline().decode('UTF-8')
# # print (resp)
if 'OK' not in resp:
# # print("Echo"+resp)
exit(-1)
# show signal quality
sendCommand('AT+CSQ')
ser.readline().decode('UTF-8') # get the empty line
resp = ser.readline().decode('UTF-8')
ser.readline().decode('UTF-8') # get the empty line
ok = ser.readline().decode('UTF-8') # get the 'OK'
# # # print("resp: {}".format(repr(resp)))
if 'OK' not in ok:
# # print('Unexpected "OK" response: ' + ok)
exit(-1)
sendCommand("AT+SBDMTA=0")
#if debug:
# # print("Signal quality 0-5: " + resp)
ser.write("AT+SBDREG? \r\n".encode('UTF-8'))
while True:
try:
regStat = int(ser.readline().decode('UTF-8').split(":")[1])
break
except:
continue
break
if regStat != 2:
sendCommand("AT+SBDREG")
def sendCommand(cmd):
logger.warning("sendCommand starting")
if cmd[-1] != '\r\n':
cmd += '\r\n'
logger.warning("Sending command: {}".format(cmd))
ser.write(cmd.encode('UTF-8'))
ser.flush()
def send(thingToSend):
# try to send until it sends
startTime = time.time()
alert = 2
while alert == 2:
#signal = ser.readline().decode('UTF-8')#empty line
#signal = ser.readline().decode('UTF-8')#empty line
sendCommand("AT+CSQF")
signal = ser.readline().decode('UTF-8')#empty line
signal = ser.readline().decode('UTF-8')
# # print("last known signal strength: "+signal)
# prepare message
sendCommand("AT+SBDWT=" + thingToSend)
ok = ser.readline().decode('UTF-8') # get the 'OK'
ser.readline().decode('UTF-8') # get the empty line
# send message
sendCommand("AT+SBDI")
resp = ser.readline().decode('UTF-8') # get the empty line
resp = resp.replace(",", " ").split(" ")
startTime = time.time()
currTime = startTime
#signal = ser.readline().decode('UTF-8')#empty line
#signal = ser.readline().decode('UTF-8')#empty line
while len(resp) > 0 and len(resp) <= 2:
# # print(resp)
resp = ser.readline().decode('UTF-8')
resp = resp.replace(",", " ").split(" ")
curTime = time.time()
if (curTime-startTime)>30:
# # print("time out moving on")
break
# get the rsp
# if debug:
# # #print("resp: {}"t )
try:
# # print("*******************" + str(resp))
alert = int(resp[1])
# # print(alert)
except:
# # print("********************exception thrown")
continue
#if debug:
# # #print("alert: {}".format(alert))
exit(-1)
def listen():
if len(sys.argv) < 1:
logger.warning("No args")
exit(255)
logger.warning("listen starting")
sendCommand("AT+SBDMTA=1")
signalStrength = 0
ringSetup = 0
iteration = 0
while ringSetup != 2:
ring = ser.readline().decode('UTF-8')
logger.warning(ring)
if "SBDRING" in ring:
bytesLeft = 1
ser.timeout = 120
while bytesLeft != 0:
sendCommand("AT+SBDIXA")
resp = "A"
while len(resp) < 2:
test = ser.readline().decode('UTF-8')
logger.warning("Response before Splitting: "+test)
resp = test.split(': ')
logger.warning("Response after splitting: "+resp[1]+" "+resp[0]+" END")
try:
resp = resp[1].split(', ')
except:
logger.error("index out of bounds exception \r\n closing program")
exit(-1)
bytesLeft = int(resp[0])
sendCommand("AT+SBDRT")
sendCommand("at+sbdmta=0")
break
global port
port=sys.argv[1]
setup(port)
send(sys.argv[2])