LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-11-2013, 03:23 AM   #1
ajit.nayak87
LQ Newbie
 
Registered: Mar 2013
Posts: 2

Rep: Reputation: Disabled
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
 
Old 03-11-2013, 05:47 AM   #2
ajit.nayak87
LQ Newbie
 
Registered: Mar 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
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()

Last edited by ajit.nayak87; 03-11-2013 at 05:48 AM. Reason: incomplete
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Read datas after write on serial port truboy Programming 4 03-08-2011 11:21 AM
bash: serial port write THEN read Metermon Programming 1 08-11-2010 09:05 AM
[SOLVED] Serial Programming, Write-Read Issue a5k3r Programming 3 08-09-2010 10:49 AM
[bash] synchronizing write/read to serial t00c00l Programming 3 08-01-2009 08:44 AM
Serial port Read Write SeanatIL Programming 2 07-14-2004 03:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration