0

I am using this code, to read a string of alphanumeric digits from an Arduino with a RFID sensor connected.:

import serial


ser = serial.Serial('/dev/ttyACM0',9600)
s = [0]
while True:
    read_serial=ser.readline()
    s[0] = str(ser.readline())
    print s[0]
    print read_serial

The Arduino uses this code which just prints to serial the unique ID of the RFID tag each time its tapped on the reader click link for arduino code: https://www.raspberrypi.org/forums/viewtopic.php?f=28&t=179457

My issue is that the raspberry pi works and reads out the Unique id when tapped on the reader attached to the Arduino, but it only prints it every 2 times it's tapped on the reader.

I would really appreciate any help with this! Thanks very much kind internet people!!

Updated code using advice by user:

    import serial


    ser = serial.Serial('/dev/ttyACM0',9600)
s = [0]
while True:
read_serial = ser.readline()
s[0] = str(read_serial)
print s[0]
print read_serial

Now instantly printing each time, but printing twice!

.............

Final working Code:

import serial


ser = serial.Serial('/dev/ttyACM0',9600)
s = [0]
while True:
read_serial = ser.readline()
print read_serial
  • Welcome to Raspberry Pi! Please take the tour and visit the helpcenter to see how things work here. Please note that meta information ('please help') should be kept to a minimum in post and title. – Ghanima Apr 2 '17 at 14:31
  • Of course I will not include this in the future, thanks for the advice! – Hugo Til Apr 2 '17 at 15:00
2

In your loop you're reading twice from the serial port:

read_serial=ser.readline()
s[0] = str(ser.readline())

which is probably not what you're trying to intend here. It reads more from the serial port and hiding it from the user. It should probably look more like this (though I have to admit that I am not sure where you're going with this s[0] and s = [0] after all):

read_serial = ser.readline()
s[0] = str(read_serial)

Per the additional comment, why it is printing each code twice. Well look at the code again

read_serial = ser.readline()
s[0] = str(read_serial)
print s[0]
print read_serial

read_serial is a string as is s[0] which then holds the same content which you print both - so you'll see the same thing twice. So the question really is, what do you expect this s[0] = ... to do?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.