首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Microbit未接收到计算机发送的串行数据

Microbit未接收到计算机发送的串行数据
EN

Stack Overflow用户
提问于 2020-07-11 01:47:41
回答 1查看 303关注 0票数 1

我正在尝试通过python将数据从我的MacBook发送到我的microbit所连接的USB端口。我的python程序传输数据,然后通过查看microbit的背面,我看到当发送信息时,在USB端口旁边有一个小灯在闪烁,所以microbit正在接收信息,但是我为microbit编写的程序不会显示已经发送的信息。我也遵循了一个关于如何做到这一点的教程。有点不对劲,我需要帮助!

代码语言:javascript
复制
import serial
import Stock_Web as SW
import time

ser = serial.Serial()
ser.baudrate = 115200
ser.port = "/dev/cu.usbmodem14102"
ser.open()

while True:
    i =+ 1
    i = str(i)
    print('this is time ' + i)
    DowPerBytes = str(SW.DowPercent())
    DowPerBytes = DowPerBytes.encode()
    ser.write(DowPerBytes)
    time.sleep(.5)
    # data = microbitdata[2:]
    # data = data.replace('')

这是我的自定义模块SW:

代码语言:javascript
复制
import requests
from bs4 import BeautifulSoup as soup

def DowPercent():
    url = 'https://money.cnn.com/data/markets/'
    result = requests.get(url)
    src = result.content
    Soup = soup(src, 'html.parser')

    stock_per_raw = Soup.find('span', class_="ticker-name-change", attrs={"stream": "changePct_599362", "data-ticker-name": "Dow"})

    return soup.get_text(stock_per_raw)

下面是微位代码:

EN

回答 1

Stack Overflow用户

发布于 2020-07-14 22:26:04

请在下面找到一个可行的解决方案。

这是我使用的块的屏幕截图:

我获得了以下Python v3.7代码,以便使用微位运行固件V253在Debian Linux上工作。由于microbit挂载的端口在每次连接时都会发生变化,因此我编写了find_comport方法来使用micro:bit的VID和PID来识别端口。您的示例代码中没有更改Stock_Web.py。我可以看到例如0.1%滚动通过LED。

代码语言:javascript
复制
import logging
import serial
import serial.tools.list_ports as list_ports
import Stock_Web as SW
import time

logging.basicConfig(level=logging.DEBUG, format='%(message)s')

PID_MICROBIT = 516
VID_MICROBIT = 3368
TIMEOUT = 0.1


def find_comport(pid, vid, baud):
    ''' return a serial port '''
    ser_port = serial.Serial(timeout=TIMEOUT)
    ser_port.baudrate = baud
    ports = list(list_ports.comports())
    print('scanning ports')
    for p in ports:
        logging.debug('port: {}'.format(p))
        try:
            logging.debug('pid: {} vid: {}'.format(p.pid, p.vid))
        except AttributeError:
            continue
        if (p.pid == pid) and (p.vid == vid):
            logging.info('found target device pid: {} vid: {} port: {}'.format(
                p.pid, p.vid, p.device))
            ser_port.port = str(p.device)
            return ser_port
    return None


i=0
logging.debug('looking for microbit')
ser_micro = find_comport(PID_MICROBIT, VID_MICROBIT, 115200)
if not ser_micro:
    logging.info('microbit not found')
    
logging.debug('opening and monitoring microbit port')
ser_micro.open()

while True:
    i += 1
    print
    logging.debug('this is time {}'.format(i))
    DowPerBytes = str(SW.DowPercent())
    logging.debug('{}'.format(DowPerBytes))
    DowPerBytes = DowPerBytes.encode()
    logging.debug('{}'.format(DowPerBytes))
    ser_micro.write(DowPerBytes)
    time.sleep(5)

屏幕输出:

代码语言:javascript
复制
looking for microbit
scanning ports
port: /dev/ttyACM3 - "BBC micro:bit CMSIS-DAP" - mbed Serial Port
pid: 516 vid: 3368
found target device pid: 516 vid: 3368 port: /dev/ttyACM3
opening and monitoring microbit port
this is time 1
Starting new HTTPS connection (1): money.cnn.com:443
https://money.cnn.com:443 "GET /data/markets/ HTTP/1.1" 200 29158
0.10%
b'0.10%'
this is time 2
Starting new HTTPS connection (1): money.cnn.com:443
https://money.cnn.com:443 "GET /data/markets/ HTTP/1.1" 200 29158
0.10%
b'0.10%'
this is time 3
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62839716

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档