1

1.Transmitter side code: A)Recording audio into fifo file(no need to create fifo file separately arecord command will create fifo file):

import os
os.system('arecord -D plughw:1,0 -d 0 --rate=19200 audio.fifo')

B)Reading fifo and writing into UART:

import time
import serial
import os
import select
ser=serial.Serial('/dev/ttyS0',19200)
def do_work(data):
    ser.write(data)
with open("audio.fifo") as fifo:
    while True:
        select.select([fifo],[],[fifo])
        data=fifo.read(size=512)
        do_work(data)

2.Receiver side code: A)Making a fifo file:

import os
os.system("mkfifo audio.fifo")

B)Playing that fifo file:

import os
os.system("aplay --format=S13_LE --rate=19200 audio.fifo")

C)Reading UART and writing into already created fifo file:

import os
import serial
import select 
import time
while True:
    with serial.Serial() as ser:
        ser.baudrate=19200
        ser.port='/dev/ttyS0'
        ser.open()
        data=ser.read(size=512)
        with open('audio.fifo', 'wb') as fifo:
        fifo.write(data)

My aim is to transmit and receive real time audio data through UART port of raspberry pi 3b. These are the codes I am using.Note that each code is run in separate terminal.Problem is at receiver side, it is only creating audio.fifo file at receiver side not writing anything in it.Please give some suggestions.

  • Try sending a "Hello world" through your FIFO. Unless that works, it's likely a general coding issue than a Pi-related issue. – Dmitry Grigoryev Jan 27 at 9:30
2

On the first RPi :

In a first terminal : (create FIFO)

mkfifo /tmp/audio.fifo.pi1

In a second terminal : (Write datas into FIFO)

arecord /tmp/audio.fifo.pi1

readfifo-writeUART.py : pyserial short-intro

#!/usr/bin/python
# readfifo-writeUART.py

import serial

def do_work(data):
    with serial.Serial() as ser:
        ser.baudrate = 19200
        ser.port = '/dev/ttyAMA0'
        ser.open()
        ser.write(data)

with open("/tmp/audio.fifo.pi1") as fifo:
     select.select([fifo],[],[fifo]) 
     data = fifo.read(512)
     repr(data)
     do_work(data)

In a third terminal : (read FIFO datas and send it trought UART)

python readfifo-writeUART.py 

Now on the second RPi you must do the invert : read UART data, write it into a FIFO file, read the FIFO file with aplay.

mkfifo /tmp/audio.fifo.pi2

...

aplay /tmp/audio.fifo.pi2

...

readUART-writefifo.py

#!/usr/bin/python
# readUART-writefifo.py

import serial
with serial.Serial() as ser:
    ser.baudrate = 19200
    ser.port = '/dev/ttyAMA0'
    ser.open()
    data = ser.read(size=512)

    with open('/tmp/audio.fifo.pi2', 'wb') as fifo:
        fifo.write(data)

...

python readUART-writefifo.py

NOTE : All of the code provided is a pseudcode that has not been tested.

| improve this answer | |
  • Thanks ma'am.But if u could plz write "readUART-writefifo.py" file in your answer because I am getting problem in writing fifo file while receiving data through UART . – kunal Jan 15 at 12:25
  • @kunal: Take a look here this, all my code is not tested , see me edited answer – Ephemeral Jan 15 at 14:16
  • Ma'am it is not working. I tried with the same way u have written in answer.When I start readUART-wriefifo.py file it is not writing anything in the already created file "/tmp/audio.fifo.pi2".file.According to your code it should read 512 byte and then it should write it into file but program(readUART-wriefifo.py) is continuously running only. – kunal Jan 17 at 11:43
  • .Ma'am it is not working. I tried with the same way u have written in answer.When I start readUART-wriefifo.py file it is not writing anything in the already created file "/tmp/audio.fifo.pi2".file.According to your code it should read 512 byte and then it should write it into file but program(readUART-wriefifo.py) is continuously running only. – kunal Jan 17 at 13:17
  • @kunal, Please add your current codes . I hope you understood that this was a sample code and that it had to be adapted to your needs. – Ephemeral Jan 17 at 17:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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