0

I'm doing a preliminary script for reading the serial port when i send the "1" over serial for infinite time every 3 minutes. I need a thread script because this it will have to be integrated with others function together serial . The serial port is connected to an arduino with sensor but i'm not sure program is formally corrected. Anyone can help me please? Thanks

#!/usr/bin/python
import time
import serial
import threading
import time
import io

ser=serial.Serial('/dev/ttyUSB0', 57600, timeout=3)
def printit():
threading.Timer(180.0, printit).start()
ser.write(b'1\n')
lettura=ser.readline()
print lettura

try:
   printit()
except:
   print "Error: unable to start thread"
while 1:
   pass
fabrizio bianchi
  • 63
  • 1
  • 1
  • 9
  • Please fix your indentation. It's also not clear to me what you are asking exactly. Are you getting an error? – roganjosh Sep 15 '18 at 09:16
  • Yes i'm getting an error when introducing the split line. Traceback (most recent call last): File "scrivi.py", line 36, in t.start() File "scrivi.py", line 19, in start self.f() File "scrivi.py", line 28, in send_bytes temp,umid=lettura.split(' ') ValueError: need more than 1 value to unpack – fabrizio bianchi Sep 16 '18 at 08:08

1 Answers1

0

The recursive way you have defined the printit function will create an infinite loop, however, that definition makes controlling the execution of the thread somewhat complicated. You might want to consider writing a custom Thread class that accepts the function that you want to run at a given interval, and the interval you want to wait (similar to what you pass to Timer).

import threading
import time

class RepeatThread(threading.Thread):

    def __init__(self, interval, f):
        self.interval = interval
        self.f = f

    def start(self):
        while True:
            self.f()
            time.sleep(self.interval)

Then you can use the class like this,

import serial
ser = serial.Serial('/dev/ttyUSB0', 57600, timeout=3)

def send_bytes():
    ser.write(b'1\n')

t = RepeatThread(180.0, send_bytes)
t.start()
while True:
   pass

Additionally, the answer to this question offers a lot of great information about how to build infinite looping threads in python. You will probably also want to follow their instructions about locking while accessing the serial device (if multiple threads could potentially use it at the same time).

Souperman
  • 5,057
  • 1
  • 14
  • 39
  • Thanks for your code. i'm getting an error when i add the split lines. Traceback (most recent call last): File "scrivi.py", line 36, in t.start() File "scrivi.py", line 19, in start self.f() File "scrivi.py", line 28, in send_bytes temp,umid=lettura.split(' ') ValueError: need more than 1 value to unpack Can you help me please? – fabrizio bianchi Sep 16 '18 at 08:10
  • That sounds like a separate question. From the looks of things, you’re trying to unpack the output of split into two values but split only retuned one value. Print lettura.split and you’ll probably see the problem – Souperman Sep 17 '18 at 06:31