0

I really don't know why my code is not saving for me the readings from the adc and gps receiver to the file I already open it in the first line in the code. it save only one record from both adc and gps receiver.

this is my code:

import MDM

f = open("cord+adc.txt", 'w')

def getADC():
  res = MDM.send('AT#ADC?\r', 0) 
  res = MDM.receive(100)
  if(res.find('OK') != -1):
    return res
  else:
    return ""

def AcquiredPosition():
res = MDM.send('AT$GPSACP\r', 0) 
res = MDM.receive(30)
if(res.find('OK') != -1):
  tmp = res.split("\r\n")
  res = tmp[1]
  tmp = res.split(" ")
  return tmp[1]
else:
  return ""

while (1):
  cordlist = []
  adclist = []
  p = AcquiredPosition()
  res = MDM.receive(60)
  cordlist.append(p)
  cordlist.append("\r\n")
  f.writelines(cordlist)
  q = getADC()
  res = MDM.receive(60)
  adclist.append(q)
  adclist.append("\r\n")
  f.writelines(adclist)

and this is the file called "cord+adc.txt":

174506.000,2612.7354N,05027.5971E,1.0,23.1,3,192.69,0.18,0.09,191109,07
#ADC: 0

if there is another way to write my code, please advise me or just point to me the error in the above code.

thanks for any suggestion

1
  • 2
    For starters, the code indentation is broken. Commented Nov 19, 2009 at 18:16

5 Answers 5

3

You have two problems here, you are not closing you file. There is a bigger problem in your program though your while loop will go forever (or until something else goes wrong in your program) there is no terminating condition. You are looping while 1 but never explicitly breaking out of the loop. I assume that when the function AcquiredPosition() returns an empty string you want the loop to terminate so I added the code if not p: break after the call to the function if it returns an empty string the loop will terminate the file will be closed thanks to the with statement.You should restructure your while loop like below:

with open("cord+adc.txt", 'w') as f:
    while (1):
        cordlist = []
        adclist = []
        p = AcquiredPosition()
        if not p:
            break
        res = MDM.receive(60)
        cordlist.append(p)
        cordlist.append("\r\n")
        f.writelines(cordlist)
        q = getADC()
        res = MDM.receive(60)
        adclist.append(q)
        adclist.append("\r\n")
        f.writelines(adclist)
Sign up to request clarification or add additional context in comments.

Comments

1

Because you never explicitly flush() or close() your file, there's no guarantee at all about what will wind up in it. You should probably flush() it after each packet, and you must explicitly close() it when you wish your program to exit.

2 Comments

i am appreciate your answer, but can you help me in loops and how to make it finite. because i need to flush the file or close but how to do that.
How can I help you do that? You're the only one who knows when your program should terminate.
1

If your modem connection is a socket, make sure your socket is functioning by calling getADC() and AcquiredPosition() directly from the interactive interpreter. Just drop the while(1) loop in a function (main() is the common practice), then import the module from the interactive prompt.

Your example is missing the initialization of the socket object, MDM. Make sure it is correctly set up to the appropriate address, with code like:

import socket
MDM = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
MDM.connect((HOST, PORT))

If MDM doesn't refer to a TCP socket, you can still try calling the mentioned methods interactively.

2 Comments

gimel, MDM is a module that happens to have send and receive functions. It might not have anything to do with sockets.
Thanks. Of course no sockets are implied in the question. Just a duck.
0

I don't see you closing the file anywhere. Add this as the last line of your code:

f.close()

That should contribute to fixing your problem. I don;t know much about sockets, etc, so I can't help you there.

3 Comments

hi inspectorG4dget, how can I finish the loop, it is an infinite loop. I don't know how\where to add f.close().
instead put f.flush() after the writelines call.
hi, i don't know what f.flush() will make for me. can you explain. thank you
0

When you write a line into a file, it is actualy buffered into memory first (this is the C way of handling files). When the maximum size for the buffer is hit or you close the file, the buffer is emptyed into the specified file.

From the explanation so far i think you got the scary image of file manipulation. Now, the best way to solve any and all problems is to flush the buffer's content to the file (meaning after the flush() function is executed and the buffer is empty you have all the content safely saved into your file). Of cource it wold be a great thing to close the file also, but in an infinite loop it's hardly possible (you could hardcode an event maybe, send it to the actual function and when the infinite loop stops - closing the program - close the file also; just a sugestion of cource, the flush () thing shold do the trick.

1 Comment

Well, the infinite loop in the OP is a mistake, I'd say.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.