Air780E开发板USB串口

本文详细介绍了如何使用Python的pyserial库与Lua进行串口通信,涉及安装库、识别特定USB设备、设置波特率和数据位,以及在Lua中实现数据接收和发送的示例。
摘要由CSDN通过智能技术生成

python

#!/usr/bin/python3
# -*- coding: UTF-8 -*-
# 安装pyserial库
import os, sys, serial.tools.list_ports, time


for item in serial.tools.list_ports.comports():
    if not item.pid or not item.location :
        continue
    if item.vid == 0x19d1 and item.pid == 0x0001 and "x.6" in item.location :
        print(dir(item))
        print(item.name)
        with serial.Serial(item.name, 115200, timeout=1) as ser:
            while 1:
                data = ser.read(128)
                if data :
                    print( str(time.time()) + ">> " + str(data))
                    ser.write("Hi from PC".encode())
                else :
                    ser.write("Hi from PC".encode())

开发板

-- LuaTools需要PROJECT和VERSION这两个信息
PROJECT = "usb_uart"
VERSION = "1.0.0"

log.info("main", PROJECT, VERSION)

-- 引入必要的库文件(lua编写), 内部库不需要require
sys = require("sys")


--[[
注意: 若使用普通串口调试功能, 需要以下条件之一才能收到数据
1. 把DTR设置为高电平
2. 先发送一次数据

参考用的上位机, 用py演示, 需要pyserial库

import os, sys, serial.tools.list_ports, time

for item in serial.tools.list_ports.comports():
    if not item.pid or not item.location :
        continue
    if item.vid == 0x19d1 and item.pid == 0x0001 and "x.6" in item.location :
        print(dir(item))
        print(item.name)
        with serial.Serial(item.name, 115200, timeout=1) as ser:
            while 1:
                data = ser.read(128)
                if data :
                    print( str(time.time()) + ">> " + str(data))
                else :
                    ser.write("Hi from PC".encode())
]]

log.info("main", "usb uart demo")

local uartid = uart.VUART_0 -- USB虚拟串口的固定id

--初始化
local result = uart.setup(
    uartid,--串口id
    115200,--波特率,其实无所谓, 纯虚拟串口
    8,--数据位
    1--停止位
)


-- 收取数据会触发回调, 这里的"receive" 是固定值
uart.on(uartid, "receive", function(id, len)
    local s = ""
    repeat
        -- s = uart.read(id, 1024)
        s = uart.read(id, len)
        if s and #s > 0 then -- #s 是取字符串的长度
            -- 如果传输二进制/十六进制数据, 部分字符不可见, 不代表没收到
            -- 关于收发hex值,请查阅 https://doc.openluat.com/article/583
            log.info("uart", "receive", id, #s, s)
            uart.write(uart.VUART_0, s)
            -- log.info("uart", "receive", id, #s, s:toHex())
        end
    until s == ""
end)
local tx_buff = zbuff.create(24)
tx_buff:set(0, 0x31)
-- 并非所有设备都支持sent事件
uart.on(uartid, "sent", function(id)
    log.info("uart", "sent", id)
end)

sys.taskInit(function()

    while 1 do
        uart.write(uart.VUART_0, "hello test usb-uart\r\n")
        -- uart.tx(uart.VUART_0, tx_buff,0, tx_buff:len())
        sys.wait(1000)
    end
end)


-- 用户代码已结束---------------------------------------------
-- 结尾总是这一句
sys.run()
-- sys.run()之后后面不要加任何语句!!!!!

  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值