-
Notifications
You must be signed in to change notification settings - Fork 2
/
wsprbeacon.py
404 lines (373 loc) · 11.4 KB
/
wsprbeacon.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#! /usr/bin/python
#my first python3 script from scratch. I know it's probably simple
#and could be much neater, but it's a first effort
#script for programming the ZachTech LPT1 WSPR beacon
#written by Kevin Loughin, KB9RLW, March 2019
import time
import os.path
import serial
#defining variables I'll be using later
callsign = "null"
menuentry = ""
serialport = "null"
bands = ["D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D"]
#defining functions I'll call later.
#serial detection procedure
def setserial():
clearscreen()
print("Plug in the device, wait 1 second and press enter.")
os.sys.stdin.readline()
exists = os.path.exists('/dev/ttyUSB0')
if exists:
print("Found ttyUSB0")
time.sleep(1)
ser = serial.Serial('/dev/ttyUSB0')
s = ser.read(5)
ser.close()
if s == "{MIN}":
print("Success! Port is ttyUSB0")
serialport = "/dev/ttyUSB0"
return serialport
exists = os.path.exists('/dev/ttyUSB1')
if exists:
print("Found ttyUSB1")
time.sleep(1)
ser = serial.Serial('/dev/ttyUSB1')
s = ser.read(5)
ser.close()
if s == "{MIN}":
print("Success! Port is ttyUSB1")
serialport = "/dev/ttyUSB1"
return serialport
exists = os.path.exists('/dev/ttyUSB2')
if exists:
print("Found ttyUSB2")
time.sleep(1)
ser = serial.Serial('/dev/ttyUSB2')
s = ser.read(5)
ser.close()
if s == "{MIN}":
print("Success! Port is ttyUSB2")
serialport = "/dev/ttyUSB2"
return serialport
exists = os.path.exists('/dev/ttyUSB3')
if exists:
print("Found ttyUSB3")
ser = serial.Serial('/dev/ttyUSB3')
s = ser.read(5)
ser.close()
if s == "{MIN}":
print("Success! Port is ttyUSB3")
serialport = "/dev/ttyUSB3"
return serialport
time.sleep(2)
serialport = "null"
return serialport
#check serial security function
def checksec(serialport):
print("checking security of"), serialport
time.sleep(2)
security = os.access(serialport, os.R_OK|os.W_OK)
if not security:
clearscreen()
print("Your username does not have access to serial ports.")
print("You need to add your user to the dialout group.")
print("Copy the line below, change your_username to your username and execute it in the terminal.")
print("Then log out or restart and log back in to activate access.")
print("")
print("sudo adduser your_username dialout")
exit()
return security
#Print Menu and get choice function
def render_menu(menunum):
if menunum == "main":
clearscreen()
printmain()
menunum = input("Enter Menu item:")
return menunum
#Clear Screen procedure
def clearscreen():
count = 28
while(count > 1):
print("")
count = count - 1
#Print main menu procedure
def printmain():
print("Main Menu. Device detected on "), serialport
print("")
print("0: exit program")
print("1: Change current callsign. >"), callsign
print("2: Enable or disable bands")
print("3: Change current live mode, wspr or sig gen.")
print("4: Set startup mode")
#changecall procedure
def changecall(callsign):
clearscreen()
print("Current callsign:"), callsign
print("")
newcallsign = raw_input("Enter new callsign, use upper case:")
newcallsign = newcallsign.rstrip()
clearscreen()
choice = "Change callsign from " + callsign + " to " + newcallsign + "? y or n:"
choice = raw_input(choice)
if choice == "y":
print("Updating device with new callsign:")
# open serial and flush junk
serdata = "null"
ser = serial.Serial(serialport)
time.sleep(.5)
ser.flushInput()
ser.write("[CCM] S N\r\n")
time.sleep(.1)
while serdata != "{CCM} N\r\n":
serdata = ser.readline()
ser.write("[CCM] S N\r\n")
time.sleep(.1)
# sending new callsign to device
serout = "[DCS] S " + newcallsign + "\r\n"
while serdata[:5] != "{DCS}":
ser.write(serout)
time.sleep(.1)
ser.write("[DCS] G\r\n")
time.sleep(.1)
serdata = ser.readline()
print(serdata)
ser.write("[CSE] S\r\n")
time.sleep(.1)
ser.close()
callsign = getcallsign()
return callsign
#changebands procedure
def changebands():
#open port and flush unneeded stuff
serdata = "null"
ser = serial.Serial(serialport)
time.sleep(.5)
ser.flushInput()
ser.write("[CCM] S N\r\n")
time.sleep(.1)
while serdata != "{CCM} N\r\n":
serdata = ser.readline()
ser.write("[CCM] S N\r\n")
time.sleep(.1)
#Get current band status
serdata = "null"
while serdata[:5] != "{OBD}":
ser.write("[OBD] G\r\n")
time.sleep(.1)
serdata = ser.readline()
count = 1
while count < 14:
bands[count] = serdata[9:].rstrip()
serdata = ser.readline()
count = count + 1
ser.close()
printbands()
togglebands()
clearscreen()
#open port and flush unneeded stuff
serdata = "null"
ser = serial.Serial(serialport)
time.sleep(.5)
ser.flushInput()
ser.write("[CCM] S N\r\n")
time.sleep(.1)
while serdata != "{CCM} N\r\n":
serdata = ser.readline()
ser.write("[CCM] S N\r\n")
time.sleep(.1)
print("Updating device with new configuration.")
count = 1
while count < 14:
bcount = count - 1
if bcount < 10:
serdata = "[OBD] S " + "0" + str(bcount) + " " + str(bands[count]) + "\r\n"
else:serdata = "[OBD] S " + str(bcount) + " " + str(bands[count]) + "\r\n"
ser.write(serdata)
time.sleep(.1)
count = count + 1
ser.write("[CSE] S\r\n")
time.sleep(.5)
ser.close()
time.sleep(2)
#printbands procedure
def printbands():
clearscreen()
print("Current bands Enabled or Disabled")
print("")
print("1: 2190m--->"), bands[1]
print("2: 630m---->"), bands[2]
print("3: 160m---->"), bands[3]
print("4: 80m----->"), bands[4]
print("5: 40m----->"), bands[5]
print("6: 30m----->"), bands[6]
print("7: 20m----->"), bands[7]
print("8: 17m----->"), bands[8]
print("9: 15m----->"), bands[9]
print("10: 12m----->"), bands[10]
print("11: 10m----->"), bands[11]
print("12: 6m------>"), bands[12]
print("13: 4m------>"), bands[13]
#togglebands procedure
def togglebands():
bandnum = 1
while bandnum != 0:
printbands()
bandnum = input("Enter band number to toggle, zero when done:")
if bands[bandnum] == "D":
bands[bandnum] = "E"
else:bands[bandnum] = "D"
#changemode procedure
def changemode():
clearscreen()
print("Here we can set a current live mode.")
print("Setting the device to none.")
#open port and flush unneeded stuff
serdata = "null"
ser = serial.Serial(serialport)
time.sleep(.5)
ser.flushInput()
ser.write("[CCM] S N\r\n")
time.sleep(.1)
while serdata != "{CCM} N\r\n":
serdata = ser.readline()
ser.write("[CCM] S N\r\n")
time.sleep(.1)
print("")
print("Mode choices are:")
print("")
print("1: WSPR beacon on")
print("2: Signal generator mode")
print("3: Abort back to main menu")
print("")
mode = input("Mode choice? :")
#wspr mode choice
if mode == 1:
ser.write("[CCM] S W\r\n")
time.sleep(.1)
serdata = ser.readline()
print("WSPR mode on, press enter to stop.")
os.sys.stdin.readline()
ser.write("[CCM] N\r\n")
ser.close
#siggen mode choice
if mode == 2:
clearscreen()
print("Signal generator mode.")
frequency = input("Enter frequency in Hertz:")
rawfrequency = frequency * 100
length = len(str(rawfrequency))
padlen = 12 - length
pad = ""
while padlen > 1:
pad = pad + "0"
padlen = padlen -1
outfrequency = pad + str(rawfrequency)
print("")
print("Setting generator to "), frequency, "Hz"
#open port and flush unneeded stuff
serdata = "null"
ser = serial.Serial(serialport)
time.sleep(.5)
ser.flushInput()
ser.write("[CCM] S N\r\n")
time.sleep(.1)
while serdata != "{CCM} N\r\n":
serdata = ser.readline()
ser.write("[CCM] S N\r\n")
time.sleep(.1)
#set generator on with frequency
serdata = "null"
outstring = "[DGF] S " + str(outfrequency) + "\r\n"
ser.write(outstring)
time.sleep(.1)
ser.write("[CCM] S S\r\n")
time.sleep(.1)
print("Signal generator ON. Hit enter to abort.")
os.sys.stdin.readline()
while serdata != "{CCM} N\r\n":
serdata = ser.readline()
ser.write("[CCM] S N\r\n")
time.sleep(.1)
time.sleep(2)
#changestartup procedure
def changestartup():
#open port and flush unneeded stuff
serdata = "null"
ser = serial.Serial(serialport)
time.sleep(.5)
ser.flushInput()
ser.write("[CCM] S N\r\n")
time.sleep(.1)
while serdata != "{CCM} N\r\n":
serdata = ser.readline()
ser.write("[CCM] S N\r\n")
time.sleep(.1)
clearscreen()
print("Set the power up default mode, WSPR, or None.")
print("")
print("1: WSPR")
print("2: None")
print("")
choice = input("Choose power up mode:")
if choice == 1:
ser.write("[OSM] S W\r\n")
time.sleep(.2)
ser.write("[CSE] S\r\n")
time.sleep(.2)
else:
ser.write("[OSM] S N\r\n")
time.sleep(.2)
ser.write("[CSE] S\r\n")
ser.close
time.sleep(2)
#get current callsign function
def getcallsign():
serdata = "null"
ser = serial.Serial(serialport)
time.sleep(.5)
ser.flushInput()
ser.write("[CCM] S N\r\n")
time.sleep(.1)
while serdata != "{CCM} N\r\n":
serdata = ser.readline()
print(serdata)
ser.write("[CCM] S N\r\n")
time.sleep(.1)
while serdata.startswith("{DCS}") != True:
ser.write("[DCS] G\r\n")
time.sleep(.1)
serdata = ser.readline()
callsign = serdata[6:].rstrip()
ser.close()
return callsign
#Main Program
#get serial port
serialport = setserial()
#check users security for port
security = checksec(serialport)
#get current callsign from device
callsign = getcallsign()
#Clear screen and get first menu input
while(menuentry != 0):
menunum = "main"
menuentry = render_menu(menunum)
if menuentry == 1:
menuentry = 99
callsign = changecall(callsign)
if menuentry == 2:
menuentry = 99
print("Standby while I read the current band settings.")
changebands()
if menuentry == 3:
menuentry = 99
changemode()
if menuentry == 4:
menuentry = 99
changestartup()
if menuentry != 0 and menuentry != 99:
print("")
print("Bad entry, I don't understand "), menuentry
time.sleep(2)
print("User selected:", menuentry)
#end