March 11th, 2013, 03:09 AM
-
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
March 11th, 2013, 03:19 AM
-
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:])
March 11th, 2013, 03:37 AM
-
How to get the serial data ??
when i say ser.read(100) i am getting those data.
how to write in to excel sheet.???
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:])
March 11th, 2013, 03:59 AM
-
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
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.???
March 11th, 2013, 04:23 AM
-
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")
March 11th, 2013, 04:39 AM
-
What does F indicate in line of code
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")
March 11th, 2013, 04:45 AM
-
Originally Posted by ajit.nayak87
What does F indicate in line of code
f is the file object used to write to "time.txt".
March 11th, 2013, 04:47 AM
-
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
Originally Posted by ajit.nayak87
What does F indicate in line of code
March 11th, 2013, 04:54 AM
-
output after modification of code
[' in FWD']
['0']
['1']
['11/3/2013 15', '16', '50']
Originally Posted by ajit.nayak87
What does F indicate in line of code
March 11th, 2013, 04:56 AM
-
Do you nean after my modification of the code, or yours? If the latter, please post your updated code.
March 11th, 2013, 04:57 AM
-
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]
March 11th, 2013, 05:11 AM
-
You changed the code to only read 100 bytes, earlier you read 150 bytes.
March 11th, 2013, 05:16 AM
-
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
March 11th, 2013, 07:47 AM
-
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.
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.
March 11th, 2013, 07:49 AM
-
If you do want real excel interation, try http://www.python-excel.org/ (haven't used it myself though).