主界面功能按键:
1.打开端口按钮:
uart1 = UART(UART.UART2,9600, 8, parity, 1, 0) #构造串口对象
def OpenEvent(evt):global uart1openBtn = evt.get_code()if openBtn == lv.EVENT.CLICKED:if labelOpenBtn.get_text() == "打开端口":uart1 = UART(UART.UART2,int(baudRate.get_text()), int(dataBits.get_text()), parity, int(stopBits.get_text()), 0) labelOpenBtn.set_text("关闭端口") else:uart1.close()labelOpenBtn.set_text("打开端口") openBtn = lv.btn(mainInterface)
openBtn.align(lv.ALIGN.TOP_RIGHT,-20,30)
labelOpenBtn = lv.label(openBtn)
labelOpenBtn.set_text('打开端口')openBtn.add_event_cb(OpenEvent,lv.EVENT.ALL,None)
2.历史记录按钮
def event_handler2(evt):code = evt.get_code()if code == lv.EVENT.VALUE_CHANGED:source = evt.get_current_target()date = lv.calendar_date_t()if source.get_pressed_date(date) == lv.RES.OK: calendar.set_today_date(date.year, date.month, date.day)chart = lv.chart(historyInterface)chart.set_size(200, 150)chart.align(lv.ALIGN.RIGHT_MID,0,0)chart.set_type(lv.chart.TYPE.LINE) # Show lines and points too# Add two data seriesser1 = chart.add_series(lv.palette_main(lv.PALETTE.RED), lv.chart.AXIS.PRIMARY_Y)ser2 = chart.add_series(lv.palette_main(lv.PALETTE.GREEN), lv.chart.AXIS.SECONDARY_Y)print(ser2)# Set next points on ser1chart.set_next_value(ser1,10)chart.set_next_value(ser1,10)chart.set_next_value(ser1,10)chart.set_next_value(ser1,10)chart.set_next_value(ser1,10)chart.set_next_value(ser1,10)chart.set_next_value(ser1,10)chart.set_next_value(ser1,30)chart.set_next_value(ser1,70)chart.set_next_value(ser1,90)# Directly set points on 'ser2'ser2.y_points = [90, 70, 65, 65, 65, 65, 65, 65, 65, 65]chart.refresh() # Required after direct setdef historyEvent(evt):code = evt.get_code()if code == lv.EVENT.CLICKED:lv.scr_load(historyInterface)historyBtn = lv.btn(mainInterface) #历史记录按钮
historyBtn.align(lv.ALIGN.BOTTOM_LEFT,20,-40)
labelHistoryBtn = lv.label(historyBtn)
labelHistoryBtn.set_text('历史记录')
historyBtn.add_event_cb(historyEvent, lv.EVENT.ALL, None)
3.获取数据按钮
def getData(evt):global uart1getDataBtn = evt.get_code() if getDataBtn == lv.EVENT.CLICKED:uart1.write("\x03\x03\x00\x40\x00\x04""44\x3F")time.sleep(1)arr = uart1.read(13)for i in arr:print(((hex(int(i))).upper()[2:]),end=' ')zhuodu = int(str((hex(int(arr[3]))).upper()[2:])+str((hex(int(arr[4]))).upper()[2:])+str((hex(int(arr[5]))).upper()[2:])+str((hex(int(arr[6]))).upper()[2:]),16)zengyi = int(str((hex(int(arr[7]))).upper()[2:])+str((hex(int(arr[8]))).upper()[2:])+str((hex(int(arr[9]))).upper()[2:])+str((hex(int(arr[10]))).upper()[2:]),16)print(zhuodu)label.set_text(str(zhuodu/1000))averageTurbidityValue.set_value(int(zhuodu/10))print(zengyi)label3.set_text(str(zengyi))averageGainValue.set_value(int(zengyi/1000))time.sleep(1)averageTurbidityValue = lv.arc(mainInterface) #平均浊度值弧
averageTurbidityValue.set_end_angle(200)
averageTurbidityValue.set_size(150,150)
averageTurbidityValue.align(lv.ALIGN.CENTER,-120,0)
averageTurbidityValue.add_event_cb(event_cb, lv.EVENT.VALUE_CHANGED, None)
averageTurbidityValue.set_range(0,3000)
label = lv.label(averageTurbidityValue)label2 = lv.label(mainInterface)
label2.align(lv.ALIGN.CENTER,-120,0)
label2.set_text("平均浊度值")averageGainValue = lv.arc(mainInterface) #平均增益值弧
averageGainValue.set_size(150,150)
averageGainValue.align(lv.ALIGN.CENTER,120,0)
averageGainValue.add_event_cb(event_cb1, lv.EVENT.VALUE_CHANGED, None)
averageGainValue.set_range(0,200000)
label3 = lv.label(averageGainValue)getDataBtn = lv.btn(mainInterface) #获取数据按钮
getDataBtn.align(lv.ALIGN.BOTTOM_MID,0,-40)
getDataBtnLabel = lv.label(getDataBtn)
getDataBtnLabel.set_text('获取数据')
getDataBtn.add_event_cb(getData,lv.EVENT.ALL, None)