Skip to content

Commit fc07a16

Browse files
authored
Merge pull request #93 from futre123/main
Update serial port example
2 parents d3f264a + d0062d1 commit fc07a16

File tree

2 files changed

+88
-124
lines changed

2 files changed

+88
-124
lines changed

en-us/api/QuecPythonClasslib.md

Lines changed: 43 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -5537,7 +5537,7 @@ from machine import UART
55375537
The following two global variables are required. Users can modify the values of the following two global variables according to the actual projects.
55385538
'''
55395539
PROJECT_NAME = "QuecPython_UART_example"
5540-
PROJECT_VERSION = "1.0.0"
5540+
PROJECT_VERSION = "1.0.1"
55415541

55425542
'''
55435543
* Parameter1: Port
@@ -5558,74 +5558,56 @@ PROJECT_VERSION = "1.0.0"
55585558
log.basicConfig(level=log.INFO)
55595559
uart_log = log.getLogger("UART")
55605560

5561-
state = 5
5562-
5563-
5564-
def uartWrite():
5565-
count = 10
5566-
# Configure UART
5567-
uart = UART(UART.UART2, 115200, 8, 0, 1, 0)
5568-
while count:
5569-
write_msg = "Hello count={}".format(count)
5570-
# Send data
5571-
uart.write(write_msg)
5572-
uart_log.info("Write msg :{}".format(write_msg))
5573-
utime.sleep(1)
5574-
count -= 1
5575-
uart_log.info("uartWrite end!")
5561+
class Example_uart(object):
5562+
def __init__(self, no=UART.UART2, bate=115200, data_bits=8, parity=0, stop_bits=1, flow_control=0):
5563+
self.uart = UART(no, bate, data_bits, parity, stop_bits, flow_control)
5564+
self.uart.set_callback(self.callback)
55765565

55775566

5578-
def UartRead():
5579-
global state
5580-
uart = UART(UART.UART2, 115200, 8, 0, 1, 0)
5581-
while 1:
5582-
# It returns whether the data length is readable
5583-
msgLen = uart.any()
5584-
# Read when there is data
5585-
if msgLen:
5586-
msg = uart.read(msgLen)
5587-
# The initial data is byte type, and encode it
5588-
utf8_msg = msg.decode()
5589-
# str
5590-
uart_log.info("UartRead msg: {}".format(utf8_msg))
5591-
state -= 1
5592-
if state == 0:
5593-
break
5594-
else:
5595-
continue
5567+
def callback(self, para):
5568+
uart_log.info("call para:{}".format(para))
5569+
if(0 == para[0]):
5570+
self.uartRead(para[2])
55965571

5572+
5573+
def uartWrite(self, msg):
5574+
uart_log.info("write msg:{}".format(msg))
5575+
self.uart.write(msg)
5576+
5577+
def uartRead(self, len):
5578+
msg = self.uart.read(len)
5579+
utf8_msg = msg.decode()
5580+
uart_log.info("UartRead msg: {}".format(utf8_msg))
5581+
return utf8_msg
5582+
5583+
def uartWrite_test(self):
5584+
for i in range(10):
5585+
write_msg = "Hello count={}".format(i)
5586+
self.uartWrite(write_msg)
5587+
utime.sleep(1)
55975588

5589+
if __name__ == "__main__":
5590+
uart_test = Example_uart()
5591+
uart_test.uartWrite_test()
5592+
55985593

5599-
def run():
5600-
# Create a thread to listen to received UARt messages
5601-
_thread.start_new_thread(UartRead, ())
5594+
# 运行结果示例
5595+
'''
5596+
INFO:UART:write msg:Hello count=0
5597+
INFO:UART:write msg:Hello count=1
5598+
INFO:UART:write msg:Hello count=2
5599+
INFO:UART:write msg:Hello count=3
5600+
INFO:UART:write msg:Hello count=4
5601+
INFO:UART:write msg:Hello count=5
5602+
INFO:UART:write msg:Hello count=6
5603+
INFO:UART:write msg:Hello count=7
5604+
INFO:UART:write msg:Hello count=8
5605+
INFO:UART:write msg:Hello count=9
56025606
5607+
INFO:UART:call para:[0, 2, 15]
5608+
INFO:UART:UartRead msg: my name is XXX
56035609
5604-
if __name__ == "__main__":
5605-
uartWrite()
5606-
run()
5607-
while 1:
5608-
if state:
5609-
pass
5610-
else:
5611-
break
56125610
5613-
# Example of running results
5614-
'''
5615-
INFO:UART:Write msg :Hello count=8
5616-
INFO:UART:Write msg :Hello count=7
5617-
INFO:UART:Write msg :Hello count=6
5618-
INFO:UART:Write msg :Hello count=5
5619-
INFO:UART:Write msg :Hello count=4
5620-
INFO:UART:Write msg :Hello count=3
5621-
INFO:UART:Write msg :Hello count=2
5622-
INFO:UART:Write msg :Hello count=1
5623-
INFO:UART:uartWrite end!
5624-
INFO:UART:UartRead msg: read msg 1
5625-
5626-
INFO:UART:UartRead msg: read msg 2
5627-
5628-
INFO:UART:UartRead msg: read msg 3
56295611
'''
56305612

56315613
```

zh-cn/api/QuecPythonClasslib.md

Lines changed: 45 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -6011,7 +6011,7 @@ from machine import UART
60116011
下面两个全局变量是必须有的,用户可以根据自己的实际项目修改下面两个全局变量的值
60126012
'''
60136013
PROJECT_NAME = "QuecPython_UART_example"
6014-
PROJECT_VERSION = "1.0.0"
6014+
PROJECT_VERSION = "1.0.1"
60156015

