-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathINTO.py
217 lines (196 loc) · 6.37 KB
/
INTO.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import threading
import serial
import time
import sys
import logging
logger = logging.getLogger("iridium")
debug = True
global ser
def sendCommand(cmd):
if cmd[-1] != '\r\n':
cmd += '\r\n'
#if debug:
#print("Sending command: {}".format(cmd))
ser.write(cmd.encode('UTF-8'))
ser.flush()
cmd_echo = ser.readline()
#if debug:
#print("Echoed: " + cmd_echo.decode('UTF-8'))
def setup(port):
global ser
ser = serial.Serial(port=port, baudrate = 19200, timeout = 15)
ser.flush()
doTheOK()
def doTheOK():
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 on_Startup():
argument = " "
command = " "
global port
if len(sys.argv) > 1:
port = sys.argv[1]
else:
port = '/dev/ttyUSB0'
setup(port)
if len(sys.argv) < 4:
# # print("not enought args")
exit(-1)
else:
if sys.argv[2] == "message":
command = sys.argv[3]
# # print("Message to send: "+command)
elif sys.argv[2] == "command":
argument = sys.argv[3]
# # print("Command to execute: "+argument)
elif sys.argv[2] == "listen":
# # print("Listening for Ring")
listenUp.start()
else:
# # print("argument 3 is not valid, say either command, message or listen")
exit(-1)
#setup(port)
#if debug:
#print("Connected to {}".format(ser.name))
#clear everything in buffer
#ser.reset_input_buffer()
#ser.reset_output_buffer()
#disable echo
#sendCommand('ATE0', has_resp=True)
if ' ' not in argument:
#print("Sending command: "+argument)
sendCommand(argument)
exit(-1)
if ' ' not in command:
#print('Sending Message: '+command)
send(command).start()
def listenUp():
sendCommand("AT+SBDMTA=1")
ser = serial.Serial(port=port, baudrate = 19200, timeout = 1)
signalStrength = 0
ringSetup = 0
iteration = 0
while ringSetup != 2 :
ring = ser.readline().decode('UTF-8')
#print(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')
#print("Response before Splitting: "+test)
resp = test.split(': ')
#print("Response after splitting: "+resp[1]+" 0 "+resp[0]+" END")
try:
resp = resp[1].split(', ')
except:
#print("index out of bounds exception \r\n closing program")
exit(-1)
bytesLeft= int(resp[0])
#print("split response: "+resp[1])
#bytesLeft = 0
sendCommand("AT+SBDRT")
#while True:
#try:
#print(ser.readline().decode('UTF-8').split(":")[1])
#print("done")
#break
#except:
#continue
ringSetup = 0
#print(ser.readline().decode('UTF-8'))
#print(ser.readline().decode('UTF-8'))
#print(ser.readline().decode('UTF-8'))
#print(ser.readline().decode('UTF-8'))
#print(ser.readline().decode('UTF-8'))
#print(ser.readline().decode('UTF-8'))
sendCommand("at+sbdmta=0")
break
#ser.flush()
#print("listening...")
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)
if __name__ == '__main__':
if len(sys.argv) < 4:
logger.debug(len(sys.argv))
logger.debug("Usage: $0 <serial port> <command or message or listenUp> <text to send or command>")
exit(-1)
try:
#print(len(sys.argv))
on_Startup()
finally:
#sendCommand('ATE1', has_resp=False)
pass