I am trying to get information from the line.

First of all, I had a code to send via serial "t" and I received 3 data lines.

import serial import time port = serial.Serial(port = "/dev/ttyAMA0", baudrate = 115200, timeout = 1) def send_serial(): cde = "t\r" port.write(cde.encode('ascii')) print port.read(9999) 

this is what was displayed in the console

 HDMI IN CH1 1920x1080p60 RGB HDMI OUT 1920x1080p60 RGB HDMI IN CH2 1920x1080p60 RGB 

I want to get every value after CH1 , CH2 and OUT and without RGB .

For starters, I tried using this code:

 import serial import time port = serial.Serial(port = "/dev/ttyAMA0", baudrate = 115200, timeout = 1) def send_serial(): cde = "t\r" port.write(cde.encode('ascii')) f = port.read(9999) for line in f: if line[0:14] == 'HDMI IN CH1': length = len(line) j = line[12: length -1] print j 

But all I get is an empty string.

    1 answer 1

    To get the desired information from the line:

     import re results = re.findall(r'(\d+)x(\d+)p(\d+)', port.read(9999)) 

    for line in f in your code is misleading. In your case, f is a sequence of bytes and, accordingly, the loop iterates byte-by-byte, rather than line by line.

    • and how, with the help of your code, to divide the values ​​of 1920 and so on into different variables? I do not understand him. - Insider
    • @Insider if you don’t know how to work with lists in Python, then print(results) , specify which values ​​in which variables you want to put and publish as a new question (code with serial is not necessary in this case, it’s enough if print(results) output print(results) shown) - jfs
    • Here's a new question - stackoverflow.com/q/566272/19108 - Insider
    • @Insider "not all results" and "I do not understand" are different questions — try to limit yourself to one problem per question. For input in the question, the regular expression returns all results. If different input is possible, then give all the options that you want to support in the question (with or without spaces, with all the numbers, with missing data, etc.) —you should easily fix the regex. - jfs
    • There is no conclusion, the question in my opinion no longer needs to be reduced, unfortunately there is nothing to reduce there - Insider