BBC micro:bit - 无线电字符串传输随机回车 [英] BBC micro:bit - Radio string transfer random carriage returns

查看:91
本文介绍了BBC micro:bit - 无线电字符串传输随机回车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 BBC Micro Bit 并使用 Radio 功能将数据从一个从机传输到主 Micro Bit.传输数据时,我收到随机回车,我不确定是什么问题,我尝试删除任何随机 CR 等,但仍然遇到相同的问题.

I have two BBC Micro Bit and using Radio function to transfer data from one slave to the master Micro Bit. When the data is transferred I am getting random carriage returns, I am not sure what is the problem, I have tried to strip any random CR etc, but still getting the same problem.

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)  

================================================/p>

从代码

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

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")

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

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

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()

推荐答案

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

I found your scripts worked without sending extra carriage returns. I tested using two microbits. I used the REPL in mu and also CoolTerm, set to 115200 baud. I am using Linux Mint as my OS. CoolTerm output: Matt,B=On,Off Matt,A=On,Off

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

Added after the pyserial code was posted: The code below works for me to produce the expected output without extra blank lines. The newline is removed by using the end='' in the print statement. Finding the microbit using the pid and vid enables you to have other serial devices attached. Credit to microbit-playground for posting example code on how to use pid and vid to find the microbit.

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

I tested this on Linux using the jupyter notebook. It should work on Windows without modification.

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()

这篇关于BBC micro:bit - 无线电字符串传输随机回车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