1

我正在尝试通过 python 将数据从我的 MacBook 发送到我的 microbit 连接到的 USB 端口。我的python程序传输数据然后通过查看我的microbit背面我看到在发送信息时USB端口旁边有一个小灯在闪烁,所以micro bit正在接收信息但是我为microbit编写的程序赢了'不显示已发送的信息。我也遵循了有关如何执行此操作的教程。出了点问题我需要帮助!

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:

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)

这是微位代码:

代码

4

1 回答 1

1

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

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

在此处输入图像描述

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

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)

屏幕输出:

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
于 2020-07-14T14:26:03.863 回答