import serial # 导入串口包
import time # 导入时间包
from openpyxl import Workbook, load_workbook
ser = serial.Serial("COM3", 9600, timeout=5) # 开启com3口,波特率115200,超时5
ser.flushInput() # 清空缓冲区
# global i
# i = 1
def main():
while True:
count = ser.inWaiting() # 获取串口缓冲区数据
if count != 0:
recv = ser.read(ser.in_waiting).decode("utf-8") # 读出串口数据,数据采用gbk编码
# recv = ser.read(ser.in_waiting).decode("gbk") # 读出串口数据,数据采用gbk编码
# recv = ser.read(ser.in_waiting) # 读出串口数据,数据采用gbk编码
wb = load_workbook("excel_test.xlsx") # 打开 files.xlsx
sheet = wb.get_sheet_by_name("Sheet1") # 设Sheet1可用
sheet.append([recv])
# sheet.append([int(recv)*5/1023])
wb.save("excel_test.xlsx")
print(recv) # 打印一下子
# i += 1
time.sleep(0.1) # 延时0.1秒,免得CPU出问题
if __name__ == '__main__':
main()
网友评论