Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a little script in Python which I am brand new to. I want to check if a certain word appears in ser.readline(). The syntax for the If statement is not right and I am not sure how to lay this out so it continuously reads the serial for the word "sound". I've attached an output image below so you can see how it is printing. I'd like to trigger an MP3 as it finds the word "sound" but as of yet I haven't even managed to get it to print a confirmation saying its found the word.

import serial
import time
ser = serial.Serial('COM6', 9600, timeout=0)

while 1:
 try:
  print (ser.readline())
  time.sleep(1)

  **if "sound" in ser.readline():
        print("foundsound")**

 except ser.SerialTimeoutException:
  print('Data could not be read')
  time.sleep(1)

Serial Printing

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

You may be reading from the port more often than you intend to. I would call ser.readline() just once per iteration of your main loop:

while True:
    try:
        data = ser.readline().decode("utf-8")     # Read the data
        if data == '': continue                   # skip empty data

        print(data)                               # Display what was read
        time.sleep(1)

        if "sound" in data:
           print('Found "sound"!')

    except ser.SerialTimeoutException:
        print('Data could not be read')
        time.sleep(1)
share|improve this answer
    
I got Traceback (most recent call last): File "C:/Users/Dan/Desktop/ConnectArduino.py", line 15, in <module> if "sound" in data: TypeError: Type str doesn't support the buffer API During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:/Users/Dan/Desktop/ConnectArduino.py", line 18, in <module> except ser.SerialTimeoutException: AttributeError: 'Serial' object has no attribute 'SerialTimeoutException' –  Dan W 2 days ago
    
See edit. I believe you need to convert the bytes array to a string by decoding it with a specified encoding. I'm assuming UTF-8 here, but it really depends on your device. –  Jonathon Reinhart 2 days ago
    
The code was intending to read the serial every second, however it's not completely necessary so long as it picks up "sound" whenever it gets posted to serial by arduino. I am getting an error with decode saying invalid syntax? –  Dan W 2 days ago
    
I missed the . –  Jonathon Reinhart 2 days ago
    
Aha yep I found that, seems to be printing fine now. Much obliged! –  Dan W 2 days ago
show 2 more comments

can you try:

import serial
import time
ser = serial.Serial('COM6', 9600, timeout=0)

while 1:
 try:
  line = ser.readline()
  print line
  time.sleep(1)

  **if "sound" in line:
        print("foundsound")**

 except ser.SerialTimeoutException:
  print('Data could not be read')
  time.sleep(1)
share|improve this answer
    
Same Error as other comment –  Dan W 2 days ago
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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