Python Forum
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pyserial char by char io
#1
pyserial is a nifty little module. I'm using it to communicate with 2 serial ports on my ARM eveluation board. It even allows me to send DSR which is essential for me.

However I want to receive from serial port character by character, not wait until a newline char is received as per .readline()
.read() also blocks waiting for a \n

How do I get each character as they arrive?
Reply
#2
If you want each byte as it arrives (in a blocking fashion) you can use read(1).
Reply
#3
read(1) also blocks waiting for \n.

Basically my interrupt handler outputs a single char '#' and toggles my LED. It can only output a single char because with the uart you have to wait until the buffer clears to send the next char, and blocking I/O in interrupt handlers is ... unwise.

So I see all the interrupts that have fired only when I press a key causing all sorts of line terminated I/O

Here's my receive process from the uart

def rxProcess():
    while (True):
        line = tty.read(1)
#        line = tty.read() # tty.readline() # Returns byte array which we coerce into string with str()
        print(line.decode('utf8'), end='') # end= supresses \n normally inherent in print
Reply
#4
(May-15-2018, 08:54 AM)fcagney Wrote: read(1) also blocks waiting for \n.
Are you sure about that? That behavior doesn't make any sense, and isn't in the docs. I don't have hardware handy to test it but if I were you I'd double check that the thing sending the data over serial isn't doing something funny like never sending the byte unless there's also a newline following it.

You might also want to try setting parity=serial.PARITY_EVEN when you create the Serial object, like at https://pythonhosted.org/pyserial/shortintro.html
Reply
#5
(May-15-2018, 08:54 AM)fcagney Wrote: read(1) also blocks waiting for \n.
read(1) blocks until one byte is in the buffer. It doesn't matter which char.

I use pyserial to read data from a radar transceiver. If there is a bug in pyserial,
I won't get usable data. Pyserial works fine with ftdi.

Maybe it's a hardware issue? Have you tried another serial port?
Maybe you try out a usb2tty from ftdi. They have also a python module,
which has a better performance and gives you the ability for bit banging.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Sad How to split a String from Text Input into 40 char chunks? lastyle 7 1,054 Aug-01-2023, 09:36 AM
Last Post: Pedroski55
  How to replace on char with another in a string? korenron 3 2,292 Dec-03-2020, 07:37 AM
Last Post: korenron
  How to remove char from string?? ridgerunnersjw 2 2,481 Sep-30-2020, 03:49 PM
Last Post: ridgerunnersjw
  Sum char word dictionary RavCOder 3 2,289 Nov-08-2019, 01:32 PM
Last Post: perfringo
  The use of escape char \ hishamzero1 2 2,339 Aug-12-2019, 10:20 PM
Last Post: hishamzero1
  First non-repeating char and some errors. blackknite 1 2,236 Jan-06-2019, 02:19 PM
Last Post: stullis
  json.dumps save some unicode chars as code, not char buran 1 2,877 Aug-02-2018, 04:02 PM
Last Post: buran
  how to give your data column names based on char position whyme 7 4,351 Oct-27-2017, 06:45 PM
Last Post: heiner55
  Regex: Remove all match plus one char before all Alfalfa 34 22,024 Mar-27-2017, 03:41 AM
Last Post: nilamo
  Probelm in displaying char. indexwise ShailendraRaghunathPhadke 5 4,513 Feb-09-2017, 09:00 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020