简体   繁体   English

在 Windows 中读取从 Microbit 发送的串行数据

[英]Reading Serial Data Sent From Microbit in Windows

I am trying to send data from my microbit to my windows PC over serial communication.我正在尝试通过串行通信将数据从我的 microbit 发送到我的 Windows PC。 I followed the example here from the microbit website but can't seem to get my Tera Term window to display anything.我跟着的例子在这里从microbit网站,但似乎无法让我的Tera Term窗口来显示什么。

The microbit is plugged into one of the USB ports on the front of my PC and has the following code on it: microbit 插入我电脑前面的 USB 端口之一,上面有以下代码:

basic.forever(function () {
    serial.writeLine("test")
})

When I add a new connection to Tera Term I select COM1 (this is the only option I have)当我向 Tera Term 添加新连接时,我选择COM1 (这是我唯一的选项)

Tera 术语中的新连接

I then go into Setup > Serial Port and save the following settings然后我进入设置>串行端口并保存以下设置

串行传输设置

Unfortunately, this doesn't display any data, I have tried different USB ports and cables to no avail.不幸的是,这不会显示任何数据,我尝试了不同的 USB 端口和电缆均无济于事。

The following python 3 code scans through the serial ports until it locates the one connected to your microbit, using the microbit's VID and PID.以下 python 3 代码扫描串行端口,直到它使用 microbit 的 VID 和 PID 找到连接到您的 microbit 的那个。 The script will then display port information.然后脚本将显示端口信息。 After this, the script displays anything sent through the serial port from the microbit.在此之后,脚本显示从 microbit 通过串行端口发送的任何内容。

You can use the port name to set up Tera Term, or let the script continue to display the data coming through the serial port.您可以使用端口名称来设置 Tera Term,或者让脚本继续显示通过串行端口传入的数据。 The default baud rate for the microbit is 115200. In the example output below, the port name is COM5. microbit 的默认波特率为 115200。在下面的示例输出中,端口名称为 COM5。 Each time you unplug and replug the microbit, the port name can change.每次拔出和重新插入 microbit 时,端口名称都可以更改。

example output:示例输出:

starting
looking for microbit
scanning ports
port: COM5 - mbed Serial Port (COM5)
pid: 516 vid: 3368
found target device pid: 516 vid: 3368 port: COM5
opening and monitoring microbit port

code:代码:

import serial
import serial.tools.list_ports as list_ports


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:
        print('port: {}'.format(p))
        try:
            print('pid: {} vid: {}'.format(p.pid, p.vid))
        except AttributeError:
            continue
        if (p.pid == pid) and (p.vid == vid):
            print('found target device pid: {} vid: {} port: {}'.format(
                p.pid, p.vid, p.device))
            ser_port.port = str(p.device)
            return ser_port
    return None


def main():
    print('looking for microbit')
    ser_micro = find_comport(PID_MICROBIT, VID_MICROBIT, 115200)
    if not ser_micro:
        print('microbit not found')
        return
    print('opening and monitoring microbit port')
    ser_micro.open()
    while True:
        line = ser_micro.readline().decode('utf-8')
        if line:  # If it isn't a blank line
            print(line)
    ser_micro.close()


if __name__ == '__main__':
    print('starting')
    main()
    print('exiting')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM