-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart_serial.py
122 lines (108 loc) · 2.81 KB
/
start_serial.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
import time
import sys
import serial
from thread import start_new_thread
from random import randint
# configure the serial connections (the parameters differs on the device you are connecting to)
ser = serial.Serial(
port='/dev/ttyS0',
baudrate=115200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS
)
ser.isOpen()
ser.write("AT+ADDR=" + sys.argv[1] + "\r\n")
time.sleep(0.5)
ser.write("AT+CFG=433000000,20,6,10,1,1,0,0,0,0,3000,8,4\r\n")
input=1
currDest = ''
received = []
knownAddr = ["FFFF"]
waitingForOk = False
ok = False
def getOut():
out = ''
while not out.endswith("\r\n"):
if ser.inWaiting() == 0:
time.sleep(0.1)
else:
out += ser.read(1)
return out
def receive():
global input
while input != 'exit':
out = getOut()
print(out)
received.append(out)
def processReceived():
global ok
global input
global knownAddr
while input != "exit":
if len(received) == 0:
time.sleep(0.1)
else:
cmd = received.pop(0)
if cmd.endswith("AT,OK\r\n"):
ok = True
elif cmd.endswith("RTI\r\n"):
addr = cmd.split(",")[1]
if addr not in knownAddr:
knownAddr.append(addr)
print("Found address " + addr)
def waitForOk():
global ok
while not ok:
time.sleep(0.1)
def sendAndWaitForOk(s):
global waitingForOk
global ok
while waitingForOk:
time.sleep(0.1)
waitingForOk = True
ser.write(s)
waitForOk()
ok = False
waitingForOk = False
def sendRti():
global currDest
while 1:
if currDest != "FFFF":
sendAndWaitForOk("AT+DEST=FFFF\r\n")
currDest = "FFFF"
sendAndWaitForOk("AT+SEND=3\r\n")
ser.write("RTI\r\n")
time.sleep(randint(30, 60))
def sendMessage(addr, m):
global currDest
global knownAddr
if addr not in knownAddr:
print("Address not discovered " + addr)
return
if currDest != addr:
sendAndWaitForOk("AT+DEST=" + addr + "\r\n")
currDest = addr
print("12")
sendAndWaitForOk("AT+SEND=" + str(len(m)) + "\r\n")
print("13")
ser.write(m)
print("message sent")
start_new_thread(receive, ())
start_new_thread(processReceived, ())
start_new_thread(sendRti, ())
while 1:
input = raw_input()
if input == 'exit':
break
elif input == "printtable":
print(knownAddr)
elif input.startswith("send"):
parts = input.split(";")
if len(parts) != 3:
print("wrong format")
else:
sendMessage(parts[1], parts[2])
else:
ser.write(input + '\r\n')
ser.close()