LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   read serial port and display data in tkinter gui (https://www.linuxquestions.org/questions/programming-9/read-serial-port-and-display-data-in-tkinter-gui-4175662647/)

RonHof 10-16-2019 04:42 PM

read serial port and display data in tkinter gui
 
This is related to a couple of my previous questions but different enough to use a new thread. I'm trying to read the serial out put of a legacy piece of hardware and display it in a tkinter GUI. I've made a lot of progress with all your help but this current problem has me baffled. The hardware is basically a timer. I've opened the serial port as ttyUSB0)
Code:

ser = serial.Serial(
        port='/dev/ttyUSB0',
        baudrate = 4800,
        parity = serial.PARITY_NONE,
        stopbits = serial.STOPBITS_TWO,
        bytesize = serial.EIGHTBITS,
        timeout=1
        )

I'm reading the data stream with
Code:

Str = ser.readline(8)
    print (Str)#debug statement
    Str = Str.decode('cp1250','strict').replace("60","GO").strip()
    print ("decoded string =", Str)#debug statement
    print ("")#debug statement

the number is 7 characters long and the number of leading spaces changes depending on the value. The debug print looks like this
Quote:

b' 0.30\r'
decoded string = 0.30

b' 0.30\r'
decoded string = 0.30

b' 0.30\r'
decoded string = 0.30
The GUI is happy and displays the text.
Now the problem:

When the GO button is pressed an integer 60 is passed but the number of spaces before and after changes with each word. This was done to cause the integer 60 to move down a digital display board. The problem is that this causes the 60 to jump around on the GUI display and when the number wraps around the EOL marker the GUI splits the number into two lines. Then somehow the read gets out of sync with the word spacing and even the floating point seconds nomber is moved or line wraped. The debug print looks like this:
Code:

b'    60\r '
decoded string = GO

b'  60\r  '
decoded string = GO

b'  60\r  '
decoded string = GO

b' 60\r    '
decoded string = GO

b'60\r    6'
decoded string = GO
    6

b'0\r    60'
decoded string = 0
    GO

I tried stripping the spaces out and that helped but it still wraps the integer 60 and the decimal seconds when the EOL is not the 8th character read. What am I missing in reading this serial data? Any thoughts? Thanks

NevemTeve 10-17-2019 02:41 AM

Don't read fix number of bytes, read lines. In some other topic you have already found ser.eol= '\r' (or similar), now use that.

RonHof 10-17-2019 03:28 PM

Thanks!
I also found that readline.replace(b"\r",b"\r\n") works


All times are GMT -5. The time now is 07:57 PM.