tags:

views:

89

answers:

4

Hi guys, 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  A: 

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.

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

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.

Jonathan Feinberg
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.
mahdi86
How can I help you do that? You're the only one who knows when your program should terminate.
Jonathan Feinberg
A: 

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.

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

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)
Tendayi Mawushe