Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 1241

Python • reading a string from serial

$
0
0
Hello,
I am a complete beginner to Raspberry Pi and Python. Currently, I am trying to convert the Arduino code that reads the serial string sent from RFID card through serial. The string has 12 characters, and I need to deal with the underscores and the carriage return. Here's the fragment of the Arduino code:

Code:

void loop(){  if (RFID.available() > 0)   {     c = RFID.read();         // underscore? ignore it and jump to the next character     if (c == '_') return;         // send the character     Serial.write(c);     // if it was a carriage return send a linefeed character, too     if (c == '\r')  Serial.write('\n');  }}

Now, for Raspberry Pi I am using the following:

Code:

import serialser = serial.Serial(' /dev/ttyAMA0', 9600)while True:   string = ser.read()   if '\r' in string:       print('\r\n')  
I am ignoring the underscores for now. It seems that the Arduino's method of finding the character in string does not work for Python. What would be the correct method of finding the carriage return? What is the Python's alternative for Arduino command "return"?

Thanks!

Statistics: Posted by santon — Wed Feb 26, 2025 3:21 pm



Viewing all articles
Browse latest Browse all 1241

Trending Articles