I am getting an incoming call but no audio. Is this compatible.
import serial
import time
class GSMModem:
def init(self, port=‘COM19’, baudrate=9600, timeout=1):
self.ser = serial.Serial(port, baudrate, timeout=timeout)
if self.ser.isOpen():
print(f"Connected to {port}“)
else:
print(f"Failed to connect to {port}”)
def send_command(self, command, wait_for_response=True):
self.ser.write((command + "\r").encode())
time.sleep(0.5)
if wait_for_response:
response = self.ser.read(self.ser.inWaiting()).decode().strip()
print(f"Command: {command}\nResponse: {response}") # Print the command and response for debugging
return response
return None
def check_modem(self):
response = self.send_command('AT')
if "OK" in response:
print("Modem is active and responding.")
else:
print("Modem not responding.")
def configure_audio(self):
print("Configuring audio settings...")
# Set Audio Mode
response = self.send_command('AT+QAUDMOD=0')
if "ERROR" in response:
print("Error: Failed to set audio mode.")
else:
print("Audio mode set successfully.")
# Set Uplink (MIC) Gain
response = self.send_command('AT+QMIC=0,15')
if "ERROR" in response:
print("Error: Failed to set microphone gain.")
else:
print("Microphone gain set successfully.")
# Enable Echo Cancellation
response = self.send_command('AT+QEEC=1,1')
if "ERROR" in response:
print("Error: Failed to enable echo cancellation.")
else:
print("Echo cancellation enabled successfully.")
def monitor_incoming(self):
self.send_command('AT+CMGF=1') # Set SMS to text mode
self.send_command('AT+CNMI=2,1,0,0,0') # Configure to receive new message indications
print("Monitoring incoming SMS and calls... Press Ctrl+C to stop.")
try:
while True:
if self.ser.inWaiting():
response = self.ser.read(self.ser.inWaiting()).decode().strip()
if "+CMTI:" in response:
print("\nNew SMS received:")
self.print_latest_sms()
elif "RING" in response:
print("\nIncoming call detected.")
self.answer_call()
time.sleep(1)
except KeyboardInterrupt:
print("Stopped monitoring.")
def print_latest_sms(self):
self.send_command('AT+CMGF=1') # Set SMS to text mode
response = self.send_command('AT+CMGL="REC UNREAD"')
if response and "+CMGL:" in response:
messages = response.split('+CMGL:')
for message in messages[1:]:
parts = message.split(',')
if len(parts) >= 5:
sender = parts[2].replace('"', '').strip()
message_body = message.split('\r\n', 1)[1].strip()
print(f"Sender: {sender}")
print(f"Message: {message_body}\n")
else:
print("No unread messages found.")
def answer_call(self):
print("Answering call...")
# Configure audio before answering
self.configure_audio()
# Add a short delay before answering
time.sleep(1) # 1-second delay
# Answer the call
response = self.send_command('ATA')
if "OK" in response:
print("Call answered. You can now speak using the modem's microphone.")
try:
while True:
if self.ser.inWaiting():
response = self.ser.read(self.ser.inWaiting()).decode().strip()
if "NO CARRIER" in response:
print("Call ended.")
break
time.sleep(1)
except KeyboardInterrupt:
print("Call interrupted.")
else:
print(f"Failed to answer the call. Response: {response}")
def hang_up_call(self):
response = self.send_command('ATH')
if "OK" in response:
print("Call ended.")
else:
print(f"Failed to hang up the call. Response: {response}")
def close(self):
if self.ser.isOpen():
self.ser.close()
print("Serial connection closed.")
Example usage:
if name == “main”:
modem = GSMModem(port=‘COM19’)
modem.check_modem()
# Monitor incoming SMS messages and calls, answering calls automatically
modem.monitor_incoming()
# Close the modem connection when done
modem.close()