iRisc / Scan_in / Python / send2arduino.py
send2arduino.py
Raw
import serial
import time

# Set up the serial connection to the Arduino (adjust the port as needed)
ser = serial.Serial('/dev/tty.usbmodem101', 1000000)  # Use your specific port (e.g., /dev/ttyUSB0 or COM3)
time.sleep(2)  # Wait for the connection to establish

# Count the total number of lines in the file to calculate percentage later
file_path = 'scan_bitstream.csv'

# Open the file that contains the GPIO patterns
with open(file_path, 'r') as file:
    binary_chunks = []

    for i, line in enumerate(file, start=1):  # Use enumerate to track line number

        binary_str = line.strip()
        binary_chunks.append(binary_str)
        if len(binary_chunks) == 4:
            # Combine the four 4-bit binary strings into one 16-bit binary string
            combined_binary = ''.join(binary_chunks)
            # print(combined_binary)/
            
            # Convert the 16-bit binary string to an integer
            combined_int = int(combined_binary, 2)
            
            # Send the 16-bit integer as two bytes over serial
            ser.write(combined_int.to_bytes(2, byteorder='big'))
            # print(combined_int.to_bytes(2, byteorder='big'))

            # print(f"Binary to send: {combined_binary} (Hex: {hex(combined_int)})")
            # print(i)
            # time.sleep(0.01)

            binary_chunks.clear()

        # Optionally adjust the timing between transmissions
        time.sleep(4e-6)  # Small delay (adjust as needed)

# Close the serial connection once done
ser.close()