String Introduction json/regular

introduction

In traditional MCU applications, when two MCUs communicate via serial ports, they define some frames themselves, including frame header, data frame, check frame, and frame tail. The figure shows the flight control MavLink protocol.

This way is stable and efficient but with obvious shortcoming such as the opening problem is a little huge,needing make protocol by your own ,compile the code manually as well.if intend to try communicate between two single chip,I recommend to use serial-ports transmission string json!

Excellent feature:there is no need to know the issues in the bottom,for instance:

  • no considering main aspects and little end
  • no considering byte changing of data
  • support random length of int,float
  • Easy to understand and open up.

Shortcoming:the efficiency is a little bit low,the compiling and the decoding will take up cpu.

In fact, transmitting JSON has become a standard in network programming. For example, in RESTful API, the front-end and back-end use JSON to obtain information. It is just not common in the field of traditional single-chip microcomputers. On the one hand, the efficiency is slightly lower, and embedded systems usually have higher requirements for cost control. However, with the reduction of chip costs, many applications do not have very high requirements for material costs, and pay more and more attention to development efficiency.

String

string = "hello string!"

OpenMV is able to send string through serial.

from machine import UART

uart = UART(1, 9600)
string = "hello string!"
uart.write(string)

String operation

http://www.runoob.com/python3/python3-string.html

For example:


blobs=[12,23,11,22,33,44]

print("%d", blobs[3])

JSON is a concise and efficient form to exchange data.It can be simple

like this:

"[[12,0],[10,12],[22,10],[99,11]]"

Remark:I use such straightforward string to send coordination x,y of color lump in OpenMV.

Also It can be complex like this:(‘’’indicates multirow string in python)

'''
{
    "number":10,
    "color" :[255,0,0],
    "rate" :0.65
}
'''

I use this structure to send color information gathered in OpenMV to the server of wifi.

So such as,it can be like this:

'''
{
  "firstName": "John",
  "lastName": "Smith",
  "sex": "male",
  "age": 25,
  "address": 
  {
    "streetAddress": "21 2nd Street",
    "city": "New York",
    "state": "NY",
    "postalCode": "10021"
  },
  "phoneNumber": 
  [
    {
      "type": "home",
      "number": "212 555-1234"
    },
   {            
      "type": "fax",
      "number": "646 555-4567"
    }
  ]
}
'''

Attention:the form of json is like Python,but json is the expressing form about subject,unlike the representation of python somewhat.

Python dump json

OpenMV has the modules of json, json.dumps(obj) and ujson.loads(str) could easily dump string jsonand decode string json.

import json

obj = [[12,0],[10,12],[22,10],[99,11]]
print(json.dumps(obj))

obj = {
    "number":10,
    "color" :[255,0,0],
    "rate" :0.65
}
print(json.dumps(obj))

Will output:

'[[12, 0], [10, 12], [22, 10], [99, 11]]'

'{"color": [255, 0, 0], "number": 10, "rate": 0.65}'

Then send strings through serial,on the other end,decode string json into subjrct/array and go with following logical operation.

Other sigle chip’s module json

Json is simple and universal.

You can use these banks,transport string json into object.

Other language’s module json on computer

Basically,all the language support jaon(even lisp),so it is easily to communicate.

Regular expression

If it is some simple process of string,just need to use some internal functions.But to some relatively complex require,you will need regular expression.

For example,on the process of URL(it will not be used often in OpenMV,omit it,there will be a minute introduction on esp application of network in my MicroPython)

results matching ""

    No results matching ""