我正在研究这个ussd项目,它只是检查调制解调器上的余额并将其打印出来。但是,我坚持到了这一点,我得到了#34;好的"打印到我的屏幕上。我的ussd命令只返回" OK"不是实际的ussd回应。
我在做pyserial。所以假设我发送* 124#来检查余额,我希望余额但最终会返回"好的"
出了什么问题。
import glob
import time
import serial
from pdu import pdu_to_text, text_to_pdu
def list_ports():
ports = glob.glob('/dev/ttyUSB*')
for val, port in enumerate(ports):
print(val, port)
print('-----------------------')
port = int(input('your desired port: '))
print('You selected' + ports[port])
return ports[port]
def command_maker():
command = raw_input('enter ATCommand: ')
return bytes(command.encode('utf-8'))
def ussd_maker():
# return '1' is specified here to allow return of a casted string
ussd_code = convert_text2pdu(raw_input('Input the USSD code: '))
ussd_command = "at+cusd=1, " + str(ussd_code) + ', 15\r\n'
print(ussd_command)
return bytes(ussd_command.encode('utf8'))
def convert_pdu2text(pdu_encoded_data):
"""
Convert pdu encoded data to text(string *utf-8*)
"""
pdu_decoded_to_text = pdu_to_text(pdu_encoded_data)
return pdu_decoded_to_text
def convert_text2pdu(unicode_string):
"""
Convert text to pdu format to work with gsm modems and any other
device that support pdu data format
"""
text_encoded_to_pdu = text_to_pdu(unicode_string)
return text_encoded_to_pdu
def app_run(port, atcmd):
ser = serial.Serial()
ser.port = (port)
try:
ser.open()
time.sleep(0.5)
ser.write(bytes(atcmd) + b'\r\n')
out = b''
time.sleep(3)
while ser.inWaiting() > 0:
out += ser.read(1)
if out != '':
print("------response--------\n" + out.decode("utf-8"))
ser.close()
except Exception as e:
raise e
def app():
select_option = raw_input('Select your desired option. \n1. Send raw AT command \n2. Send USSD code: \n')
print(select_option)
ls_ports = list_ports()
if select_option == '1':
cmd_maker = command_maker()
app_run(ls_ports, cmd_maker)
elif select_option == '2':
ussd = ussd_maker()
app_run(ls_ports, ussd)
else:
app()
if __name__ == "__main__":
app()