-
Notifications
You must be signed in to change notification settings - Fork 3
/
cw.py
129 lines (106 loc) · 4.33 KB
/
cw.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
"""
Python Code for Putting LoRaMOTE into CW Mode
Copyright (c) 2016 Jason Biegel, Chris Merck
All Rights Reserved
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
import serial
import time
import sys
BAUD_RATE = 57600
class LoRaSerial(object):
def __init__(self,_serial_port,_dev_addr):
'''
configures serial connection
'''
self._ser = serial.Serial(_serial_port, BAUD_RATE)
# timeout block read
self._ser.timeout = 8
# disable software flow control
self._ser.xonxoff = False
# disable hardware (RTS/CTS) flow control
self._ser.rtscts = False
# disable hardware (DSR/DTR) flow control
self._ser.dsrdtr = False
# timeout for write
self._ser.writeTimeout = 0
print "Resetting LoRa Tranceiver..."
self.write_command('sys reset',False)
print "Configuring Tranceiver..."
self.write_command('radio set mod lora')
self.write_command('mac pause')
def read(self):
'''
reads serial input
'''
return self._ser.readline().strip()
def write(self, str):
'''
writes out string to serial connection, returns response
'''
self._ser.write(str + '\r\n')
return self.read()
def write_command(self, config_str, check_resp=True):
'''
writes out a command
'''
#print "Command: '%s'"%config_str
response = self.write(config_str)
if check_resp and response != 'ok':
print "ERROR: Unexpected response: '%s'"%response
def send_message(self, data):
'''
sends a message to backend via gateway
'''
print "Sending message: ", data
# send packet (returns 'ok' immediately)
self.write_command('mac tx uncnf 1 %s'%data)
# wait for success message
response = self.read()
if response == 'mac_tx_ok':
print "Message sent successfully!"
else:
print "ERROR: mac tx command returned unexpected response: ", response
def receive_message(self):
'''
waits for a message
'''
pass
if __name__ == "__main__":
if len(sys.argv) < 4:
print "Usage: python cw.py <port> <dev_addr> <period>"
print
exit(0)
port = sys.argv[1]
dev_addr = sys.argv[2]
period = int(sys.argv[3])
loramote = LoRaSerial(port,dev_addr)
while True:
print "Sending V..."
loramote.write('radio cw on')
print "Read: ", loramote.read()
time.sleep(0.1)
loramote.write('radio cw off')
print "Read: ", loramote.read()
time.sleep(0.1)
loramote.write('radio cw on')
print "Read: ", loramote.read()
time.sleep(0.1)
loramote.write('radio cw off')
print "Read: ", loramote.read()
time.sleep(0.1)
loramote.write('radio cw on')
print "Read: ", loramote.read()
time.sleep(0.1)
loramote.write('radio cw off')
print "Read: ", loramote.read()
time.sleep(0.1)
loramote.write('radio cw on')
print "Read: ", loramote.read()
time.sleep(2)
loramote.write('radio cw off')
print "Read: ", loramote.read()
time.sleep(0.5)