Skip to content

Instantly share code, notes, and snippets.

@storca
Created November 9, 2022 13:54
Show Gist options
  • Save storca/dd33670fcbcf8b09f94c1f3357069e6c to your computer and use it in GitHub Desktop.
Save storca/dd33670fcbcf8b09f94c1f3357069e6c to your computer and use it in GitHub Desktop.
Python helper to configure the DRF1278DM module
"""
(Very) simple script to read and configure the DORJI DRF1278DM module (firmware version 2.7)
The original software provided by the manufacturer did not work on my computer, so I made my own (very simple) version
Script made following the manufacturer documentation :
http://www.dorji.com/docs/data/DRF1278DM.pdf
"""
from serial import Serial
from time import sleep
read_command = b'\xaf\xaf\x00\x00\xaf\x80\x02\x0e\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x9d\x0d\x0a'
types = {
128:'Request',
0:'Response'
}
baud = {
1:1200,
2:2400,
3:4800,
4:9600,
5:19200,
6:38400,
7:57600
}
parity = {
0:'No partity check',
1:'Odd parity',
2:'Even partity'
}
rf_factor = { i:2**i for i in (7, 8, 9, 10, 11, 12) }
modes = {
0:"Standard",
1:"Central",
2:"Node"
}
rf_bw = {
6:"62.5k",
7:"125k",
8:"256k",
9:"500k"
}
rf_power = {
1:"4dBm",
2:"7dBm",
3:"10dBm",
4:"13dBm",
5:"14dBm",
6:"17dBm",
7:"20dBm"
}
breath = {
0:"2s",
1:"4s",
2:"6s",
3:"8s",
4:"10s"
}
wake_timer = { i:f"{2**(i+1)}ms" for i in range(6)}
def main():
try:
ser = Serial(port='/dev/ttyUSB0', baudrate=9600)
except Exception as e:
print(e)
read_data(ser)
def write_data(ser):
"""
Writes the following parameters to the module
Baudrate : 9600
Parity : No partity check
Frequency : 433998888.96
RF Factor : 2048
Mode : Standard
RF_BW : 125k
ID (star mode only): 0
NetID (star mode only): 0
RF Power : 13dBm
Breath : 2s
Wake timer : 2ms
"""
write_command = b'\xaf\xaf\x00\x00\xaf\x80\x01\x0e\x04\x00l\x80\x00\x0b\x00\x07\x00\x00\x00\x04\x00\x00'
# Replace the C by the actual checksum
write_command += compute_crc(write_command)
write_command += b'\r\n'
print("Writing new configuration to module...")
ser.write(write_command)
def compute_crc(data:bytes):
init = 0
for b in data[:21]:
init += b
return (init % 256).to_bytes(1, byteorder='big')
def read_data(ser):
if ser.writable():
ser.write(read_command)
else:
print("Serial not writable")
sleep(.1) # wait for data reception
received = ser.read_all()
print(received)
beautify_data(received)
def beautify_data(received):
"""
Works for DRF1278DM
"""
try:
print(f"Type : {types[received[5]]}")
print(f"Baudrate : {baud[received[8]]}")
print(f"Parity : {parity[received[9]]}")
print(f"Frequency : {int.from_bytes(received[10:13], 'big') * 61.035}")
print(f"RF Factor : {rf_factor[received[13]]}")
print(f"Mode : {modes[received[14]]}")
print(f"RF_BW : {rf_bw[received[15]]}")
print(f"ID (star mode only): {int.from_bytes(received[16:18], 'big')}")
print(f"NetID (star mode only): {received[18]}")
print(f"RF Power : {rf_power[received[19]]}")
print(f"Breath : {breath[received[20]]}")
print(f"Wake timer : {wake_timer[received[21]]}")
except Exception as err:
print(err)
print("Unknown option (missing key in dict)")
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment