Skip to content

Instantly share code, notes, and snippets.

@apullin apullin/mbt_tone.py
Created Jun 23, 2018

Embed
What would you like to do?
Python code to start & stop Bluetooth carrier TX for bt_mfg_test app
#!/anaconda3/bin/python
import serial
import sys
import ctypes
def tone(ser, frequency, power, onoff = True):
if (frequency < 2402) or (frequency > 2480):
print("Frequency must be between 2402-2480 Mhz")
if type(frequency) != int:
print("Frequency must be an integer")
cmd = [0x01, 0x014, 0xfc, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
if onoff:
cmd[4] = 0
else:
cmd[4] = 1
cmd[5] = frequency-2400
cmd[6] = 0
cmd[7] = 0
if onoff:
cmd[8] = 8
else:
cmd[8] = 0
cmd[9] = ctypes.c_uint8( power ).value
ser.write(cmd)
res = ser.read(7)
print(res)
def tone_all_off(ser):
cmd = [ int(x,16) for x in "01 14 fc 07 01 02 00 00 00 00 00".split(" ") ]
ser.write(cmd)
res = ser.read(7)
print(res)
def main():
if len(sys.argv) != 5:
print("Bad arguments")
print("Usage: mbt_tone <COMxx|/dev/...> <onoff> <frequency> <power>")
sys.exit(-1)
COMPORT = sys.argv[1]
onoff = int(sys.argv[2])
frequency = int(sys.argv[3])
power = int(sys.argv[4])
print("Opening serial...")
try:
ser = serial.Serial(port = "/dev/tty.usbserial-A12TT9R4", baudrate = 115200, rtscts = False, timeout=1)
except Exception:
print("Error:")
print(Exception)
sys.exit(-1)
if frequency == 0:
tone_all_off(ser)
else:
tone( ser, frequency, power, int(onoff) )
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.