1

I have the following code

import serial
import time
ser = serial.Serial('COM10', 9600, timeout=0)
timeout = time.time() + 60*1

def readresponse():
   while 1:
     try:
        print(ser.readline())
        time.sleep(1)
     except ser.SerialTimeoutException:
        print('Data could not be read')
     if (time.time() > timeout):
        break
     time.sleep(1)

def sendHttpPost():
  ser.write('AT+HTTPACTION=1\r'.encode())
def initialAtCommands():
  ser.write('AT+SAPBR=3,1,\"Contype\",\"GPRS\"\r'.encode())
  ser.write('AT+SAPBR=3,1,\"APN\",\"APN\"\r'.encode())
  ser.write('AT+SAPBR=1,1\r'.encode())
  ser.write('AT+HTTPINIT\r'.encode())
  ser.write('AT+HTTPPARA=\"CID\",1\r'.encode())
  ser.write('AT+HTTPPARA=\"URL\"URL\"\r'.encode())

 if(ser.isOpen() == False):
    print("Serialport is currently not connected")   
 else:
    print("Serialport successfully connected")
    initialAtCommands()

 while 1:
    sendHttpPost()
    readresponse()

The idea is, that sendhttppost is executed and then readresponse for 60 seconds and again sendhttppost. But the code doesn't go into readresponse again.

Any ideas why?

1
  • Does your if in readresponse() in except? Commented Jul 14, 2018 at 16:31

2 Answers 2

2

Because your readResponse() function breaks if and only if it encounters a serialTimeoutException. Note that you've put the break statement under the if block which in turn is inside the except block. This means as long as your program doesn't encounter that exception the while loop goes on infinitely. If you want the function to return immediately after it executes the try block inside readResponse(), then just put a return statement right after print(ser.readline()) and put the time.sleep() method at the end of the readResponse() function. Don't nest it inside the if block.

P.S. The time.sleep(1) here:

if (time.time() > timeout):
        break
        time.sleep(1)

is dead code. I think you need to rethink what you wanted to do with the if block.

Sign up to request clarification or add additional context in comments.

1 Comment

There were some copy / paste errors in the code. Now it's like it is in my real code.
1

I've found the solution myself

import serial
import time
ser = serial.Serial('COM10', 9600, timeout=0)
timeout = 60
timeout_start = time.time()

def readresponse():

while time.time() < timeout_start + timeout:
    try:
        print(ser.readline())
        time.sleep(1)
    except ser.SerialTimeoutException:
        print('Data could not be read')
    time.sleep(1)


def sendHttpPost():
    ser.write('AT+HTTPACTION=1\r'.encode())

def initialAtCommands():
    ser.write('AT+SAPBR=3,1,\"Contype\",\"GPRS\"\r'.encode())
    ser.write('AT+SAPBR=3,1,\"APN\",\"APN\"\r'.encode())
    ser.write('AT+SAPBR=1,1\r'.encode())
    ser.write('AT+HTTPINIT\r'.encode())
    ser.write('AT+HTTPPARA=\"CID\",1\r'.encode())
    ser.write('AT+HTTPPARA=\"URL\",\"URL\"\r'.encode())

if(ser.isOpen() == False):
    print("Serialport is currently not connected")
else:
    print("Serialport successfully connected")
    initialAtCommands()

while 1:
    sendHttpPost()
    time.sleep(1)
    readresponse()
    timeout_start = time.time()
    time.sleep(1)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.