Search code examples
pythonraspberry-pipyserialserial-communication

pySerial : Can't read data more than 8 characters


I use pyserial to write and read between two Raspberry pi(s) via UART

Raspberry pi 1 : send data to Raspberry pi 2

    while 1:
        ser=serial.Serial('/dev/ttyAMA0')
        if not ser.isOpen():
            ser.open()
        msg=raw_input('RPi 1 send:')
        ser.write(msg)
        ser.close()

Raspberry pi 2 : receive data from Raspberry pi 1

    while 1:
        ser=serial.Serial('/dev/ttyAMA0' ,timeout=0)
        if not ser.isOpen():
            ser.open()
        data=ser.read(1024)
        if data.__len__()>0:
            print 'RPi 2 receive:',data
        ser.close()

I run both code.

and send data

    RPi 1 send : Hello

and receive data

    RPi 2 receive : Hello

But if RPi 1 send data more than 8 characters

for example

    RPi 1 send : Hello Raspberry pi NO.2

The result is

    RPi 2 receive: Hello Ra
    RPi 2 receive: spberry 
    RPi 2 receive: pi NO.2

This is my problem. Because I want it to receive like this

(RPi 2 receive: Hello Raspberry pi NO.2) #show only one line.

and if send (more 8 char) again, it show in a new line.

What code to join it in one line? or other way to do this? :)


Solution

  • I like to do this

    sender.py

    delim = "\x00"
    ser.write(msg+delim)
    

    reciever.py

    delim = "\x00"
    recvd = "".join(iter(lambda:ser.read(1),delim))
    print recvd