| | |
| | | |
| | | import utime |
| | | from driver import UART |
| | | import math |
| | | |
| | | # 串口实例化 |
| | | #ttl串口 |
| | | uart0 = UART() |
| | |
| | | #rs232串口 |
| | | uart1 = UART() |
| | | uart1.open("serial2") |
| | | |
| | | if __name__ == '__main__': |
| | | print("Initial start") |
| | | utime.sleep(1) |
| | |
| | | resultSize = 0 |
| | | result = bytearray(50) # RS232串口接收数据 |
| | | resultSize = uart1.read(result) |
| | | print("read data length", resultSize) |
| | | print("read data",result) |
| | | print("read data",result.decode('utf-8')) |
| | | # print("read data length", resultSize) |
| | | # print("read data",result) |
| | | # print("read data",result.decode('utf-8')) |
| | | utime.sleep_ms(1000) |
| | | if(resultSize > 0): |
| | | # TTL串口发送接收到的数据 |
| | | strResult=result.decode('utf-8') |
| | | list= strResult.split(',') |
| | | print(list[2]) |
| | | if(len(list[2])==8): |
| | | strResult=result[0:resultSize].decode('utf-8') |
| | | print("read data",strResult) |
| | | list= strResult.split(' ') |
| | | print("read data------------") |
| | | print(list) |
| | | print('----------') |
| | | lengthList=len(list) |
| | | print('----lengthList------',lengthList) |
| | | numberKg=list[lengthList-1] |
| | | kg=numberKg.strip('kg\r\n') |
| | | print(kg) |
| | | if(numberKg): |
| | | # 转换结果方便计算 |
| | | tzNumber=float(list[2]) |
| | | tzNumber=float(kg)+0.1 |
| | | # 计算体重+0.1kg |
| | | tzNumber=tzNumber+0.1 |
| | | #转回string类型 |
| | | tzStr=str(tzNumber) |
| | | #构建一个8位空字符串 |
| | | lsjg=' ' |
| | | #构建一个临时变量 |
| | | qianStr="" |
| | | #拼凑原来的空字符 |
| | | for x in range(len(list[2])-len(tzStr)): |
| | | qianStr+=" " |
| | | print(tzStr) |
| | | lis=tzStr.split('.') |
| | | #拼接计算后的list[2] |
| | | list[2]=qianStr+tzStr |
| | | list[lengthList-1]=lis[0]+'.'+lis[1][0]+'kg\r\n' |
| | | print('-----666-') |
| | | print(list) |
| | | #转为原始数据字符串 |
| | | list2=",".join(list) |
| | | list2=' '.join(list) |
| | | print('----777------------') |
| | | print(list2) |
| | | print('new result',list2) |
| | | #转成bytearray发送到串口 |
| | | sendByarray=bytearray(list2.encode("utf-8")) |
| | | uart0.write(sendByarray) |
| | | print('sund succer-----') |
| | | utime.sleep_ms(1000) |
| | | #ST,GS, 17.5,kg .encode() |
| | | #ST,GS 60.5kg |
| | | |
| | | |
| | | print("this is usb reply") # usb口作为交互口,利用print打印消息 |