add excel reader
This commit is contained in:
@@ -1,23 +1,48 @@
|
||||
import logging
|
||||
|
||||
import serial
|
||||
from gsmmodem import GsmModem
|
||||
from gsmmodem.modem import SentSms
|
||||
|
||||
from utils.logging import init_logger
|
||||
|
||||
PORT = "/dev/tty.usbmodem11101"
|
||||
BAUDRATE = 115200
|
||||
ser = serial.Serial(PORT, BAUDRATE, timeout=1)
|
||||
|
||||
|
||||
def has_sim() -> bool:
|
||||
# ser = serial.Serial(PORT, BAUDRATE, timeout=1)
|
||||
|
||||
|
||||
def get_devices_ports() -> list:
|
||||
return ["/dev/tty.usbmodem1121101",
|
||||
"/dev/tty.usbmodem1121103",
|
||||
"/dev/tty.usbmodem1121105",
|
||||
"/dev/tty.usbmodem1121107",
|
||||
"/dev/tty.usbmodem1121201",
|
||||
"/dev/tty.usbmodem1121203",
|
||||
"/dev/tty.usbmodem1121205",
|
||||
"/dev/tty.usbmodem1121207",
|
||||
"/dev/tty.usbmodem1121301",
|
||||
# "/dev/tty.usbmodem1121303",
|
||||
"/dev/tty.usbmodem1121305",
|
||||
"/dev/tty.usbmodem1121307",
|
||||
"/dev/tty.usbmodem1121401",
|
||||
"/dev/tty.usbmodem1121403",
|
||||
"/dev/tty.usbmodem1121405",
|
||||
"/dev/tty.usbmodem1121407"]
|
||||
|
||||
|
||||
def has_sim(ser) -> bool:
|
||||
# check pin
|
||||
cmd_check_pin = "AT+cpin?\r"
|
||||
msg = send_command(cmd_check_pin)
|
||||
msg = send_command(cmd_check_pin, ser)
|
||||
if b'OK' in msg:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
def send_command(cmd: str) -> bytes:
|
||||
def send_command(cmd: str, ser) -> bytes:
|
||||
print("send command {}".format(cmd))
|
||||
ser.write(cmd.encode())
|
||||
msg = ser.read(100)
|
||||
@@ -25,6 +50,16 @@ def send_command(cmd: str) -> bytes:
|
||||
return msg
|
||||
|
||||
|
||||
def get_phone_number():
|
||||
cmd = "AT+CNUM\r"
|
||||
send_command(cmd)
|
||||
|
||||
|
||||
def get_sim_ccid():
|
||||
cmd = "AT+CCID\r"
|
||||
send_command(cmd)
|
||||
|
||||
|
||||
def send_sms(sms_destination: str, msg: str):
|
||||
print('Initializing modem...')
|
||||
modem = GsmModem(PORT, BAUDRATE)
|
||||
@@ -41,11 +76,51 @@ def send_sms(sms_destination: str, msg: str):
|
||||
modem.close()
|
||||
|
||||
|
||||
def create_modem_for_port(port: str) -> GsmModem:
|
||||
print('Initializing modem... for ' + port)
|
||||
# Uncomment the following line to see what the modem is doing:
|
||||
init_logger()
|
||||
modem = GsmModem(port, BAUDRATE)
|
||||
modem.connect('0000')
|
||||
number = modem.ownNumber
|
||||
print("The SIM card phone number is:")
|
||||
print(number)
|
||||
cmd = "AT+CCID\r"
|
||||
ccid = modem.write(cmd, True)
|
||||
print("The SIM card ccid is:" + " ".join(ccid))
|
||||
return modem
|
||||
|
||||
|
||||
def start_to_handle_sms(modem: GsmModem):
|
||||
modem.smsReceivedCallback = handle_sms
|
||||
# modem = GsmModem(PORT, BAUDRATE, smsReceivedCallbackFunc=handle_sms)
|
||||
modem.smsTextMode = False
|
||||
# modem.connect('0000')
|
||||
print('Waiting for SMS message...')
|
||||
try:
|
||||
modem.rxThread.join(
|
||||
2 ** 31) # Specify a (huge) timeout so that it essentially blocks indefinitely, but still receives CTRL+C interrupt signal
|
||||
finally:
|
||||
modem.close()
|
||||
|
||||
|
||||
def handle_sms(sms):
|
||||
print(u'== SMS message received ==\nFrom: {0}\nTime: {1}\nMessage:\n{2}\n'.format(sms.number, sms.time, sms.text))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
# enable verbose logs
|
||||
cmd = "AT+CMEE=2\r"
|
||||
send_command(cmd)
|
||||
if has_sim():
|
||||
send_sms("+33649614591","Modem pool test")
|
||||
else:
|
||||
print("no cart sim")
|
||||
# enable verbose logs for all port
|
||||
# create modems for all the available port
|
||||
modem_list = []
|
||||
for port in get_devices_ports():
|
||||
modem_list.append(create_modem_for_port(port))
|
||||
# cmd = "AT+CMEE=2\r"
|
||||
# send_command(cmd)
|
||||
|
||||
# if has_sim():
|
||||
# # send_sms("+33649614591", "Modem pool test")
|
||||
# modem = show_own_phone_number()
|
||||
# get_sim_ccid()
|
||||
# # start_to_handle_sms(modem)
|
||||
# else:
|
||||
# print("no cart sim")
|
||||
|
||||
Reference in New Issue
Block a user