【问题标题】:BBC micro:bit - Radio string transfer random carriage returnsBBC micro:bit - 无线电字符串传输随机回车
【发布时间】:2017-10-27 13:46:03
【问题描述】:

我有两个 BBC Micro Bit 并使用 Radio 功能将数据从一个从机传输到主 Micro Bit。传输数据时,我得到随机回车,我不确定是什么问题,我试图剥离任何随机 CR 等,但仍然遇到同样的问题。

a=1,开,

12

=2,

关闭,77

=3,

开,88

================================================ ====

网关代码

from microbit import *
import radio

radio.config(group=0)
radio.on()

while True:
   incoming = radio.receive()
   if incoming:
      uart.write(incoming)  

===============================================

从机代码

from microbit import *
import radio

radio.config(group=0)
radio.on()

while True:
  if button_a.was_pressed():
      radio.send('Matt,A=On,Off' + '\n')  # a-ha
      display.scroll("A")

  if button_b.was_pressed():
      radio.send('Matt,B=On,Off' + '\n')  # a-ha
      display.scroll("B")

================================================ ==========

Py序列号

import sys
import glob  
import serial


def serial_ports():
    ports = ['COM%s' % (i + 1) for i in range(256)]

result = []
for port in ports:
    try:
        s = serial.Serial(port)
        s.close()
        result.append(port)
    except (OSError, serial.SerialException):
        pass
return result


if __name__ == '__main__':
  print(serial_ports())
try:
ser = serial.Serial('COM5', 115200, timeout = 0)
print("connected to: " + (ser.portstr))
except serial.SerialException:
pass

while True:
line = ser.readline().decode('utf-8')
# Read a line and convert it from b'xxx\r\n' to xxx 

if line:  # If it isn't a blank line
    f = open('output.csv', 'a+')
    f.write(line + '\n')
    print(line)
    f.close()

ser.close()

【问题讨论】:

    标签: python string radio strip bbc-microbit


    【解决方案1】:

    我发现您的脚本无需发送额外的回车即可运行。我使用两个微比特进行了测试。我在 mu 和 CoolTerm 中使用了 REPL,设置为 115200 波特。我使用 Linux Mint 作为我的操作系统。 CoolTerm 输出: 哑光,B=开,关 马特,A=开,关

    在 pyserial 代码发布后添加: 下面的代码可以让我在没有额外空行的情况下产生预期的输出。通过在 print 语句中使用 end='' 删除换行符。使用 pid 和 vid 查找 microbit 使您能够连接其他串行设备。感谢 microbit-playground 发布了有关如何使用 pid 和 vid 查找 microbit 的示例代码。

    我使用 jupyter notebook 在 Linux 上对此进行了测试。它应该可以在 Windows 上运行而无需修改。

    import serial
    import serial.tools.list_ports as list_ports
    
    def find_microbit_comport():
        ports = list(list_ports.comports())
        for p in ports:
            if (p.pid == 516) and (p.vid == 3368):
                return str(p.device)
    
    if __name__ == '__main__':
        ser = serial.Serial()
        ser.baudrate = 115200
        ser.port = find_microbit_comport()
        ser.open()
    
    while True:
        line = ser.readline().decode('utf-8')
        if line:  # If it isn't a blank line
            f = open('output.csv', 'a+')
            f.write(line)
            print(line, end='')
            f.close()
    
    ser.close()
    

    【讨论】:

    • 用另一个串行终端测试好点,我一直在使用 pyserial,所以我的 pyserial 代码肯定有问题。感谢您的测试
    • 如果您想发布您的 pyserial 代码,也许我们可以看到添加回车的内容。
    • 我添加了示例 pyserial 代码。如果你喜欢这个解决方案,请接受它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-23
    • 2018-05-14
    • 1970-01-01
    • 1970-01-01
    • 2020-04-29
    • 2014-10-21
    • 1970-01-01
    相关资源
    最近更新 更多