开发者

Troubles with python list and file saving

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


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)


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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