0

my group and I are interfacing Arduino and python on our Raspberry pi zero w so that an IR receiver connected to the Arduino sends the corresponding characters to python, which it does.However, we are trying to make the data sent to Ardunio do things now but Python is not recognizing the data. Here is the python code:

from time import sleep
import serial
ser = serial.Serial('/dev/ttyAMC0', 9600) # Establish the connection on a specific port
while True:
    print (ser.readline()) # Read the newest output from the Arduino
    IR = (ser.readline())
    if IR == 'FF6897\n':
        print('0')
    else:
        print('wtf')

and here is the Ardunio code:

void setup() {
  Serial.begin(9600); //set band rate
  Serial.println("Ready"); //print "Ready" once
}
void loop() {
  char inByte = ' ';

  if(Serial.available()){ //only send data back if data has been sent
    char inByte = Serial.read(); //read the incoming data
    Serial.println(inByte); //send the data back in a new line so that it is not
  }
  delay(100); //delay for 1/10 of a second
}

When button 0 is pressed on the remote python will receive

b'FF6897\r\n'

despite all the other stuff the FF6897 is what we expected. Therefore, we told python that is it sees that code print "0" However it just keeps printing "wtf" because it does not recognize the code for some reason. Any ideas how to make what Python is receiving something we can actually use?

0

Data communication are often serialised and transmit in byte instead of string. Bytes in python are indicated by the prefix b in front of the data as you shown b'FF6897\r\n' but you are trying to treat it as a str as your if statement is trying to compare, you can confirm that the return data are indeed in bytes by by running:

print(type(IR))

It will shown

`<class 'bytes'>`

There are two solutions here:

1) Use it as is, in this case, your if statement need to compare it with the return bytes like this:

if IR == b'FF6897\r\n':

2) Convert it to str as your if statement is doing, but the data need to be decoded first:

IR = (ser.readline()).decode("utf-8")
if IR == 'FF6897\r\n':
  • Thank you hcheung your suggestion worked! I added the b where you did and that was it! – user85352 Apr 29 '18 at 1:26
2

It's receiving both a return char, and a newline. But you are only checking for the newline.

from time import sleep
import serial
ser = serial.Serial('/dev/ttyAMC0', 9600) # Establish the connection on a specific port
while True:
    print (ser.readline()) # Read the newest output from the Arduino
    IR = (ser.readline())
    if IR == 'FF6897\r\n': #check for carriage return and newline
        print('0')
    else:
        print('wtf')
  • This is only part of the problem. But the real problem is the return data is in bytes, not in str as the if statement is trying to compare. – hcheung Apr 27 '18 at 15:38

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.