Python Serial: How to use the read or readline function to read more than 1 character at a time

前端 未结 4 565
谎友^
谎友^ 2020-12-14 08:00

I\'m having trouble to read more than one character using my program, I can\'t seem to figure out what went wrong with my program.

import serial

ser = seria         


        
相关标签:
4条回答
  • 2020-12-14 08:38

    I see a couple of issues.

    First:

    ser.read() is only going to return 1 byte at a time.

    If you specify a count

    ser.read(5)
    

    it will read 5 bytes (less if timeout occurrs before 5 bytes arrive.)

    If you know that your input is always properly terminated with EOL characters, better way is to use

    ser.readline()
    

    That will continue to read characters until an EOL is received.

    Second:

    Even if you get ser.read() or ser.readline() to return multiple bytes, since you are iterating over the return value, you will still be handling it one byte at a time.

    Get rid of the

    for line in ser.read():
    

    and just say:

    line = ser.readline()
    
    0 讨论(0)
  • 2020-12-14 08:50

    I use this small method to read Arduino serial monitor with Python

    import serial
    ser = serial.Serial("COM11", 9600)
    while True:
         cc=str(ser.readline())
         print(cc[2:][:-5])
    
    0 讨论(0)
  • 2020-12-14 08:50

    I was reciving some date from my arduino uno (0-1023 numbers). Using code from 1337holiday, jwygralak67 and some tips from other sources:

    import serial
    import time
    
    ser = serial.Serial(
        port='COM4',\
        baudrate=9600,\
        parity=serial.PARITY_NONE,\
        stopbits=serial.STOPBITS_ONE,\
        bytesize=serial.EIGHTBITS,\
            timeout=0)
    
    print("connected to: " + ser.portstr)
    
    #this will store the line
    seq = []
    count = 1
    
    while True:
        for c in ser.read():
            seq.append(chr(c)) #convert from ANSII
            joined_seq = ''.join(str(v) for v in seq) #Make a string from array
    
            if chr(c) == '\n':
                print("Line " + str(count) + ': ' + joined_seq)
                seq = []
                count += 1
                break
    
    
    ser.close()
    
    0 讨论(0)
  • 2020-12-14 08:54

    Serial sends data 8 bits at a time, that translates to 1 byte and 1 byte means 1 character.

    You need to implement your own method that can read characters into a buffer until some sentinel is reached. The convention is to send a message like 12431\n indicating one line.

    So what you need to do is to implement a buffer that will store X number of characters and as soon as you reach that \n, perform your operation on the line and proceed to read the next line into the buffer.

    Note you will have to take care of buffer overflow cases i.e. when a line is received that is longer than your buffer etc...

    EDIT

    import serial
    
    ser = serial.Serial(
        port='COM5',\
        baudrate=9600,\
        parity=serial.PARITY_NONE,\
        stopbits=serial.STOPBITS_ONE,\
        bytesize=serial.EIGHTBITS,\
            timeout=0)
    
    print("connected to: " + ser.portstr)
    
    #this will store the line
    line = []
    
    while True:
        for c in ser.read():
            line.append(c)
            if c == '\n':
                print("Line: " + ''.join(line))
                line = []
                break
    
    ser.close()
    
    0 讨论(0)
提交回复
热议问题