update
This commit is contained in:
63
SARA/sara.py
63
SARA/sara.py
@@ -49,51 +49,62 @@ config = load_config(config_file)
|
|||||||
# Access the shared variables
|
# Access the shared variables
|
||||||
baudrate = config.get('SaraR4_baudrate', 115200)
|
baudrate = config.get('SaraR4_baudrate', 115200)
|
||||||
|
|
||||||
ser = serial.Serial(
|
|
||||||
|
try:
|
||||||
|
|
||||||
|
ser = serial.Serial(
|
||||||
port=port, #USB0 or ttyS0
|
port=port, #USB0 or ttyS0
|
||||||
baudrate=baudrate, #115200 ou 9600
|
baudrate=baudrate, #115200 ou 9600
|
||||||
parity=serial.PARITY_NONE, #PARITY_NONE, PARITY_EVEN or PARITY_ODD
|
parity=serial.PARITY_NONE, #PARITY_NONE, PARITY_EVEN or PARITY_ODD
|
||||||
stopbits=serial.STOPBITS_ONE,
|
stopbits=serial.STOPBITS_ONE,
|
||||||
bytesize=serial.EIGHTBITS,
|
bytesize=serial.EIGHTBITS,
|
||||||
timeout = timeout
|
timeout = timeout
|
||||||
)
|
)
|
||||||
|
|
||||||
ser.write((command + '\r').encode('utf-8'))
|
ser.write((command + '\r').encode('utf-8'))
|
||||||
|
|
||||||
#ser.write(b'ATI\r') #General Information
|
#ser.write(b'ATI\r') #General Information
|
||||||
#ser.write(b'AT+CCID?\r') #SIM card number
|
#ser.write(b'AT+CCID?\r') #SIM card number
|
||||||
#ser.write(b'AT+CPIN?\r') #Check the status of the SIM card
|
#ser.write(b'AT+CPIN?\r') #Check the status of the SIM card
|
||||||
#ser.write(b'AT+CIND?\r') #Indication state (last number is SIM detection: 0 no SIM detection, 1 SIM detected, 2 not available)
|
#ser.write(b'AT+CIND?\r') #Indication state (last number is SIM detection: 0 no SIM detection, 1 SIM detected, 2 not available)
|
||||||
#ser.write(b'AT+UGPIOR=?\r') #Reads the current value of the specified GPIO pin
|
#ser.write(b'AT+UGPIOR=?\r') #Reads the current value of the specified GPIO pin
|
||||||
#ser.write(b'AT+UGPIOC?\r') #GPIO select configuration
|
#ser.write(b'AT+UGPIOC?\r') #GPIO select configuration
|
||||||
#ser.write(b'AT+COPS=?\r') #Check the network and cellular technology the modem is currently using
|
#ser.write(b'AT+COPS=?\r') #Check the network and cellular technology the modem is currently using
|
||||||
#ser.write(b'AT+COPS=1,2,20801') #connext to orange
|
#ser.write(b'AT+COPS=1,2,20801') #connext to orange
|
||||||
#ser.write(b'AT+CFUN=?\r') #Selects/read the level of functionality
|
#ser.write(b'AT+CFUN=?\r') #Selects/read the level of functionality
|
||||||
#ser.write(b'AT+URAT=?\r') #Radio Access Technology
|
#ser.write(b'AT+URAT=?\r') #Radio Access Technology
|
||||||
#ser.write(b'AT+USIMSTAT?')
|
#ser.write(b'AT+USIMSTAT?')
|
||||||
#ser.write(b'AT+IPR=115200') #Check/Define baud rate
|
#ser.write(b'AT+IPR=115200') #Check/Define baud rate
|
||||||
#ser.write(b'AT+CMUX=?')
|
#ser.write(b'AT+CMUX=?')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
try:
|
|
||||||
# Read lines until a timeout occurs
|
# Read lines until a timeout occurs
|
||||||
response_lines = []
|
response_lines = []
|
||||||
while True:
|
start_time = time.time()
|
||||||
line = ser.readline().decode('utf-8').strip()
|
|
||||||
if not line:
|
while (time.time() - start_time) < timeout:
|
||||||
break # Break the loop if an empty line is encountered
|
line = ser.readline().decode('utf-8', errors='ignore').strip()
|
||||||
|
if line:
|
||||||
response_lines.append(line)
|
response_lines.append(line)
|
||||||
|
|
||||||
|
# Check if we received any data
|
||||||
|
if not response_lines:
|
||||||
|
print(f"ERROR: No response received from {port} after sending command: {command}")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
# Print the response
|
# Print the response
|
||||||
for line in response_lines:
|
for line in response_lines:
|
||||||
print(line)
|
print(line)
|
||||||
|
|
||||||
except serial.SerialException as e:
|
except serial.SerialException as e:
|
||||||
print(f"Error: {e}")
|
print(f"ERROR: Serial communication error: {e}")
|
||||||
|
sys.exit(1)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"ERROR: Unexpected error: {e}")
|
||||||
|
sys.exit(1)
|
||||||
finally:
|
finally:
|
||||||
if ser.is_open:
|
# Close the serial port if it's open
|
||||||
|
if 'ser' in locals() and ser.is_open:
|
||||||
ser.close()
|
ser.close()
|
||||||
#print("Serial closed")
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user