Delay from serial port using Python

lordhog

I am trying to write a small Python script to read data from a serial port and simply display it to the screen. It seems to be working, but there seems to be a delay to when I see data displayed on the screen.

import serial
import sys

SerialGF = serial.Serial(port='/dev/ttyAMA0', baudrate=115200, parity='N', stopbits=1, xonxoff=0, rtscts=0, timeout=0)

def main():
    # openSerial_GF()
    print SerialGF.isOpen()
    SerialGF.flush()
    SerialGF.flushInput()
    SerialGF.flushOutput()
    try:
        while True:
            readSerial_GF()
    except KeyboardInterrupt:
        pass
    SerialGF.close()
    print SerialGF.isOpen()

# def openSerial_GF():
#     global SerialGF = serial.Serial(port='/dev/ttyAMA0', baudrate=115200, parity='N', stopbits=1, xonxoff=0, rtscts=0, timeout=0)

def readSerial_GF():
    s = SerialGF.read(SerialGF.inWaiting())
    sys.stdout.write(s)

if __name__ == "__main__":
    main()

The data on serial port is a stream of dash characters, '-', until some event occurs. What I am seeing a delay when the data is displayed to the screen. What is odd is that 1024 characters are displayed at a time. I have set the timeout to zero (0) so it should return straight away, but it isn't. Does anyone have any ideas why there is a delay?

Thanks, Mark

abarnert

This is almost certainly a buffering issue. The short answer is, if you want output to show up immediately, do sys.stdout.flush() after each sys.stdout.write(…).

On most platforms, by default,* the stdio streams end up line buffered. This means that anything written is buffered up until you either write a newline, or fill up the buffer inside stdio, which is usually some nice round length like 1024 bytes.

Python 2.x** mostly leaves buffering up to the C stdio library, so for full details, you want to look up your platform's fopen and possibly fwrite implementations. The docs for open explain the options Python gives you for controlling the buffering, and the command line and environment docs explain how you can influence the way the stdio streams are opened. But really, rather than forcing everyone to run your program with -u, it's better to just explicitly flush when you want to make sure output appears.

* If you open the files in binary mode—which is what -u and PYTHONUNBUFFERED control—you get unbuffered output. If you open the files in text mode and isatty() is true or not available, you usually get line-buffered mode. Otherwise, you usually get "fully-buffered" mode, meaning there's a fixed-size buffer and writes happen when that buffer is filled.

** Note that this is all Python 2.x-specific; 3.x has its own file objects that work directly on top of native file descriptors/handles, instead of using C stdio.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Delay from serial port using Python

From Dev

Putting delay in read and write from serial port

From Dev

Send file over serial port from Python

From Dev

Python: Writing to and Reading from serial port

From Dev

How to make a serial port sniffer sniffing physical port using a python

From Dev

Delay in serial communication using jssc

From Dev

Reading from serial port asynchronously using Async await method

From Dev

Read serial port from client(Browser) using Applet + Javascript

From Dev

Using Pyserial to plot time against voltage from an Arduino Serial Port

From Dev

C# , receiving data from serial port using readto()

From Dev

Sending a character to the Arduino serial port using Python's Pyserial

From Dev

login using serial port in Linux

From Dev

Read double from serial port

From Dev

Data from USB ~ Serial Port

From Dev

Read double from serial port

From Dev

Reading from serial port in C

From Dev

Receiving data from Serial Port

From Dev

Read from serial port in Windows

From Dev

code not reading from serial port

From Dev

python called from powershell loop using delay

From Dev

Why do we need to delay when sending char to serial port?

From Dev

Serial port communication from ASP.NET application using WCF Service

From Dev

Serial port communication from ASP.NET application using WCF Service

From Dev

Using Livecharts Constant Changes example to plot points based on time, and readings from a serial port?

From Dev

I cant read the data correctly, I receive from serial port using c#

From Dev

Using DMA to access High Speed Serial Port

From Dev

Reading and writing to a serial port using shell and Java

From Dev

Can't connect to SERIAL PORT using Putty

From Dev

Using DartLang to comunicate via Serial port

Related Related

  1. 1

    Delay from serial port using Python

  2. 2

    Putting delay in read and write from serial port

  3. 3

    Send file over serial port from Python

  4. 4

    Python: Writing to and Reading from serial port

  5. 5

    How to make a serial port sniffer sniffing physical port using a python

  6. 6

    Delay in serial communication using jssc

  7. 7

    Reading from serial port asynchronously using Async await method

  8. 8

    Read serial port from client(Browser) using Applet + Javascript

  9. 9

    Using Pyserial to plot time against voltage from an Arduino Serial Port

  10. 10

    C# , receiving data from serial port using readto()

  11. 11

    Sending a character to the Arduino serial port using Python's Pyserial

  12. 12

    login using serial port in Linux

  13. 13

    Read double from serial port

  14. 14

    Data from USB ~ Serial Port

  15. 15

    Read double from serial port

  16. 16

    Reading from serial port in C

  17. 17

    Receiving data from Serial Port

  18. 18

    Read from serial port in Windows

  19. 19

    code not reading from serial port

  20. 20

    python called from powershell loop using delay

  21. 21

    Why do we need to delay when sending char to serial port?

  22. 22

    Serial port communication from ASP.NET application using WCF Service

  23. 23

    Serial port communication from ASP.NET application using WCF Service

  24. 24

    Using Livecharts Constant Changes example to plot points based on time, and readings from a serial port?

  25. 25

    I cant read the data correctly, I receive from serial port using c#

  26. 26

    Using DMA to access High Speed Serial Port

  27. 27

    Reading and writing to a serial port using shell and Java

  28. 28

    Can't connect to SERIAL PORT using Putty

  29. 29

    Using DartLang to comunicate via Serial port

HotTag

Archive