Python : Interruption between sending chunks via uart

576 views Asked by At

I'm sending some data via uart. I take single file and split it into 5120byte parts(chunks). Between sending this chunks there is interruption (probably based on baudrate value) I need to catch this interruption and switch from sending data to receiving data. Is this possible this way ?

example code to send data :

ser = serial.Serial(
    port='/dev/ttyS0',
    baudrate=115200)

def send():
    with open("path_to_my_file_to_send", "rb") as fh:
        while True:
            chunk = fh.read(5120)
            if not chunk: break
            ser.write(chunk)

def reveice():
global receivedData
receivedData = ""
time.sleep(0.001)
while ser.inWaiting() > 0:
    receivedData= ser.read(1)
while True:
    if len(receivedData ) == 1:
        print ("received data")
        break
    else:
        print("no received data")
        break
2

There are 2 answers

4
Shivam Chawla On

So what you can do is send data from your send then wait for the recieve to send an acknowledgement. This way semd will know its data is successfully recieved and it can send new data then. If not than it can re transmit it or if it recieves the interrupt msg it can stop. At the send side u can recieve and then send acknowledgement. This acknowledgement could be length of the msgs which can be verified at the send side.

ser = serial.Serial(
port='/dev/ttyS0',
baudrate=115200)

def send():
 with open("path_to_my_file_to_send", "rb") as fh:
  while True:
   chunk = fh.read(5120)
    if not chunk: break
    ser.write(chunk)
    #for interupt just send#
    ser.write(b'interupt')

def reveice():
 global receivedData
 receivedData = ""
 time.sleep(0.001)
 while ser.inWaiting() > 0:
  receivedData= ser.read(1)
  if b'interupt' in receivedData:
   #go to recv mode

 while True:
  if len(receivedData) == 1:
   print ("received data")
   break
 else:
  print("no received data")
  break
0
KyluAce On

Ok I found something which changes look on this problem. BaudRate only tells me how long single chunk is send. But anyway there was interruption after each chunk but there are caused to big set value for baudrate (values more than 1000000) can provides gaps during transfering files via uart. This gaps if I increase baudrate a little bit was not regular, but if we goes to 4000000 (and we do other operations during transfering file) gaps appear after each chunk.