How does python read bin file and send it to serial port

  • 2021-07-09 08:58:44
  • OfStack

Here's the implementation code


# coding:utf-8
import time, serial
from struct import *
import binascii

file = open('E:\\1.bin', 'rb')
i = 0
while 1:
  c = file.read(1)
  #  Converts bytes to 16 Binary system; 
  ssss = str(binascii.b2a_hex(c))[2:-1]
  print(str(binascii.b2a_hex(c))[2:-1])
  if not c:
    break
  ser = serial.Serial('COM3', 57600, timeout=1)
  ser.write(bytes().fromhex(ssss))#  Will 16 Byte conversion 
  if i % 16 == 0:
    time.sleep(0.001)
  # Write every 1 Time for rows to wait 

  i += 1
  ser.close()
file.close()

Related articles: