gbru316
Posts: 4
Joined: Tue Mar 01, 2016 3:47 am

Parsing serial data from arduino

Tue Mar 01, 2016 3:53 am

I'm trying to read serial data and use it in some logic statements.

The plan is to play audio on RPi when a button is pushed (connected to arduino DIO pins), but at the moment I'm just trying to manipulate the serial data... baby steps :lol:

I'm able to read the data with the RPi (print the serial data received) but when I try to analyze it, I get nothing.

Code: Select all

while 1:
     x=ser.readline(2)
     print x
     if x == 25:
          print("pushed")
Shell window displays 25 when the button is pushed, but "pushed" does not display. Conversely, when I set the statement to !=, it says "pushed." I'm guessing it has something to do with data type (binary vs ascii, string vs int, etc)? Anyway, what's the easiest way to implement this?

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: Parsing serial data from arduino

Tue Mar 01, 2016 4:48 am

gbru316 wrote:

Code: Select all

     if x == 25:
          print("pushed")
Shell window displays 25 when the button is pushed, but "pushed" does not display. Conversely, when I set the statement to !=, it says "pushed." I'm guessing it has something to do with data type (binary vs ascii, string vs int, etc)? Anyway, what's the easiest way to implement this?
If you say

if x == "25":

doesn't it work?

User avatar
PeterO
Posts: 5951
Joined: Sun Jul 22, 2012 4:14 pm

Re: Parsing serial data from arduino

Tue Mar 01, 2016 8:12 am

stderr wrote: If you say
if x == "25":
doesn't it work?
or the other way round should work as well

Code: Select all

if int(x) == 25:
Also might need to watch for problems caused by any white space that may surround the digits in the string.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

gbru316
Posts: 4
Joined: Tue Mar 01, 2016 3:47 am

Re: Parsing serial data from arduino

Tue Mar 01, 2016 1:25 pm

Found the solution: had to use '25' instead of 25 or "25."

User avatar
liudr
Posts: 687
Joined: Sat Jun 01, 2013 12:11 am
Location: Central MN, USA
Contact: Website

Re: Parsing serial data from arduino

Tue Mar 01, 2016 10:06 pm

I think your problem is that you used Serial.print instead of Serial.write in your Arduino code. You should have posted that code.
Arduino data loggers, user interface, printed circuit board designer since 2009, RPI 3B 2B 2B Zero Jessie, assembly/C/C++/java/python programmer since the 80's

Return to “Beginners”