Trying to use the AT command with an Huawei E3531

依然范特西╮ 提交于 2019-12-03 17:23:05

so basically, my problem was that I could not see my modem as a serial. I explained how to do that, you have to force your modem with this command:

sudo usb_modeswitch -v 12d1 -p 1F01 -M '55534243123456780000000000000011062000000100000000000000000000'

Then I tried Minicom, which is really nice, you just have to connect well using the right parameters like baud rate and port. So look for it and do some test. To see what you are writing in minicom, just enable local echo (ctrl+a then z to the option). So you can send all the information you want.

But I wanted to integrate it in a python script so here is my code:

def terminalComAndRead(a,b) :
    found = False
    proc = subprocess.Popen([a], stdout=subprocess.PIPE, shell=True)#send the command
    (out, err) = proc.communicate()
    string=str(out)
    #print(string)
    foundstring=string.find(b, 0, len(string))#look for the string b in the serial answer of my device
    if foundstring != -1 :
        found= True #send back true if it found it
    return found

def send(data,ser):
    try:
        ser.write(data)#write the command my serail port
    except Exception as e:
        print "Couldn't send data to serial port: %s" % str(e)
    else:
        try:
            sleep(1)
            data = ser.read(1)#read the serial
        except Exception as e:
            print "Couldn't read data from serial port: %s" % str(e)
        else:
            if data:  # If data = None, timeout occurr
                n = ser.inWaiting()
                if n > 0: data += ser.read(n)
                return data

def searchModem():

    foundmob= False
    foundusb= False

    sudoPassword='Figaro5558_'#my sudo password
    command="usb_modeswitch -v 12d1 -p 1f01 -M '55534243123456780000000000000011062000000100000000000000000000'" # the command that for ce the modem to be on serial mode
    while foundusb!= True : # this allows me to look for the modem as an usb and then force it

        foundmob=terminalComAndRead("lsusb",'12d1:1f01')#doing a ls usb and then looking for the id of my device
        changemob=terminalComAndRead("lsusb",'12d1:1001')#this is the id of my device when it is already in serial mode

        if foundmob == True :
            sleep(1)
            os.system('echo %s|sudo -S %s' % (sudoPassword, command))# this is the line who execute the command on sudo

            foundusb=terminalComAndRead("ls /dev/ttyUSB*",'ttyUSB1')


        if changemob == True :
            foundusb=terminalComAndRead("ls /dev/ttyUSB*",'ttyUSB1')

        if foundusb == True :
                print('Modem ready for connection')

    ttyusb0=terminalComAndRead('dmesg | tail -50','now attached to ttyUSB0') #look int he dmesg if there is my new usb port

    if ttyusb0 == True :
        usbPort='/dev/ttyUSB0'#give the path 

    return usbPort

ser = serial.Serial(usbPort, 9600, bytesize=8, parity='N', stopbits=1, timeout=1, rtscts=False, dsrdtr=False) #connect to your serial

cmd="AT^HCSQ?\r\n"#the AT command
msg=str(send(cmd,ser))#use the send function to send the AT command
print(msg)

So this worked for me, I hope it will help you! Cheers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!