Python Forum
I need a serial device expert!
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I need a serial device expert!
#6
Great thank you!

I have been reading up threading and the way I understand it, it is basically expanding your code from vertical to horizontal (running it in parallel). So if you had 5 workers, instead of each waiting for the one before it to be done before it can do its task, they can all do them at the same time. Here is a script from a video I watched:

import threading
import time


def talker(n, name):
    print('{} is sleeping\n'.format(name))
    time.sleep(n)
    print('{} has woken up!\n'.format(name))


start = time.time()

threads_list = []

for k in range(5):
    t = threading.Thread(target=talker, name='thread{}'.format(k),args=(5, 'thread{}'.format(k)))
    threads_list.append(t)
    t.start()


for t in threads_list:
    t.join()

end = time.time()

print('time taken: {}'.format(end-start))
With out threading this task would take 25 seconds. With threading, each worker does the same job at the same time. So the code only runs for 5 seconds:

Output:
thread0 is sleeping thread1 is sleeping thread2 is sleeping thread3 is sleeping thread4 is sleeping thread0 has woken up! thread2 has woken up! thread3 has woken up! thread4 has woken up! thread1 has woken up! time taken: 5.001632213592529
But how does this work with ser.readline Larz60+. How do I define a function to read the line and write it to a file?

Can I have workers perform different tasks?

Here's what I have tried:

import threading
import time
import datetime
import serial


ser = serial.Serial()
ser.port = 'COM2'
ser.baudrate = 460800
ser.timeout = 0.02
ser.bytesize = 8
ser.stopbits = 1

ser.open()

f = open('thread.txt', 'w')


def reader():
    line = ser.readline()
    time = datetime.datetime.now()
    f.write(str(time) + '    ' + str(line.replace(b'\r\n', b'').decode('utf-8')) + '\n')


while 1:
    start = time.time()

    threads_list = []

    for k in range(5):
        t = threading.Thread(target=reader)
        threads_list.append(t)
        t.start()

    for t in threads_list:
        t.join()

    end = time.time()

    print('time taken: {}'.format(end-start))


ser.close()
and the result:

Output:
2018-12-13 17:11:43.397547 2018-12-13 17:11:43.397547 2018-12-13 17:11:43.397547 2018-12-13 17:11:43.397547 2018-12-13 17:11:43.397547 2018-12-13 17:11:43.418060 2018-12-13 17:11:43.418060 2018-12-13 17:11:43.418060 2018-12-13 17:11:43.418060 2018-12-13 17:11:43.418060 2018-12-13 17:11:43.438575 2018-12-13 17:11:43.438575 2018-12-13 17:11:43.438575 2018-12-13 17:11:43.438575 ...
It's not even reading the line from the port.
Reply


Messages In This Thread
I need a serial device expert! - by Caesars_10th - Dec-05-2018, 12:04 AM
RE: I need a serial device expert! - by Larz60+ - Dec-05-2018, 12:51 AM
RE: I need a serial device expert! - by Larz60+ - Dec-11-2018, 08:07 PM
RE: I need a serial device expert! - by Caesars_10th - Dec-14-2018, 12:21 AM
RE: I need a serial device expert! - by Larz60+ - Dec-14-2018, 02:58 AM
RE: I need a serial device expert! - by Larz60+ - Dec-14-2018, 12:05 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  pyserial/serial "has no attribute 'Serial' " gowb0w 9 5,944 Aug-24-2023, 07:56 AM
Last Post: gowb0w
  How would you (as an python expert) make this code more efficient/simple coder_sw99 3 1,924 Feb-21-2022, 10:52 AM
Last Post: Gribouillis
  Reading UDP from external device without device software ikdemartijn 2 3,574 Dec-03-2019, 04:29 PM
Last Post: Larz60+
  Display device details i.e connected with more than one device shintonp 6 5,494 May-10-2017, 06:00 AM
Last Post: shintonp

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020