Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 11th, 2013, 03:09 AM
ajit.nayak87 ajit.nayak87 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Location: Delhi
Posts: 35 ajit.nayak87 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 6 m 42 sec
Reputation Power: 1
Facebook
Wink Python serial read

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

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

Reply With Quote
  #2  
Old March 11th, 2013, 03:19 AM
partoj partoj is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 138 partoj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 39 m 41 sec
Reputation Power: 1
Please use code tags when pasting your code, otherwise it's impossble to read it.

Use split() to extract the values you want to get:

Code:
>>> str = "time and date: 2013/03/11 09:22"
>>> val = str.split(":")
>>> val
['time and date', ' 2013/03/11 09', '22']
>>> time_and_date = ":".join(val[1:])

Reply With Quote
  #3  
Old March 11th, 2013, 03:37 AM
ajit.nayak87 ajit.nayak87 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Location: Delhi
Posts: 35 ajit.nayak87 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 6 m 42 sec
Reputation Power: 1
Facebook
How to get the serial data ??
when i say ser.read(100) i am getting those data.
how to write in to excel sheet.???



Quote:
Originally Posted by partoj
Please use code tags when pasting your code, otherwise it's impossble to read it.

Use split() to extract the values you want to get:

Code:
>>> str = "time and date: 2013/03/11 09:22"
>>> val = str.split(":")
>>> val
['time and date', ' 2013/03/11 09', '22']
>>> time_and_date = ":".join(val[1:])

Reply With Quote
  #4  
Old March 11th, 2013, 03:59 AM
ajit.nayak87 ajit.nayak87 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Location: Delhi
Posts: 35 ajit.nayak87 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 6 m 42 sec
Reputation Power: 1
Facebook
output window:

value is ['calc dir', ' in FWD\r\nActuator On/OFF', '0\r\nMode', '1\r\nLocal Date & Time', '11/3/2013 14', '23', '21\r\nDesired Angle', '-15.32\r\n Actual Pos', '-10.40\r\n----------']
date is in FWD
Actuator On/OFF:0
Mode:1
Local Date & Time:11/3/2013 14:23:21
Desired Angle:-15.32
Actual Pos:-10.40

what are changes need to made in below code


code modified :
Code:

import serial
import csv
import os
import time


def main():
    pass

if __name__ == '__main__':
    main()
    COUNT=0
    while(COUNT<5):
      ser=serial.Serial()
      ser.port=12
      ser.baudrate=9600
      ser.open()
      str=ser.read(150)
      val=str.split(":")
      print "value is",val
      time_date=":".join(val[1:])
      print "date is ",time_date
      foo=open("time.txt","a+");
      print str
      foo.write(str)
      foo.write("\n")
      foo.close();
      ser.close()
      COUNT=COUNT+1






Quote:
Originally Posted by ajit.nayak87
How to get the serial data ??
when i say ser.read(100) i am getting those data.
how to write in to excel sheet.???

Reply With Quote
  #5  
Old March 11th, 2013, 04:23 AM
partoj partoj is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 138 partoj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 39 m 41 sec
Reputation Power: 1
I've modified your example a little, hope that is ok. I've also had to comment out the serial stuff, but if you uncomment those lines and remove my "str =" line it should work.



Code:
from __future__ import with_statement
import serial

if __name__ == '__main__':
    for _ in range(0, 5):
#        ser=serial.Serial()
#        ser.port=12
#        ser.baudrate=9600
#        ser.open()
#        str=ser.read(150)
#        ser.close()
        str = 'calc dir: in FWD\r\nActuator On/OFF:0\r\nMode:1\r\nLocal Date & Time:11/3/2013 14:23:21\r\nDesired Angle:-15.32\r\n Actual Pos:-10.40\r\n----------'
        lines = str.split("\r\n")   # First get each line
        with open("time.txt", "a+") as f:   # the 'with' statement also closes the file
            for line in lines[:-1]:    # Ignore the last line with only hyphens
                parts = line.split(":")
                f.write("%s," % ":".join(parts[1:]))
            f.write("\n")

Reply With Quote
  #6  
Old March 11th, 2013, 04:39 AM
ajit.nayak87 ajit.nayak87 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Location: Delhi
Posts: 35 ajit.nayak87 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 6 m 42 sec
Reputation Power: 1
Facebook
What does F indicate in line of code

Quote:
Originally Posted by partoj
I've modified your example a little, hope that is ok. I've also had to comment out the serial stuff, but if you uncomment those lines and remove my "str =" line it should work.



Code:
from __future__ import with_statement
import serial

if __name__ == '__main__':
    for _ in range(0, 5):
