Spartan09
Posts: 7
Joined: Thu Mar 16, 2017 10:38 pm

CSV file?

Wed Mar 22, 2017 8:04 pm

Anyone know hoe i can save the scanned values into a .CSV file? the following code reads the RFID tag but doesn't save it in a CSV file

Code: Select all

import os
import threading
import urllib2
import Serial


port = serial.Serial("/dev/ttyS0", baudrate=9600, timeout=0.2)

while True:
    RFID = port.readline()
    if len(RFID)>10:
        print (RFID)

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: CSV file?

Wed Mar 22, 2017 8:11 pm

What have you tried so far?

Have you looked at the python documentation for writing to files and CSV?
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

pcmanbob
Posts: 9462
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: CSV file?

Wed Mar 22, 2017 9:05 pm

Hi.

Have a look at this post, viewtopic.php?f=32&t=177852
there is an example of how to do it on there.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Spartan09
Posts: 7
Joined: Thu Mar 16, 2017 10:38 pm

Re: CSV file?

Wed Mar 22, 2017 9:09 pm

So I tried the following but it still did not work

make a .CSV file called data
In write mode
past the value for RFID in the .CSV file

Code: Select all

import csv
import serial

port = serial.Serial("/dev/ttyS0", baudrate=9600, timeout=0.5)

while True:
        RFID = port.readline()
        if len(RFID)>10:
                print (RFID)

with open('data.csv' , 'w' , newline='') as fp:
    a = csv.writer(fp,delimiter=',')
    data = [RFID]
    a.writerows(data)

ghp
Posts: 1498
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: CSV file?

Wed Mar 22, 2017 9:23 pm

Hello,
you should check identation. To make it clear, two samples

Code: Select all

while True:
    print ("step 1")
print("step 2")
In this sample, the 'step 2' is never printed, as the while loop is only running for the idented 'step_1'-prrintout

Code: Select all

while True:
    print ("step 1")
    print("step 2")
With identation modified, now step_1 and step_2 are inside while loop.

The solution for your csv-writer is to ident the csv-writer code. And perhaps the 'writerows' should be 'writerow'.

pcmanbob
Posts: 9462
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: CSV file?

Wed Mar 22, 2017 9:28 pm

so if you looked at the example in the posted link you should have written some thing like this.

Code: Select all

import serial

port = serial.Serial("/dev/ttyS0", baudrate=9600, timeout=0.5)

while True:
	RFID = port.readline()
	if len(RFID)>10:
		print (RFID)
		csvresult = open("/home/pi/data.csv","a")
		data = str(RFID)
		csvresult.write(data + "," + "\n")
		csvresult.close
I have not test this but it should be close to what you need, it should produce a csv file with a single column of figures.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Spartan09
Posts: 7
Joined: Thu Mar 16, 2017 10:38 pm

Re: CSV file?

Wed Mar 22, 2017 10:06 pm

Thank you for showing my where I am going wrong. I got a lot of learning to do.
The code you have produced creates the csv file and fills it with the tag info as described, however the tag needs to be scanned twice as it is not saved in the .csv file the first time.

I am guessing this is something to do with the update rate or write rate? maybe a delay would help?

Thanks

ghp
Posts: 1498
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: CSV file?

Wed Mar 22, 2017 10:36 pm

Hello, perhaps the scan does not return the same number of chars each time ? Place a print direct after the RFID = port.readline() to check what is returned from the scanner.

Spartan09
Posts: 7
Joined: Thu Mar 16, 2017 10:38 pm

Re: CSV file?

Wed Mar 22, 2017 10:46 pm

No it's something to do with a loop.
There is a delay in printing the tag data to the csv file.
Although it will read the tag, it will only save the tag info to the csv file once the next tag has been scanned.
ie. it is always delayed until the next tag is scanned to write to the csv file

ghp
Posts: 1498
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: CSV file?

Thu Mar 23, 2017 5:49 am

Hello,
writing to a file is quite fast, usually. Is the csv-file very large ?
Did you insert the print(RFID) direct after the read ? And how does this printout look like ?
Post current code please.
Regards,
Gerhard

pcmanbob
Posts: 9462
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: CSV file?

Thu Mar 23, 2017 8:28 am

it may be that you are not giving the Pi time to read the tag before you try processing the answer.
Try putting a sleep(0.5) between the line reading the tag and the if statement,
like this

Code: Select all

while True:
   RFID = port.readline()
   time.sleep(0.5)
   if len(RFID)>10:
you will of course need to add "import time" to the top of your program as well.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Spartan09
Posts: 7
Joined: Thu Mar 16, 2017 10:38 pm

Re: CSV file?

Thu Mar 23, 2017 11:11 am

ghp wrote:Hello,
writing to a file is quite fast, usually. Is the csv-file very large ?
Did you insert the print(RFID) direct after the read ? And how does this printout look like ?
Post current code please.
Regards,
Gerhard

hello,

So when I put the print(RFID) after the readline the program get stuck in an infinite loop. It's like the serial port is constantly outputing blank data by transmitting a series of b'', then when the tag is scanned it registers the card but does not make the scv file.
Attachments
2017-03-23-105401_1824x984_scrot.png
2017-03-23-105401_1824x984_scrot.png (37.3 KiB) Viewed 3444 times

Spartan09
Posts: 7
Joined: Thu Mar 16, 2017 10:38 pm

Re: CSV file?

Thu Mar 23, 2017 11:15 am

pcmanbob wrote:it may be that you are not giving the Pi time to read the tag before you try processing the answer.
Try putting a sleep(0.5) between the line reading the tag and the if statement,
like this

Code: Select all

while True:
   RFID = port.readline()
   time.sleep(0.5)
   if len(RFID)>10:
you will of course need to add "import time" to the top of your program as well.

I'm afraid not. It is still delayed by one, just takes longer to write to the csv file. The csv file is 4.0Kib, so its not large at all. I don't know why it is doing this. It should read the tag and create file part and post it to the file.
It's like it only runs the read tag and create file part, but not the write to file part, for the first scan.

tpylkko
Posts: 409
Joined: Tue Oct 14, 2014 5:21 pm

Re: CSV file?

Thu Mar 23, 2017 12:03 pm

Why is the logical condition >10 in the if-statement? Maybe the card is giving outputs of 10 digits and since that is "not larger than 10" it doesn't write. Are you sure that it does not need to be >=10

Also, what about clearing RFID after the loop.

pcmanbob
Posts: 9462
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: CSV file?

Thu Mar 23, 2017 12:35 pm

Try running this code and then show us what data.csv contains, and please post the results using code tags then we can copy them and use the results to run code against. if you post images then people have to retype your code/results which takes time and can lead to errors.

Code: Select all

import serial
import time
port = serial.Serial("/dev/ttyS0", baudrate=9600, timeout=0.5)

while True:
	RFID = port.readline()
	if len(RFID) < 10:
		RFID = "error less than 10"
	print (RFID)
	csvresult = open("/home/pi/data.csv","a")
	data = str(RFID)
	csvresult.write(data + "," + "\n")
	csvresult.close
	time.sleep (0.1)	
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “Python”