Microbit 没有接收到从计算机发送的串行数据
Microbit not receiving serial data sent form computer
我正在尝试通过 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)
这里是微位代码:
请在下面找到可行的解决方案。
这是我使用的块的屏幕截图:
我得到了以下 Python v3.7 代码,可以在 Debian Linux 上使用 microbit 运行 固件 v 253。因为 microbit 安装的端口可以改变每个连接时,我写了find_comport方法来使用micro:bit的VID和PID来识别端口。 Stock_Web.py 未更改您的示例代码。我可以看到例如在 LED 上滚动 0.1%。
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
我正在尝试通过 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)
这里是微位代码:
请在下面找到可行的解决方案。
这是我使用的块的屏幕截图:
我得到了以下 Python v3.7 代码,可以在 Debian Linux 上使用 microbit 运行 固件 v 253。因为 microbit 安装的端口可以改变每个连接时,我写了find_comport方法来使用micro:bit的VID和PID来识别端口。 Stock_Web.py 未更改您的示例代码。我可以看到例如在 LED 上滚动 0.1%。
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