-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoraUtility.py
70 lines (58 loc) · 1.78 KB
/
LoraUtility.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
#!/usr/bin/env python
##
# @package LoraUtility Package contains the miscelaneous functions for the LORA Module
#
from LoraConnection import LoraConnection
from ModbusConsts import ModbusConsts
import time
##
# Function to change the LORA communication channel
# @param channel int: new channel no to be set for the LORA communication
# @return result boolean: Returns True if the channel was changed successfully False otherwise
def setChannel( channel ):
checkConfigCmd = "C1C1C1"
ser = LoraConnection().getLoraConnection( 9600, ModbusConsts.PARITY_NONE, ModbusConsts.SB_ONE, ModbusConsts.BYTE_EIGHT )
if ser == None:
return False
ser.flushInput()
ser.write( bytearray.fromhex( checkConfigCmd ) )
time.sleep(0.008)
ser.flushOutput()
timeout = 5000
size = 0
data = None
while( timeout > 0 ):
size = ser.inWaiting()
if size == 6:
break
else:
timeout = timeout - 10
time.sleep( 0.01 )
print(size)
if size == 6:
data = ser.read( size )
print(''.join('{:02x}'.format(x) for x in data))
currentChannel = int( data[4] )
ser.flushInput()
channelChangeCmd = "C000001A" + "%02X"%channel + "44"
ser.write( bytearray.fromhex(channelChangeCmd) )
time.sleep(0.008)
ser.flushOutput()
timeout = 5000
size = 0
data = None
while( timeout > 0 ):
size = ser.inWaiting()
if size == 6:
break
else:
timeout = timeout - 10
time.sleep( 0.01 )
print(size)
if size == 6:
data = ser.read( size )
print(''.join('{:02x}'.format(x) for x in data))
currentChannel = int( data[4] )
if currentChannel == channel:
return True
return False