60166016
'''
60176017
* 参数1:端口
@@ -6021,9 +6021,9 @@ PROJECT_VERSION = "1.0.0"
60216021
UART2 – MAIN PORT
60226022
UART3 – USB CDC PORT
60236023
* 参数2:波特率
6024-
* 参数3:data bits (5 ~ 8)
6024+
* 参数3:data bits (5~8)
60256025
* 参数4:Parity (0:NONE 1:EVEN 2:ODD)
6026-
* 参数5:stop bits (1 ~ 2)
6026+
* 参数5:stop bits (1~2)
60276027
* 参数6:flow control (0: FC_NONE 1:FC_HW)
60286028
'''
60296029

@@ -6032,74 +6032,56 @@ PROJECT_VERSION = "1.0.0"
60326032
log.basicConfig(level=log.INFO)
60336033
uart_log = log.getLogger("UART")
60346034

6035-
state = 5
6036-
6037-
6038-
def uartWrite():
6039-
count = 10
6040-
# 配置uart
6041-
uart = UART(UART.UART2, 115200, 8, 0, 1, 0)
6042-
while count:
6043-
write_msg = "Hello count={}".format(count)
6044-
# 发送数据
6045-
uart.write(write_msg)
6046-
uart_log.info("Write msg :{}".format(write_msg))
6047-
utime.sleep(1)
6048-
count -= 1
6049-
uart_log.info("uartWrite end!")
6050-
6051-
6052-
def UartRead():
6053-
global state
6054-
uart = UART(UART.UART2, 115200, 8, 0, 1, 0)
6055-
while 1:
6056-
# 返回是否有可读取的数据长度
6057-
msgLen = uart.any()
6058-
# 当有数据时进行读取
6059-
if msgLen:
6060-
msg = uart.read(msgLen)
6061-
# 初始数据是字节类型(bytes),将字节类型数据进行编码
6062-
utf8_msg = msg.decode()
6063-
# str
6064-
uart_log.info("UartRead msg: {}".format(utf8_msg))
6065-
state -= 1
6066-
if state == 0:
6067-
break
6068-
else:
6069-
continue
6035+
class Example_uart(object):
6036+
def __init__(self, no=UART.UART2, bate=115200, data_bits=8, parity=0, stop_bits=1, flow_control=0):
6037+
self.uart = UART(no, bate, data_bits, parity, stop_bits, flow_control)
6038+
self.uart.set_callback(self.callback)
60706039

60716040

6041+
def callback(self, para):
6042+
uart_log.info("call para:{}".format(para))
6043+
if(0 == para[0]):
6044+
self.uartRead(para[2])
60726045

6073-
def run():
6074-
# 创建一个线程来监听接收uart消息
6075-
_thread.start_new_thread(UartRead, ())
6076-
6046+
6047+
def uartWrite(self, msg):
6048+
uart_log.info("write msg:{}".format(msg))
6049+
self.uart.write(msg)
6050+
6051+
def uartRead(self, len):
6052+
msg = self.uart.read(len)
6053+
utf8_msg = msg.decode()
6054+
uart_log.info("UartRead msg: {}".format(utf8_msg))
6055+
return utf8_msg
6056+
6057+
def uartWrite_test(self):
6058+
for i in range(10):
6059+
write_msg = "Hello count={}".format(i)
6060+
self.uartWrite(write_msg)
6061+
utime.sleep(1)
60776062

60786063
if __name__ == "__main__":
6079-
uartWrite()
6080-
run()
6081-
while 1:
6082-
if state:
6083-
pass
6084-
else:
6085-
break
6064+
uart_test = Example_uart()
6065+
uart_test.uartWrite_test()
6066+
60866067

60876068
# 运行结果示例
60886069
'''
6089-
INFO:UART:Write msg :Hello count=8
6090-
INFO:UART:Write msg :Hello count=7
6091-
INFO:UART:Write msg :Hello count=6
6092-
INFO:UART:Write msg :Hello count=5
6093-
INFO:UART:Write msg :Hello count=4
6094-
INFO:UART:Write msg :Hello count=3
6095-
INFO:UART:Write msg :Hello count=2
6096-
INFO:UART:Write msg :Hello count=1
6097-
INFO:UART:uartWrite end!
6098-
INFO:UART:UartRead msg: read msg 1
6099-
6100-
INFO:UART:UartRead msg: read msg 2
6101-
6102-
INFO:UART:UartRead msg: read msg 3
6070+
INFO:UART:write msg:Hello count=0
6071+
INFO:UART:write msg:Hello count=1
6072+
INFO:UART:write msg:Hello count=2
6073+
INFO:UART:write msg:Hello count=3
6074+
INFO:UART:write msg:Hello count=4
6075+
INFO:UART:write msg:Hello count=5
6076+
INFO:UART:write msg:Hello count=6
6077+
INFO:UART:write msg:Hello count=7
6078+
INFO:UART:write msg:Hello count=8
6079+
INFO:UART:write msg:Hello count=9
6080+
6081+
INFO:UART:call para:[0, 2, 15]
6082+
INFO:UART:UartRead msg: my name is XXX
6083+
6084+
61036085
'''
61046086

61056087
```

0 commit comments

Comments
 (0)