String/json/regular

introduction

In the application of Single Chip Processor,two single chip serial ports communicate with each other by defining some frames including frame head, data frame,check frame and ending-frame delimiter.It is the pact of flight control MavLink

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,transport json is becoming standard in the network programming,like in restful api,forepart and back-end use json to maintain information.It is rarely seen in the field of traditional single chip.On the one hand, the efficiency is a little bit low,flush bonding has higher require to cost,but with the reduction of cost of chip,plenty of applications require not-so-high cost on material cost while focus more on the efficiency of exploitation.

String

string = "hello string!"

OpenMV is able to send string through serial.

from pyb import UART

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

String operation

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

Take an instance:

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

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

JSON

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