我有两个 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()