Ambuje Gupta
Ambuje Gupta

Reputation: 73

Not able to implement Multiprocessing in python(The parameter is incorrect)

EDIT

I am trying to run to functions simultaneously using multiprocessing

import serial
import time

from multiprocessing import Process
import sys

sertx = serial.Serial('COM4', 115200)
serrx = serial.Serial('COM3', 115200)

rx_t=0
tx_t=0

def rx(serrx):

    global rx_t

    while True:
        print("hi")
        read_serial=serrx.readline()
        rx_t = time.time()
        print(read_serial)
        print('rx: ',rx_t)

def tx(sertx):
    print("started")
    global tx_t
    while True:

        msg = str(1)
        # print('sending: ',msg.encode())

        msgstat = 'A' + msg
        #print(msgstat)
        #print(type(msgstat))
        tx_t = time.time()
        sertx.write(msg.encode())
        print('tx: ',tx_t)

if __name__ == '__main__': 
    p1 = Process(target=tx,args=(sertx,))
    p2 = Process(target=rx,args=(serrx,))
    p1.start()
    p2.start()
    p1.join()
    p2.join()

Error

Traceback (most recent call last):
  File "c:/Users/ambuj/Documents/Python Scripts/wave.py", line 58, in <module>
    p1.start()
  File "C:\Users\ambuj\Anaconda3\lib\multiprocessing\process.py", line 112, in start
    self._popen = self._Popen(self)
  File "C:\Users\ambuj\Anaconda3\lib\multiprocessing\context.py", line 223, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "C:\Users\ambuj\Anaconda3\lib\multiprocessing\context.py", line 322, in _Popen
    return Popen(process_obj)
  File "C:\Users\ambuj\Anaconda3\lib\multiprocessing\popen_spawn_win32.py", line 89, in __init__
    reduction.dump(process_obj, to_child)
  File "C:\Users\ambuj\Anaconda3\lib\multiprocessing\reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)
ValueError: ctypes objects containing pointers cannot be pickled
PS C:\Users\ambuj\Documents\Python Scripts> Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Users\ambuj\Anaconda3\lib\multiprocessing\spawn.py", line 99, in spawn_main
    new_handle = reduction.steal_handle(parent_pid, pipe_handle)
  File "C:\Users\ambuj\Anaconda3\lib\multiprocessing\reduction.py", line 82, in steal_handle
    _winapi.PROCESS_DUP_HANDLE, False, source_pid)
OSError: [WinError 87] The parameter is incorrect

Basically what I am doing/want to do

I have a transmitter that keeps on transmitting data and I have a receiver that keeps on receiving data. When I transmit the data I note down the time, when I receive the data I note down its time. Scripts are running continuously. I am running these scripts in parallel. As I want to run these scripts simultaneously I am using multiprocessing.

Thanks

Upvotes: 0

Views: 338

Answers (1)

Artiom  Kozyrev
Artiom Kozyrev

Reputation: 3836

I'm sure you do not need to use multiprocessing module for such kind of oeprations, it is used to boost computations. I guess you want reading from one port and writing to another in one process, otherwise you could just write to different programs and run them independenly. For IN/OUT threading module is used.

I've never worked with serial library, but know how to create process whcih listen to input in one thread and print to another thread:

This is my messenger program

I guess that code can work this way:

import serial
import time
from threading import Thread


def rx(_ser_rx):
    while True:
        print("hi")
        read_serial = _ser_rx.readline()
        rx_t = time.time()
        print(read_serial)
        print('rx: ', rx_t)


def tx(_ser_tx):
    print("started")
    while True:
        msg = "1"
        tx_t = time.time()
        _ser_tx.write(msg.encode())
        print('tx: ', tx_t)


if __name__ == '__main__':
    ser_tx = serial.Serial('COM4', 115200)
    ser_rx = serial.Serial('COM3', 115200)
    t1 = Thread(target=rx, args=(ser_rx,),)
    t2 = Thread(target=tx, args=(ser_tx,))
    t1.start()
    t2.start()

Upvotes: 1

Related Questions