Hyun Ho An
Hyun Ho An

Reputation: 1

Serial communication between raspberry pi pico and PC for csv files

I'm trying to establish a serial communication link to send a CSV file from a Raspberry Pi Pico to a PC. I'm facing a problem where the PC can't read all the binary text.

For example:

OUT: b', 4626, 4626\r\n2024/07/30 15:11:02 698ms, 56, 4626, 4626, 4626, 4626\r\n\n\r\

IN: , 4626, 4626

Additionally, there is an issue with the CSV file not being saved properly. When I try to open the file, it gets corrupted.

I've been trying to solve this problem all day, but I couldn't find an answer.

Is there anyone who can help me?

Here is my code.

[PC]

from Serial_com.Serial_com import *
from txt_file.file_select import *
import csv , re

def receive_file(output_file):
    file_size = 0
    bytes_received = 0
    file_data = b""

    while True:
        line = serial_in(1000) 
        if line.startswith("FILESIZE:"):
            file_size = int(line.split(':')[1])
            print(f"File size: {file_size} bytes")

        elif line.startswith("PROGRESS:"):
            progress = int(line.split(":")[1])
            print(f"Progress: {progress}%")

        elif line == "TRANSFER_COMPLETE":
            print("File transfer complete.")
            break

        elif line == "TRANSFER_ERROR":
            print("File transfer error.")
            break

        elif line == "DOWNLOAD":
            value = serial_in_download(1000)
            file_data += value
            bytes_received += len(value)

    
    with open(output_file, 'w') as csv_file:
        writer = csv.writer(csv_file)

        writer.writerow(file_data)
    print(f"Received {bytes_received} bytes.")

[Rasberry pi pico]

from Serial import *
from sd_method import *
import os

def send_file3(file_path):
    try:
        print('start')
        mount_sdcard() 
        with open(file_path, 'rb') as file:
            file_size = os.stat(file_path)[6]
            bytes_sent = 0
            
            serial_out(f'FILESIZE:{file_size}', 0.5)
            
            while True:
                chunk = file.read(1024)
                if not chunk:
                    break
                
                serial_out(f'Download',0.5)
                serial_out_download(chunk, 0.5)
                bytes_sent += len(chunk)
                progress = int((bytes_sent / file_size) * 100)
                
                
                serial_out(f'PROGRESS:{progress}', 1)
                
            serial_out("TRANSFER_COMPLETE", 0.4)
        
        unmount_sdcard()
        
    except Exception as e:
        print(f"Error: {e}")
        serial_out("TRANSFER_ERROR", 0.4)


file_name = '/sd/2024Y07M30D_14Hour38Min.csv'
send_file3(file_name)

[PC serial communocation code]

def set_serial_port():
    relative_path = os.path.join('GUI', 'txt_file', 'Serial_port.cfg')
    serial_port = Read_Value(relative_path, 'serial_port')

    try:
        ser = serial.Serial(serial_port, 115200, timeout=1)
    except serial.SerialException as e:
        show_error_message(f"Error opening serial port.\nPlease try again.")
        new_port = get_user_input("Enter a valid serial port:")
        if new_port:
            Exchage_value(relative_path, 'serial_port', new_port)
            sys.exit()
        else:
            sys.exit()

    return ser

def serial_out(wait_time, value):
    ser = set_serial_port()
    time.sleep(wait_time)
    ser.write((value + '\n').encode('utf-8'))
    ser.flush()
    print(f"Out: {value}")
    return value


        
def serial_in(wait_time=100000):
    ser = set_serial_port()
    start_time = time.time()
    buffer = ""
    while True:
        if ser.in_waiting > 0 or time.time() - start_time > wait_time:
            while ser.in_waiting > 0:
                buffer += ser.readline().decode('utf-8')
                if '\n' in buffer:
                    value = buffer.strip()
                    print("in: {}".format(value))
                    ser.flushOutput()
                    return value
        if time.time() - start_time > wait_time:
            print('no communication')
            return
            
def serial_in_download(wait_time=10000):
    ser = set_serial_port()
    start_time = time.time()
    buffer = b""
    while True:
        if ser.in_waiting > 0 or time.time() - start_time > wait_time:
            while ser.in_waiting > 0:
                buffer += ser.readall()
                print(buffer)
                ser.flushOutput()
                return buffer
        if time.time() - start_time > wait_time:
            print('no communication')
            return

I repeatedly tried encoding and decoding, but I couldn't properly load the file. I also checked continuously for any issues with the buffer output, but that wasn't the case.

Upvotes: 0

Views: 29

Answers (0)

Related Questions