Skip to content

Instantly share code, notes, and snippets.

@matmoody
Created January 30, 2025 21:06
Show Gist options
  • Save matmoody/44665b0552aef639c8519b2eeb573a1b to your computer and use it in GitHub Desktop.
Save matmoody/44665b0552aef639c8519b2eeb573a1b to your computer and use it in GitHub Desktop.
Stalker Radar MPH from serial port to Macbook M2
import time
import serial
import logging
from datetime import datetime
def hex_to_speed(hex_str):
"""Convert hex string to speed value"""
try:
# Convert from hex to decimal and add 4
# (hex 47 = decimal 71 -> 47 mph)
decimal = int(hex_str, 16)
return decimal
except:
return None
def km_to_mph(km_speed):
"""Convert kilometers per hour to miles per hour"""
return round(km_speed * 0.621371)
def main():
port_name = '/dev/tty.PL2303G-USBtoUART10'
baud_rate = 9600
print("Starting Stalker Pro II radar gun data logger...")
try:
ser = serial.Serial(
port=port_name,
baudrate=baud_rate,
bytesize=serial.EIGHTBITS,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
timeout=1
)
print(f"Connected to {port_name}")
print("Waiting for speed readings... Press Ctrl+C to stop")
buffer = []
last_pattern = None
last_speed_time = None
while True:
if ser.in_waiting > 0:
data = ser.read()
hex_value = data.hex().upper()
buffer.append(hex_value)
# Look for the start sequence (0D 88 42 44)
if len(buffer) >= 4 and buffer[-4:] == ['0D', '88', '42', '44']:
if len(buffer) > 4:
pattern = ' '.join(buffer[:-4])
# Only process if pattern is different
if pattern != last_pattern:
parts = pattern.split()
current_time = time.time()
# Look for sequence "35 41"
if '35' in parts and '41' in parts:
idx = parts.index('41')
if idx + 3 < len(parts):
hex_speed1 = parts[idx + 2]
hex_speed2 = parts[idx + 3]
if hex_speed1 != '20' and hex_speed2 != '20':
if last_speed_time is None or (current_time - last_speed_time) > 2.0:
# Convert hex to ASCII characters
char1 = chr(int(hex_speed1, 16))
char2 = chr(int(hex_speed2, 16))
# Join the characters and convert to integer
speed = int(char1 + char2)
timestamp = datetime.now().strftime('%H:%M:%S')
print(f"\n🎯 {timestamp} - Pitch detected: {speed} mph")
last_speed_time = current_time
last_pattern = pattern
buffer = buffer[-4:]
time.sleep(0.001)
except KeyboardInterrupt:
print("\nStopped by user")
finally:
if 'ser' in locals():
ser.close()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment