LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   python serial read and write in in format (https://www.linuxquestions.org/questions/programming-9/python-serial-read-and-write-in-in-format-4175453589/)

ajit.nayak87 03-11-2013 03:23 AM

python serial read and write in in format
 
Dear all,

I am using python 2.5 and pyserial 2.5
i wanna help in writing simple code in python where i wanna separate string

i am getting string in this format
---------
time and date:11/3/2013 11:10:5
dir:fwd
actual position : 30
desire position :28
---------

i wanna write this in to excel sheet for every 10 min such away that in column c1:time and date c2: dir, c3: actual position c4: desire position

in row i could able to write time,dir,AP,DP one after the other. how can i write in to excel sheet
i have written below code to write in to text file.













Code:

import serial
import csv
import os
import time


def main():
    pass

if __name__ == '__main__':
    main()
    COUNT=0
    while(COUNT<=60):
      ser=serial.Serial()
      ser.port=12
      ser.baudrate=9600
      ser.open()
      x=ser.read(150)
      foo=open("time.txt","a+");
      print x
      foo.write(x)
      foo.write("\n")
      foo.close();
      ser.close()
      time.sleep(60)
      COUNT=COUNT+1

      if(COUNT==60):
        COUNT=0


ajit.nayak87 03-11-2013 05:47 AM

Below code worked fine. Now can some one tell me how to put these seprated data into indivual columns for every 10 minute in Excel sheet. below i have written it for text one after the other
Code:

import serial
import csv
import os
import time


def main():
    pass

if __name__ == '__main__':
    main()
    COUNT=0
    while(COUNT<=2):
      ser=serial.Serial()
      ser.port=12
      ser.baudrate=9600
      ser.open()
      str=ser.read(150)
      val=str.split(":")
      print "value is",val
      lines=str.split("\r\n")
      ser.close()
      COUNT=COUNT+1
    for lines in lines[:-1]:
            parts=lines.split(":")
            foo=open("new.txt","a+");
            foo.write("%s," % ":".join(parts[1:]))
            print parts[1:]
            foo.write("\n")
            foo.close()



All times are GMT -5. The time now is 01:34 PM.