prima-mima
Posts: 13
Joined: Tue Aug 06, 2013 5:20 pm

how take data string from serial and write into file

Sat Aug 24, 2013 8:53 pm

hi all,
i have program list like this,

Code: Select all

import serial
import time

port = serial.Serial("/dev/ttyAMA0", baudrate=600, timeout= 3.0)

while True:
        rcv = port.read(5)
        value = (rcv)
        myString = str(value)
        b = open("/var/www/lampu1.txt","a")
        b.write(myString)
        b.close()
that program can receive data from serial and all data will be save in to file "lampu1.txt".
i just want to take 1 string data from the data are received and write into file.
for example:
data receive=89435
how i can take the string data, if for example i want to take string data [2]=9 and write into file "lampu1.txt" :?: :?: :( :(
anyone can help me please,
thank you,,, :)

User avatar
paddyg
Posts: 2331
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: how take data string from serial and write into file

Sat Aug 24, 2013 9:28 pm

you have to use file seek to write characters part way along an already written file. Was that what you were asking?
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

WebPi
Posts: 262
Joined: Wed Apr 10, 2013 6:47 pm
Location: Birmingham, UK
Contact: Website

Re: how take data string from serial and write into file

Sat Aug 24, 2013 9:57 pm

It looks as if you receive 5 bytes and store them as a string in rcv, and convert that to a string in myString. If you just want to write the second character in myString, try changing the line that writes to the file like this:

Code: Select all

b.write(myString[2])
If you want to add a new line after each character, you could do this:

Code: Select all

b.write(myString[2]+"\n")
This page has good info on working with strings: http://docs.python.org/2/tutorial/intro ... ml#strings
raspberrywebserver.com - Raspberry Pi tutorials
LinuxWebServers.net - Linux Web Server tutorials and examples
pyplate.com - Python web publishing tool

prima-mima
Posts: 13
Joined: Tue Aug 06, 2013 5:20 pm

Re: how take data string from serial and write into file

Sun Aug 25, 2013 5:05 pm

WebPi wrote:It looks as if you receive 5 bytes and store them as a string in rcv, and convert that to a string in myString. If you just want to write the second character in myString, try changing the line that writes to the file like this:

Code: Select all

b.write(myString[2])
If you want to add a new line after each character, you could do this:

Code: Select all

b.write(myString[2]+"\n")
This page has good info on working with strings: http://docs.python.org/2/tutorial/intro ... ml#strings
thanks for your respond,
i ever try like you said to add [2] in b.write(myString),
but when i am execute the program, the program is stoping and occur error.
this the error occur on terminal,

Code: Select all

Traceback (most recent call last):
  File "terima.py", line 11, in <module>
    b.write(myString[1])
IndexError: string index out of range
you know what the meaning and what should i must add in my program :?: :?:
i am newbie for python script, sorry... :(

WebPi
Posts: 262
Joined: Wed Apr 10, 2013 6:47 pm
Location: Birmingham, UK
Contact: Website

Re: how take data string from serial and write into file

Sun Aug 25, 2013 5:17 pm

This means there isn't a second element in the list. Maybe the call to port.read wasn't successful and didn't return 5 bytes.

Try printing rcv and myString before you write to the file:

Code: Select all

print "rcv="+rcv+", myString="+myString
raspberrywebserver.com - Raspberry Pi tutorials
LinuxWebServers.net - Linux Web Server tutorials and examples
pyplate.com - Python web publishing tool

prima-mima
Posts: 13
Joined: Tue Aug 06, 2013 5:20 pm

Re: how take data string from serial and write into file

Sun Aug 25, 2013 6:51 pm

paddyg wrote:you have to use file seek to write characters part way along an already written file. Was that what you were asking?
thanks for your respond sir,
maybe yes i need that function.
But i confused how to use file seek function in my program. I am sorry, i am beginner in python script.

prima-mima
Posts: 13
Joined: Tue Aug 06, 2013 5:20 pm

Re: how take data string from serial and write into file

Sun Aug 25, 2013 7:04 pm

WebPi wrote:This means there isn't a second element in the list. Maybe the call to port.read wasn't successful and didn't return 5 bytes.

Try printing rcv and myString before you write to the file:

Code: Select all

print "rcv="+rcv+", myString="+myString
no error and work properly,
display on the terminal as the received.


User avatar
MrBunsy
Posts: 185
Joined: Mon Feb 20, 2012 1:48 pm
Location: Southampton, UK
Contact: Website

Re: how take data string from serial and write into file

Mon Aug 26, 2013 7:08 pm

Why keep closing and opening? Why not just keep the file open?

prima-mima
Posts: 13
Joined: Tue Aug 06, 2013 5:20 pm

Re: how take data string from serial and write into file

Mon Aug 26, 2013 8:45 pm

MrBunsy wrote:Why keep closing and opening? Why not just keep the file open?
still occur error if i am delete b.close.

User avatar
MrBunsy
Posts: 185
Joined: Mon Feb 20, 2012 1:48 pm
Location: Southampton, UK
Contact: Website

Re: how take data string from serial and write into file

Tue Aug 27, 2013 5:11 pm

Post the full new code? Post the full errors?

WebPi
Posts: 262
Joined: Wed Apr 10, 2013 6:47 pm
Location: Birmingham, UK
Contact: Website

Re: how take data string from serial and write into file

Tue Aug 27, 2013 5:39 pm

What happens if write the whole string to the file like this?

Code: Select all

    b.write(myString)
Does that write to the file successfully?
raspberrywebserver.com - Raspberry Pi tutorials
LinuxWebServers.net - Linux Web Server tutorials and examples
pyplate.com - Python web publishing tool

Return to “Python”