Reputation: 161
I am trying to send the data calculated on Python to an Arduino but I think below method sends the whole double at one clock cycle. How can I split this into a byte array and send one byte at a time?
ser.write(bytes(round(i * double(self.amplitude), 5)))
ser = serial.Serial(
port=self.outputFile,
baudrate=115200,
parity=serial.PARITY_ODD,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS
)
for i in created_wave:
ser.write(bytes(round(i * double(self.amplitude), 5)))
Upvotes: 0
Views: 602
Reputation: 161
I don't know why but I couldn't access the elements of bytearr using bytearr[j] notation but I accessed them with bytearr[j:j+1] notation. It works this way.
ser = serial.Serial(
port=self.outputFile,
baudrate=115200,
parity=serial.PARITY_ODD,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS
)
for i in created_wave:
bytearr = struct.pack('f', round(i * float(self.amplitude), 5)) #4 byte
for j in range(len(bytearr)):
ser.write(bytearr[j:j + 1])
Upvotes: 0