0

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

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
Dan W
  • 365
  • 1
  • 4
  • 20

2 Answers2

1

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)
Narek
  • 548
  • 6
  • 26
1

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)
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • I got Traceback (most recent call last): File "C:/Users/Dan/Desktop/ConnectArduino.py", line 15, in 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 except ser.SerialTimeoutException: AttributeError: 'Serial' object has no attribute 'SerialTimeoutException' – Dan W May 14 '14 at 23:58
  • See edit. I believe you need to [convert the `bytes` array to a string by decoding it with a specified encoding](http://stackoverflow.com/questions/606191/convert-byte-array-to-python-string). I'm assuming UTF-8 here, but it really depends on your device. – Jonathon Reinhart May 15 '14 at 00:01
  • 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 May 15 '14 at 00:03
  • Aha yep I found that, seems to be printing fine now. Much obliged! – Dan W May 15 '14 at 00:06
  • You're welcome! Just an FYI (after looking at some of your other questions): You can up-vote any answer that is helpful, in addition to accepting the one that solved your problem. – Jonathon Reinhart May 15 '14 at 00:08
  • Upvoted, sometimes forget after the excitement of getting things working haha! – Dan W May 15 '14 at 00:19