Skip to content

Instantly share code, notes, and snippets.

@wwj718
Last active April 23, 2018 02:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wwj718/bd2acc8f1b1b20e8872f23968ee6845d to your computer and use it in GitHub Desktop.
Save wwj718/bd2acc8f1b1b20e8872f23968ee6845d to your computer and use it in GitHub Desktop.
pc程序
```
while True:
data = {"a":i}
# data = "topic-sensor-data-x" + "\r\n"
data = json.dumps(data) + "\r\n"
# data = "a" + "\r\n"
data_bytes = data.encode("utf-8")
ser.write(data_bytes)
time.sleep(0.1)
read_data = ser.readline()
print("from microbit: {}".format(read_data))
i += 1
```
现象1:
程序
```
def get_topic_and_data(b):
try:
c = b'{"a": 0}\r\n'.strip()
r = c + b'\r\n'
uart.write(r) # uart.write(c + b'\r\n')
except:
pass
```
输出
```
from microbit: b'{"a": 0}\r\n'
from microbit: b'{"a": 0}\r\n'
from microbit: b'{"a": 0}\r\n'
from microbit: b'{"a": 0}\r\n'
from microbit: b'{"a": 0}\r\n'
from microbit: b'{"a": 0}\r\n'
from microbit: b'{"a": 0}\r\n'
from microbit: b'{"a": 0}\r\n'
from microbit: b'{"a": 0}\r\n'
from microbit: b'{"a": 0}\r\n'
from microbit: b'{"a": 0}\r\n'
from microbit: b'{"a": 0}\r\n'
from microbit: b'{"a": 0}\r\n'
from microbit: b'{"a": 0}\r\n'
from microbit: b'{"a": 0}\r\n'
from microbit: b'{"a": 0}\r\n'
```
@wwj718
Copy link
Author

wwj718 commented Apr 23, 2018

现象2

程序 :

def get_topic_and_data(b):
    try:
         uart.write(b) 
    except:
        pass

输出:

from microbit: b'{"a": 0}\r\n'
from microbit: b'{"a": 1}\r\n'
from microbit: b'{"a": 2}\r\n'
from microbit: b'{"a": 3}\r\n'
from microbit: b'{"a": 4}\r\n'
from microbit: b'{"a": 5}\r\n'
from microbit: b'{"a": 6}\r\n'
from microbit: b'{"a": 7}\r\n'
from microbit: b'{"a": 8}\r\n'
from microbit: b'{"a": 9}\r\n'
from microbit: b'{"a": 10}\r\n'
from microbit: b'{"a": 11}\r\n'
from microbit: b'{"a": 12}\r\n'
from microbit: b'{"a": 13}\r\n'

现象3

程序 :

def get_topic_and_data(b):
    try:
        c = b.strip()
        r = c + b'\r\n'
        uart.write(r) # uart.write(c + b'\r\n')
    except:
        pass

输出

from microbit: b'{"a": 0}\r\n'
from microbit: b'{"a": 1}\r\n'
from microbit: b'{"a": 2}\r\n'
from microbit: b'{"a\r\n'
from microbit: b'": 3}\r\n'
from microbit: b'{"a": 4}\r\n'
from microbit: b'{"a": 5}\r\n'
from microbit: b'{"a": 6}\r\n'
from microbit: b'{"a": 7}\r\n'
from microbit: b'{"a":\r\n'
from microbit: b'8}\r\n'
from microbit: b'{"a": 9}\r\n'
from microbit: b'{"a": 10}\r\n'
from microbit: b'{"a": 11}\r\n'
from microbit: b'{"a": 12}\r\n'
from microbit: b'{"a": 13\r\n'
from microbit: b'}\r\n'

@wwj718
Copy link
Author

wwj718 commented Apr 23, 2018

pc 程序

while True:
    data = {"a": 0}
    # data = "topic-sensor-data-x" + "\r\n"
    data = json.dumps(data) + "\r\n"
    # data = "a" + "\r\n"
    data_bytes = data.encode("utf-8")
    ser.write(data_bytes)
    time.sleep(0.1)
    read_data = ser.readline()
    print("from microbit: {}".format(read_data))
    i += 1

现象4

from microbit import *

def get_topic_and_data(b):
    try:
        assert b == b'{"a": 0}\r\n'
        # c = b.strip()
        # r = c + b'\r\n'
        uart.write(b'ok'+ b)
    except:
        uart.write(b'error'+b)
        # display.show("!")

输出:

from microbit: b'ok{"a": 0}\r\n'
from microbit: b'ok{"a": 0}\r\n'
from microbit: b'error{"a":error 0}\r\n'
from microbit: b'ok{"a": 0}\r\n'
from microbit: b'ok{"a": 0}\r\n'
from microbit: b'ok{"a": 0}\r\n'
from microbit: b'ok{"a": 0}\r\n'
from microbit: b'ok{"a": 0}\r\n'
from microbit: b'ok{"a": 0}\r\n'
from microbit: b'ok{"a": 0}\r\n'
from microbit: b'ok{"a": 0}\r\n'
from microbit: b'error{"aerror": 0}\r\n'
from microbit: b'ok{"a": 0}\r\n'
from microbit: b'ok{"a": 0}\r\n'
from microbit: b'ok{"a": 0}\r\n'
from microbit: b'ok{"a": 0}\r\n'
from microbit: b'ok{"a": 0}\r\n'
from microbit: b'ok{"a": 0}\r\n'
from microbit: b'ok{"a": 0}\r\n'
from microbit: b'error{"a": error0}\r\n'
from microbit: b'ok{"a": 0}\r\n'