#        ser=serial.Serial()
#        ser.port=12
#        ser.baudrate=9600
#        ser.open()
#        str=ser.read(150)
#        ser.close()
        str = 'calc dir: in FWD\r\nActuator On/OFF:0\r\nMode:1\r\nLocal Date & Time:11/3/2013 14:23:21\r\nDesired Angle:-15.32\r\n Actual Pos:-10.40\r\n----------'
        lines = str.split("\r\n")   # First get each line
        with open("time.txt", "a+") as f:   # the 'with' statement also closes the file
            for line in lines[:-1]:    # Ignore the last line with only hyphens
                parts = line.split(":")
                f.write("%s," % ":".join(parts[1:]))
            f.write("\n")

Reply With Quote
  #7  
Old March 11th, 2013, 04:45 AM
partoj partoj is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 138 partoj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 39 m 41 sec
Reputation Power: 1
Quote:
Originally Posted by ajit.nayak87
What does F indicate in line of code

f is the file object used to write to "time.txt".

Reply With Quote
  #8  
Old March 11th, 2013, 04:47 AM
ajit.nayak87 ajit.nayak87 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Location: Delhi
Posts: 35 ajit.nayak87 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 6 m 42 sec
Reputation Power: 1
Facebook
i have tried above code. But it is not writing whole data into parts
it will write up to time and date , missing desired and actual angle from part. how to get entire line of data then seprating out.
Any specific instrction to check EOF









Quote:
Originally Posted by ajit.nayak87
What does F indicate in line of code

Reply With Quote
  #9  
Old March 11th, 2013, 04:54 AM
ajit.nayak87 ajit.nayak87 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Location: Delhi
Posts: 35 ajit.nayak87 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 6 m 42 sec
Reputation Power: 1
Facebook
output after modification of code

[' in FWD']
['0']
['1']
['11/3/2013 15', '16', '50']









Quote:
Originally Posted by ajit.nayak87
What does F indicate in line of code

Reply With Quote
  #10  
Old March 11th, 2013, 04:56 AM
partoj partoj is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 138 partoj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 39 m 41 sec
Reputation Power: 1
Do you nean after my modification of the code, or yours? If the latter, please post your updated code.

Reply With Quote
  #11  
Old March 11th, 2013, 04:57 AM
ajit.nayak87 ajit.nayak87 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Location: Delhi
Posts: 35 ajit.nayak87 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 6 m 42 sec
Reputation Power: 1
Facebook
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(100)
     # val=str.split(":")
     # print "value is",val

      lines=str.split("\r\n")
      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()

      """calc_dir=":".join(val[1:])
      print "calc_dir:",calc_dir

      act_on_off=":".join(val[1:])
      print "act_on:",act_on_off

      mode=":".join(val[1:])
      print "mode:",mode

      date_time=":".join(val[1:])
      print "date_time:",date_time

      desire_angle=":".join(val[1:])
      print "desired_angle:",desire_angle

      actual_angle=":".join(val[1:])
      print "actual_angle:",actual_angle
"""


      foo=open("time.txt","a+");
     # print str
      #foo.write(str)
      foo.write("\n")
      foo.close();
      ser.close()
      COUNT=COUNT+1








QUOTE=partoj]Do you nean after my modification of the code, or yours? If the latter, please post your updated code.[/QUOTE]

Reply With Quote
  #12  
Old March 11th, 2013, 05:11 AM
partoj partoj is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 138 partoj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 39 m 41 sec
Reputation Power: 1
You changed the code to only read 100 bytes, earlier you read 150 bytes.

Code:
str=ser.read(100)

Reply With Quote
  #13  
Old March 11th, 2013, 05:16 AM
ajit.nayak87 ajit.nayak87 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Location: Delhi
Posts: 35 ajit.nayak87 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 6 m 42 sec
Reputation Power: 1
Facebook
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()


Modified the code and it started working fine. Now i just wanted to know i put those data in excel sheet under specified column
like DIR,timeand date, desire angle, actual angle.

it some how look like
1)
dir-timeand date-desireangle- actual angle
fwd-23-3-13 3:4:50-30 -28

2) how to give delay in program . suppose i wanna write it for every 10 min how can give delay

Reply With Quote
  #14  
Old March 11th, 2013, 07:47 AM
partoj partoj is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 138 partoj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 39 m 41 sec
Reputation Power: 1
Quote:
Originally Posted by ajit.nayak87
Modified the code and it started working fine. Now i just wanted to know i put those data in excel sheet under specified column
like DIR,timeand date, desire angle, actual angle.

it some how look like
1)
dir-timeand date-desireangle- actual angle
fwd-23-3-13 3:4:50-30 -28

Excel can read csv files, so I suggest you simply output your data as comma-separated values. Make the first line a header with the column names you want to have.

Quote:
2) how to give delay in program . suppose i wanna write it for every 10 min how can give delay


Either use time.sleep(), or (imho better) run a cron script every 10 minutes that will run your script.

Reply With Quote
  #15  
Old March 11th, 2013, 07:49 AM
partoj partoj is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 138 partoj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 39 m 41 sec
Reputation Power: 1
If you do want real excel interation, try http://www.python-excel.org/ (haven't used it myself though).

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Python serial read

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap