Parallel logging of serial port

Viewed 270

I have a program, my_prog, that communicates with some HW and is generating output through a UART port. The HW supports two types of communication, ethernet and UART. I am using the ethernet platform to send control commands to the HW using Python's pexpect, and to verify that those commands was received by the HW, but I also need to log the UART ports because they conatin more data. I want to log the UART ports while my_prog is running, and save the log to a file. The logger needs to work in parallel to my_prog so it can capture all the output while my_prog is running. My code goes something like this:

from multiprocessing import Process, Queue
import pexpect


class my_prog_class:
    def __init__():
        self.uart_logger = uart_logger_drv()
        self.process = pexpect.spawn('hw_activation_command')

def my_prog(self,queue):
    # Send some control commands using pexpect and get response using ethernet comm...
    queue.put("stop_logging")

class uart_logger:
    def __init__(self):
        self.core_port = serial.Serial(
        port='/dev/ttyUSB0',
        baudrate=115200,
        parity=serial.PARITY_NONE,
        stopbits=serial.STOPBITS_ONE,
        bytesize=serial.EIGHTBITS,
        timeout=0)

    def logger_control(self,queue):
        """
        Description: Log my_prog
        """
        core_1_file_path = 'core_1_log.txt'
        with open(core_1_file_path, 'a') as core_log:
            while(queue.empty()==True):
                line1 = self.core_port.readline().decode( "utf-8" )    
                core_log.write(re.sub(r'\t', '',line1))

test_instance = my_prog_class()
queue = Queue()
test_process = Process(target=test_instance.my_prog,args=(queue,),name='test_process')
uart_logger_process =  Process(target=test_instance.uart_logger.logger_control,args= 
(queue,),name='uart_logger_process')
test_process.start()
uart_logger_process.start()
test_process.join()
   

My problem is that some of the expected data (I compared to a log file that was generated by manual activation of the HW) is missing, the logger is not printing anything for a small period of time every 3 seconds or so. Any ideas on how to solve this?

0 Answers
Related