-1

i'd like to send hex codes through my COM port in order to drive an engine. I have all the hexadecimal codes in a .txt that i open using this method (https://openclassrooms.com/forum/sujet/convertir-fichier-tableur-csv-en-liste-sur-python, in french). This gives me a list of hex codes:

  • Code[0] = "0X53,0X0B,0X01,0X02,0XFF,0X60,0X00,0X00,0X00,0X00,0X00,0XC2,0X45"

The issue is to send that code through my com port. Using ser.write(bytearray([0X53,0X0B,0X01,0X02,0XFF,0X60,0X00,0X00,0X00,0X00,0X00,0XC2,0X45]));

works but i have to input manually the hex code. I'd like to extract the line from the .txt and send it through the COM port Here's my code at the moment

    import serial
import csv
 
def convertisseur(chemin):
    Tableau = []
    Temps = []
    Acc = []

    f = open(chemin)
    csv.reader(f, delimiter=',')

    for row in f:
        Tableau.append(row)
    
    f.close

    n = len(Tableau)
    for i in range(n-1):
        Temps.append(Tableau[i][0])
        Acc.append(Tableau[i][1])
             
    return (Tableau)

def main():
        print("TEST DE PySerial")
        print("----------------")
        print
        ser = serial.Serial('COM3', 115200, timeout=1)
        ser.close()
        ser.open()
        ser.write(bytearray([0X53,0X0B,0X01,0X02,0XFF,0X60,0X00,0X00,0X00,0X00,0X00,0XC2,0X45]));
        
        Code=convertisseur('C:/Users/Tdeba/Desktop/Cod.txt')
        ser.write(Code[0].encode())
        print(Code[0].encode())
        #ser.write(bytearray([0X53,0X0B,0X01,0X02,0XFF,0X60,0X00,0X00,0X00,0X00,0X00,0XC2,0X45]));
        ser.close()
        return   0
    

if __name__ == '__main__':    
    
    main()
Ello
  • 1
  • 1

1 Answers1

0

You can pass generator into bytearray constructor:

bytearray(int(i, 16) for i in s.split(","))
Olvin Roght
  • 7,677
  • 2
  • 16
  • 35