3.16 haas506开发教程-example-通过子线程检测串口指令数据

该教程详细介绍了如何使用Haas506开发板进行串口数据处理。通过创建子线程,程序在主线程持续打印'helloworld'的同时,监听并解析串口助手发送的JSON格式数据,用于控制LED灯的状态。用户可以自定义输入指令,点亮或熄灭特定的LED灯。
摘要由CSDN通过智能技术生成

3.16 haas506开发教程-example-通过子线程检测串口指令数据

1.简介

 本案例使用子线程对串口所发送的数据进行处理和执行相应的逻辑操作。

2.测试代码

  • main.py

 在main.py中,先初始化haas506开发板上的led灯,然后开启子线程对串口助手发送过来的数据进行处理,最后在主线程中循环打印helloworld。

import utime as time
import app_led
import app_input

def main():
    app_led.led_init()
    #子线程检测数据输入
    app_input.start_input_cmd_thread()
    #主线程
    while True:
        time.sleep(1)
        #do something 
        #for example ,print helloworld
        print("hello world")

if __name__=="__main__":
    main()

  • app_led.py

 在app_led.py中初始化led灯,以及执行点亮和熄灭操作。

from driver import GPIO

led1=GPIO()
led2=GPIO()
led3=GPIO()
led4=GPIO()
led5=GPIO()
led1.open('led1')
led2.open('led2')
led3.open('led3')
led4.open('led4')
led5.open("led5")


def led_init():
    led1.write(0)
    led2.write(0)
    led3.write(0)
    led4.write(0)
    led5.write(0)
    
def write(key,value):
    if key=="led1":
        led1.write(value)
    elif key=="led2":
        led2.write(value)
    elif key=="led3":
        led3.write(value)            
    elif key=="led4":
        led4.write(value)
    elif key=="led5":
        led5.write(value)
    else:
        print("please check cmd that you send")
        pass

  • app_input

 在app_input.py中 ,先接收串口助手发送过来的json格式的数据,然后对该数据进行处理。

# -*- coding: UTF-8 -*-
import _thread
import ujson
import app_led


# data format :  {"led_ctrl":{"led1":1,"led2":1,"led3":1,"led4":1,"led5":1}}

def wait_input_cmd(id):
    while True:
        try:
            inputcmd = input('please input cmd:')
        except KeyboardInterrupt:
            _thread.exit()
        try:
            inputcmd_dict = ujson.loads(inputcmd)
            #判断所输入的数据 是否是字典格式
            if isinstance(inputcmd_dict,dict):
                #判断 "键led_ctrl" 所对应的"值"是否是字典格式
                if 'led_ctrl' in inputcmd_dict and isinstance(inputcmd_dict['led_ctrl'],dict):
                    for key,value in inputcmd_dict['led_ctrl'].items():
                        #修改led灯的电平值,实现点亮/熄灭led灯
                        app_led.write(key,value)
            else:
                print("cmd is not a dict")
                print(inputcmd_dict) 
        except ValueError:
            '''other cmd process'''
            print('input cmd is:' + inputcmd)
            pass
        finally:
            pass

def start_input_cmd_thread():
    _thread.start_new_thread(wait_input_cmd,(1,))


  • board.json
{
    "name": "haas506",
   "version": "1.0.0",
   "io": {
    "led1": {
      "type": "GPIO",
      "port": 0,
      "dir": "output",
      "pull": "pullup"
      },
  "led2": {
      "type": "GPIO",
      "port": 6,
      "dir": "output",
      "pull": "pullup"
      },
  "led3": {
      "type": "GPIO",
      "port": 7,
      "dir": "output",
      "pull": "pullup"
      },
  "led4": {
      "type": "GPIO",
      "port": 8,
      "dir": "output",
      "pull": "pullup"
      },
  "led5": {
      "type": "GPIO",
      "port": 9,
      "dir": "output",
      "pull": "pullup"
      },  
   "serial1":{
       "type":"UART",
       "port":0,
       "dataWidth":8,
       "baudRate":115200,
       "stopBits":1,
       "flowControl":"disable",
       "parity":"none"
       },
  "serial2":{
       "type":"UART",
       "port":1,
       "dataWidth":8,
       "baudRate":115200,
       "stopBits":1,
       "flowControl":"disable",
       "parity":"none"
       },
  "serial3":{
       "type":"UART",
       "port":2,
       "dataWidth":8,
       "baudRate":115200,
       "stopBits":1,
       "flowControl":"disable",
       "parity":"none"
       }
   },
   "debugLevel": "ERROR",
   "repl":"enable",
   "replPort":0
}

3.测试结果

 代码烧录完毕后,打开串口助手。主线程循环打印"helloworld”,子线程等待数据的输入。

在这里插入图片描述

 先在串口助手的编辑框中输入{“led_ctrl”:{“led1”:1,“led2”:1,“led3”:1,“led4”:1,“led5”:1}},然后勾选“加回车换行”,最后点击“发送”。这样会点亮5个小灯。

在这里插入图片描述

 也可以自定义点亮/熄灭某个小灯,如输入{“led_ctrl”:{“led1”:1,“led2”:0,“led3”:0,“led4”:0,“led5”:1}},点亮两个小灯和熄灭3个小灯。

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值