1

I'm rather new to the Pi and Python and I could really use your help, so thanks in advance. My project goal is to transmit data, (a jpeg file for instance) from one Pi2 to another one asynchronously. Currently, I'm using the UART pins and a simple Python program, which it's main modulus are:

   #initiating the serial port 
   self.ser = serial.Serial(
        port='/dev/ttyUSB0',
        baudrate= int(self.baudRate),
        parity=serial.PARITY_NONE,
        stopbits=serial.STOPBITS_ONE,
        bytesize=serial.EIGHTBITS,
        rtscts=False,
        dsrdtr=False
    )

    #writing data to the port
    f=open(self.filename, 'rb')
    data=f.read()

    data_length=str(len(data)) #writing the size of the file I wish to send 
    data_length1=str(len(data_length))
    size=data_length1+data_length
    self.ser.write(size) #writing the size of the file I wish to send 

    for b in data:
        self.ser.write(b))

  #reading data from the port
   file=open(self.filename, 'wb')
   first=int(self.ser.read(1))
   file_l=int(self.ser.read(first))  # figuring the size of the file 
   data=self.ser.read(1)
   for i in range (1,file_l):
       data+=self.ser.read(1)
   file.write(data)

The thing is that no matter how high I set the baud rate, I couldn’t get a writing rate of over 380Kbps. When I didn't use the "for" loop on the transmitter module, it seemed that the writing was indeed faster, but then the receiver couldn't read anything.

Any advice on how to reach a higher rate?

8
  • Why write individual bytes? Have you tried self.ser.write(data) ?
    – joan
    Mar 14, 2016 at 9:36
  • thank you for your replay. i indeed tried it, but then the reciver couldn't read anyhing. i'm not sure why.
    – Bar Beker
    Mar 14, 2016 at 9:52
  • Yes, sorry, I misread your question. Doesn't that mean the problem is in the receiving software not the sending software? Is there any reason to be reading a character at a time?
    – joan
    Mar 14, 2016 at 9:55
  • not really, any idea on how to write it better? couldnt find a refernce where the data sent is more then one short string
    – Bar Beker
    Mar 14, 2016 at 9:59
  • If you really need speed don't use Python. There are better ways to send a file.
    – Milliways
    Mar 14, 2016 at 9:59

1 Answer 1

1

I've used this in /boot/config.txt:

init_uart_baud = 1000000
init_uart_clock = 16000000

To do 1 Mbps communication. Note there may be specific rates built into the kernel which it is only willing to work with; I haven't actually tested this for sustained heavy data transfer, mostly just communication via minicom, which does have 1000000 as one of its rates. The device on the other end is (without doubt) currenly hardwired for only 1 Mbps, however, and everything comes through clearly this way. It will not without this, since the default UART clock is 3 MHz, and it needs to be around 16 times the actual baud rate in order for everything to work (if you read up on how UART is generally implemented you'll get why this higher clock is required to keep the communication synced; I think it could be made to work with looser granularity which is why you might be getting 380 Kbps to work).

I haven't tried 2 Mbps yet (because 1 is already overkill for what I'm doing). I have read this is also possible and that the theoretical upper limit is actually 4 Mbps (with the clock set to 64 MHz), but I've also read people saying that they get some corruption at 2 Mbps which they attribute to the pi.

Anyway, that 1 Mbps is definitely achievable. The config.txt doc link is here. I doubt very much there is some limitation of python involved, you just need to pre-configure the hardware.

1
  • 1
    Note that at baudrates of 1Mbps and more the effects of singal reflections become more and more noticeable, so the corruption should probably be attributed to the cables rather than RPi itself. Mar 28, 2017 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.