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

2024-09-28 13:25:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图通过python将数据从MacBook发送到microbit连接的USB端口。我的python程序传输数据,然后通过查看microbit的背面,我看到在发送信息时,USB端口旁边有一个小灯闪烁,因此microbit正在接收信息,但我为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('')

这是我的自定义模块软件:

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)

这是微位代码:

code


Tags: 端口import程序信息datatimeasserial
1条回答
网友
1楼 · 发布于 2024-09-28 13:25:57

请在下面找到一个有效的解决方案

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

enter image description here

我使用运行固件V253的microbit在DebianLinux上运行了以下Python v3.7代码。由于microbit安装的端口在每次连接时都会发生变化,因此我编写了find_comport方法,使用micro:bit的VID和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

相关问题 更多 >

    热门问题