How can I read just one serial port message in python?

1

Good people, it turns out that I am communicating an Arduino UNO with a RaspberryPI and what happens to me is that when an Arduino button is pressed it sends a simple serial number to RaspberryPi and when it recives it will emit a sound, the question is that to avoid annoying people pressing the button many times and locking the system, I have put a delay, but apparently this does not work, because if you press the button many times, it is as if the serial was waiting and if you press 5 times the button, every 10 seconds the sound will be emitted as messages continue to arrive from the serial, until the 5 times you have pressed the button, it is as if that were in a waiting queue when using the sleep (). The question is, how can I modify my code so that it receives 1 message from the serial, every 10s and the other pulsations discard them if they are within those 10s.

Code:

ser = serial.Serial('/dev/ttyACM0',9600)
s = [0]
try:
    while True:
        read_serial=ser.readline()
        print read_serial
        playSong()
        sleep(10)  
    
asked by Santiago D'Antuoni 04.09.2017 в 23:06
source

2 answers

1

Indeed, you have noticed them, even if you do a sleep the messages keep coming, so the alternative is to ignore them until the time you want has passed. One way to do it is this:

ser = serial.Serial('/dev/ttyACM0',9600)
s = [0]
last_time = 0
try:
    while True:
        read_serial=ser.readline()
        print read_serial
        if time.time() - last_time >= 10:
          playSong()
          last_time = time.time() 

We use the time.time() function that returns the number of seconds since 1 / 1/1970 at 00:00:00 and we check it against a variable last_time that has the same data but the last execution of playSong() (or 0 if it is the first execution). This should print all the data received by console, but only when the 10 seconds are completed, playSong() will be executed.

    
answered by 04.09.2017 / 23:44
source
1

Exactly what you say occurs, if you continue sending data during the delay, they remain in the buffer waiting to be read. In your case it should be enough to clean all the data present in the buffer and then read while waiting for new ones to arrive. This is done with flushInput :

ser = serial.Serial('/dev/ttyACM0',9600)
s = [0]
try:
    while True:
        ser.flushInput()
        read_serial=ser.readline()
        print read_serial
        playSong()
        sleep(10)  
  

Note : if the information arrives as blocks of bytes, it is possible that the chain is cut because of the flush if it coincides with the moment in which it is being written, losing data in the process. If this is the case you must implement some method to avoid this. One possibility is to have Arduino send the data next to a delimiter at the beginning of the chain, so that when reading it you can check if it is integrated or was partially eliminated by the flush.

    
answered by 04.09.2017 в 23:45