Skip to content

Instantly share code, notes, and snippets.

@sleventyeleven
Created July 19, 2026 04:07
Show Gist options
  • Select an option

  • Save sleventyeleven/47f28b252b77abbb586b119b4f668924 to your computer and use it in GitHub Desktop.

Select an option

Save sleventyeleven/47f28b252b77abbb586b119b4f668924 to your computer and use it in GitHub Desktop.
Lora Testing Script via pyserial
import serial
import threading
import time
# Configure the serial port.
# Replace '/dev/ttyUSB0' with your actual port:
# Windows: 'COM3', 'COM4', etc.
# Mac/Linux: '/dev/ttyUSB0', '/dev/ttyAMA0', etc.
SERIAL_PORT = '/dev/ttyUSB4'
BAUD_RATE = 115200
try:
ser = serial.Serial(
port=SERIAL_PORT,
baudrate=BAUD_RATE,
bytesize=serial.EIGHTBITS,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
timeout=1.0
)
except Exception as e:
print(f"Error opening serial port: {e}")
exit()
def read_from_port(serial_connection):
"""Continuously listens for incoming data from the RYLR998 module."""
print("Listening for incoming LoRa messages...")
while True:
if serial_connection.in_waiting > 0:
try:
# Read the data until a newline character is found
incoming_data = serial_connection.readline().decode('utf-8').strip()
if incoming_data:
print(f"\n[Received]: {incoming_data}")
# Incoming message format is usually: +RCV=ADDRESS,LENGTH,DATA,RSSI,SNR
if "+RCV=" in incoming_data:
print("-> Parsed Data Packet Received!")
except Exception as e:
print(f"Error reading serial: {e}")
time.sleep(0.01)
# Start a background daemon thread to handle receiving data concurrently
listener_thread = threading.Thread(target=read_from_port, args=(ser,))
listener_thread.daemon = True
listener_thread.start()
def send_command(command_str):
"""Formats and sends an AT command to the module."""
full_command = f"{command_str}\r\n"
ser.write(full_command.encode('utf-8'))
print(f"[Sent]: {command_str}")
# Basic initialization sequence
time.sleep(1)
send_command("AT") # Test if the module responds with "+OK"
# Interactive Loop
try:
while True:
print("\n--- RYLR998 Control Menu ---")
print("1. Send a Test (AT)")
print("2. Send Text Message to another module")
print("3. Custom AT Command")
print("4. Exit")
choice = input("Select an option: ")
if choice == '1':
send_command("AT")
elif choice == '2':
target_addr = input("Enter target module Address (e.g., 0, 1, 2): ")
message = input("Enter your text message: ")
length = len(message)
# Format: AT+SEND=ADDRESS,LENGTH,DATA
send_command(f"AT+SEND={target_addr},{length},{message}")
elif choice == '3':
custom_cmd = input("Enter full AT command (e.g., AT+ADDRESS?): ")
send_command(custom_cmd)
elif choice == '4':
print("Exiting...")
break
else:
print("Invalid choice.")
time.sleep(2) # Give the background thread time to print the response
except KeyboardInterrupt:
print("\nProgram interrupted by user.")
finally:
ser.close()
print("Serial port closed.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment