PySerial receive unknown amount of data

Hi I'm working with Python 2 and I'm trying to receive data from an Arduino via Serial Port.

I'm using PySerial to collect the data but I get it chopped and I can't use readline. The size of the array send via serial changes along time.

When I put a very large value to read, it doesn't return a value. I think it keeps waiting until that number of bytes is received.

I would like to know if is a way to get the array complete each time I execute the script.

The code:

class SerialReceiver(serial.Serial):
def __init__(self, portName):
    super(SerialReceiver, self).__init__()

    self.port = portName
    self.baudrate  = 115200
    self.timeout = None
    self.xonxoff = 1

    try:
        self.open()
    except serial.SerialException:
        sys.stderr.write("Could not open serial port %s\n" % (portName))
        sys.exit(1)


if __name__ == '__main__':
    serialReceiver = SerialReceiver('COM3')
    while True:
        sended = raw_input("Pulsar 'O' para recibir data:")
        serialReceiver.write(sended)
        data = serialReceiver.read(2048*2)
        print data

Upvotes: 0

Views: 756

Answers (0)

Related Questions