python读取bin文件并下发串口

# 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)
# 将字节转换成16进制;
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))# 将16进制转换为字节
if i % 16 == 0:
time.sleep(0.001)
#写每一行等待的时间

i += 1
ser.close()
file.close()
posted @ 2016-04-08 18:07  xiaojiayu  阅读(4626)  评论(0编辑  收藏  举报