Page 1 of 1

CSV file?

Posted: Wed Mar 22, 2017 8:04 pm
by Spartan09
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)

Re: CSV file?

Posted: Wed Mar 22, 2017 8:11 pm
by scotty101
What have you tried so far?

Have you looked at the python documentation for writing to files and CSV?

Re: CSV file?

Posted: Wed Mar 22, 2017 9:05 pm
by pcmanbob
Hi.

Have a look at this post, viewtopic.php?f=32&t=177852
there is an example of how to do it on there.

Re: CSV file?

Posted: Wed Mar 22, 2017 9:09 pm
by Spartan09
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)

Re: CSV file?

Posted: Wed Mar 22, 2017 9:23 pm
by ghp
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'.

Re: CSV file?

Posted: Wed Mar 22, 2017 9:28 pm
by pcmanbob
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.

Re: CSV file?

Posted: Wed Mar 22, 2017 10:06 pm
by Spartan09
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

Re: CSV file?

Posted: Wed Mar 22, 2017 10:36 pm
by ghp
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.

Re: CSV file?

Posted: Wed Mar 22, 2017 10:46 pm
by Spartan09
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

Re: CSV file?

Posted: Thu Mar 23, 2017 5:49 am
by ghp
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

Re: CSV file?

Posted: Thu Mar 23, 2017 8:28 am
by pcmanbob
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.

Re: CSV file?

Posted: Thu Mar 23, 2017 11:11 am
by Spartan09
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.

Re: CSV file?

Posted: Thu Mar 23, 2017 11:15 am
by Spartan09
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.

Re: CSV file?

Posted: Thu Mar 23, 2017 12:03 pm
by tpylkko
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.

Re: CSV file?

Posted: Thu Mar 23, 2017 12:35 pm
by pcmanbob
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)