@wwj718
Copy link
Author

wwj718 commented Apr 23, 2018

重要现象

def get_topic_and_data(b):
    try:
        # assert b == b'{"a": 0}\r\n'
        uart.write(b) 
    except:
        uart.write(b'error'+b) 

输出:

from microbit: b'{"a": 0}\r\n'
from microbit: b'{"a": 0}\r\n'
from microbit: b'{"a": 0}\r\n'
from microbit: b'{"a": 0}\r\n'
from microbit: b'{"a": 0}\r\n'
from microbit: b'{"a": 0}\r\n'
from microbit: b'{"a": 0}\r\n'
from microbit: b'{"a": 0}\r\n'
from microbit: b'{"a": 0}\r\n'
from microbit: b'{"a": 0}\r\n'
from microbit: b'{"a": 0}\r\n'
from microbit: b'{"a": 0}\r\n'

解释: 实际上这里已经出现问题了,被拆开,往回写又合在了一起, 少掉空格也是这个原因

对照现象

def get_topic_and_data(b):
    try:
        assert b == b'{"a": 0}\r\n'
        uart.write(b) 
    except:
        uart.write(b'error'+b) 

输出:

from microbit: b'error{"a"error: 0}\r\n'
from microbit: b'{"a": 0}\r\n'
from microbit: b'{"a": 0}\r\n'
from microbit: b'{"a": 0}\r\n'
from microbit: b'{"a": 0}\r\n'
from microbit: b'{"a": 0}\r\n'
from microbit: b'error{"errora": 0}\r\n'
from microbit: b'{"a": 0}\r\n'
from microbit: b'{"a": 0}\r\n'
from microbit: b'{"a": 0}\r\n'
from microbit: b'{"a": 0}\r\n'
from microbit: b'{"a": 0}\r\n'
from microbit: b'{"a": 0}\r\n'
from microbit: b'{"a": 0}\r\n'
from microbit: b'{"a": 0}\r\n'
from microbit: b'{"a": 0}\r\n'
from microbit: b'{"a": 0}\r\n'
from microbit: b'error{"aerror": 0}\r\n'

@wwj718
Copy link
Author

wwj718 commented Apr 23, 2018

推理1: r = uart.readline() 有时候没有抵达\r\n

@wwj718
Copy link
Author

wwj718 commented Apr 23, 2018

最终解决方案:

pc:

#!/usr/bin/env python
# encoding: utf-8

import serial, json, time
from serial.tools.list_ports import comports as list_serial_ports

def find_microbit():
    """
    Finds the port to which the device is connected.
    https://github.com/mu-editor/mu/blob/803153f661097260206ed2b7cc9a1e71b564d7c3/mu/contrib/microfs.py#L44
    """
    ports = list_serial_ports()
    for port in ports:
        if "VID:PID=0D28:0204" in port[2].upper():
            return port[0]
    return None


port = find_microbit()
baudrate=115200
ser = serial.Serial(port, baudrate, timeout=1)

i = 0
while True:
    data = {"a": i}
    # data = "topic-sensor-data-x" + "\r\n"
    data = json.dumps(data) + "\r\n"
    # data = "a" + "\r\n"
    data_bytes = data.encode("utf-8")
    ser.write(data_bytes)
    time.sleep(0.1)
    read_data = ser.readline()
    print("from microbit: {}".format(read_data))
    i += 1

microbit:

from microbit import *

uart.init(115200)

def get_topic_and_data(b):
    try:
        c = b.strip()
        json_str = str(c, 'utf-8')
        json = eval(json_str)
        show_num = str(json['a'])[-1]
        display.show(show_num)
        uart.write(c + b'\r\n')
    except:
        uart.write(b'error'+b) 
        
r_last = b''

while True:
    r = uart.readline()  # readline不保证返回完整的一行,自行拼接,使用一个具体的例子思考 b'{"a": 1}'
    if r:
        if r_last:
            r = r_last+r
            r_last = b''
        if r.startswith(b'{') and r.endswith(b'\r\n'):
            d = get_topic_and_data(r)
        else:
            r_last = r 

输出:

from microbit: b'{"a": 0}\r\n'
from microbit: b'{"a": 1}\r\n'
from microbit: b'{"a": 2}\r\n'
from microbit: b'{"a": 3}\r\n'
from microbit: b'{"a": 4}\r\n'
from microbit: b'{"a": 5}\r\n'
from microbit: b'{"a": 6}\r\n'
from microbit: b'{"a": 7}\r\n'
from microbit: b'{"a": 8}\r\n'
from microbit: b'{"a": 9}\r\n'
from microbit: b'{"a": 10}\r\n'
from microbit: b'{"a": 11}\r\n'
from microbit: b'{"a": 12}\r\n'
from microbit: b'{"a": 13}\r\n'
from microbit: b'{"a": 14}\r\n'
from microbit: b'{"a": 15}\r\n'
from microbit: b'{"a": 16}\r\n'
from microbit: b'{"a": 17}\r\n'
from microbit: b'{"a": 18}\r\n'
from microbit: b'{"a": 19}\r\n'
from microbit: b'{"a": 20}\r\n'
from microbit: b'{"a": 21}\r\n'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment