Home > Net >  Failing to find a string within a string
Failing to find a string within a string

Time:08-13

I've been trying to write some code for programming a mini WiFi module, and I've got everything to work except for this bit of code I've reproduced as an example here.

The only part not working is the part I thought would be the simplest - checking the readout of the module to see if it contains the command that was sent.

Any insights would be greatly appreciated!

(Please excuse the formatting, I've inherited this code and only started learning Python last week because of it.)

def sendCmd(command, check="AOK", checkNum=3, lookCmd="yes"):
    retryCount = 1
    cmdReceived = "no"
    while retryCount <= 5:
        checkLine = 1

        if lookCmd != "yes":
            foundCmd = "yes"
        else:
            foundCmd = "no"
        print('Command: ' command)
        print('Check: ' check)
        print('foundCmd: ' foundCmd)
        print('sending: '   str(command))
        
        while checkLine <= checkNum:
            currentLine = str(testLine)
            print('reply: ' currentLine)
            if command in currentLine:
                foundCmd = "yes"
                print('##FOUND IT##')
            if check in currentLine and foundCmd == "yes":
                print('# Command received #\n')
                cmdReceived = "yes"
                return currentLine
                break
            checkLine  = 1
        if cmdReceived == "yes":
            break
        if retryCount < 5:
            # Attempt to reenter command mode if lost
            print('\n# Retry ' str(retryCount) ' #')
            checkNum  = 1
            retryCount  = 1
        elif retryCount == 5:
            print('\n*********************************************')
            print('## COMMAND FAILED: please check connection and restart ##')
            print('*********************************************')
            quit()

testLine = b'Name=TEST01010_0x56ce_0xd05\r\n'

sendCmd('TEST01010_0x56ce_0xd05\r')

CodePudding user response:

Perhaps you need a find() method for strings.

The keyword in is used, for example, for arrays:

arrayForExample = ["y", "n", "yes", "no"]
print("foo" in arrayForExample) # False
print("y" in arrayForExample) # True
  • Related