'Store serial port data in txt

I am trying to make a txt file to store the data that im receiving from USB port, the problem that i have is that the data i send truth bluetooth from my phone to usb port(I use nordic NRF52840), ble uart conexion, when i received the data it considers it as a whole, if i send "hello" and then "how are you?" I receive in txt file: "hellohow are you?" all together as 1 message, it consider everything as 1 string, it doesnt matter if i write \n somewhere. here is my code:

import serial
archi1=open("datos7.txt",'w')
ser=serial.Serial('COM17',baudrate = 115200, timeout=15)

try:
    ser.isOpen()
    print ("Reading open.")
except:
    print ("Error.")
    exit()

while True:
    data = ser.read(500)
    data = data.replace(b'\r',b'')
    data = data.replace(b'\x1b',b'')
    dataASCII = data.decode("ascii")
    archi1.write('Envio de datos empezado.\n')
    print(data, end='')
    if archi1.write('Los datos obtenidos son:|%s|' % (dataASCII)):
        break
archi1.write('\nEnvio de datos terminado.')
archi1.close()


Solution 1:[1]

Try to delete the following line:

data = data.replace(b'\r',b'')

You delete the \r character, which makes the new lines.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JANO