当前位置:网站首页>Alibaba cloud development board haas510 parses serial port JSON data and sends attributes

Alibaba cloud development board haas510 parses serial port JSON data and sends attributes

2022-06-12 13:48:00 TMS320VC5257H

Abstract : This paper introduces the use of HaaS510 Receive data from serial port , Extract the required attribute values from the data , And send it to the IOT platform . The data format received from the serial port is JSON Format .

The experiment purpose : Through the upper computer to haas510 send out JSON character string , from HaaS510 Parse and extract the specified attribute value , And send it to the cloud platform .

Hardware :haas510 (2.0 edition ),USB A serial port

Software :win7 x64,vscode

Catalog

1. Introduction to key knowledge points

2. Write source code

3. Observe the operation effect


1. Introduction to key knowledge points

In this experiment, the computer is used as the upper computer , Upper computer passes USB Serial port and HaaS510 Connect . If you use other microprocessors , As long as it can be sent JSON Format string , The effect is the same . The key knowledge points of the experiment are 2 individual .

First point , How to convert the data received by the serial port into a string . We know , Serial communication , yes 8 position 8 Receive data bit by bit , Equivalent to one by one 8 An array of bit data , This array is not a string at first , We can understand it as a series of hexadecimal data . So we need to restore this data to a string , And then we can parse it .

See documentation

Alibaba cloud development board HaaS510 Respond to UART Serial port instruction https://blog.csdn.net/youngwah292/article/details/120377994?spm=1001.2014.3001.5501icon-default.png?t=L892https://blog.csdn.net/youngwah292/article/details/120377994?spm=1001.2014.3001.5501

Second point , How to parse a string into key attribute values . This knowledge point is also explained in detail through documents .

Alibaba cloud development board HaaS510 analysis JSON data

2. Write source code

This article provides the source code directly

var iot = require('iot');
var network = require('network');
var uart = require('uart');
var net = network.openNetWorkClient();
/*  For information about device triples, please refer to common In the catalog iot Component description  */
var productKey = 'gcCCCCCCu2';
var deviceName = 'ceCCCCCCC01';
var deviceSecret = 'eeSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSb3a9';
//  Build Internet of things connection instances 
var device;
var lightSwitch = 0;

// The test parses a json character string , The string of the serial port is not parsed here , Instead, it parses a fixed array of strings .
var json = '{"uid":"123456","beizhu":"5557589","shijian":"20210919"}';
var obj = JSON.parse(json);

  // user uart
  var serial = uart.open({
    id: 'UART2'
  });

  var msgbuf = [97, 98, 99, 100];

  function ArrayToString(fileData) {
    var dataString = "";
    for (var i = 0; i < fileData.length; i++) {
      dataString += String.fromCharCode(fileData[i]);
    }
    return dataString;
  }


net.on('connect', function () {
    device = iot.device({
        productKey: productKey,
        deviceName: deviceName,
        deviceSecret: deviceSecret,
        region: 'cn-shanghai',
        success: function () {
            console.log('iot: [success] connect');
        },
        fail: function () {
            console.log('iot: [failed] connect');
        }
    });
})

setInterval(function () {
    lightSwitch = 1 - lightSwitch;

    // user uart send data
    serial.write(msgbuf);
    // The parsed content is shown below 
    console.log(obj);         
    console.log(obj.uid);    
    console.log(obj.beizhu);   
    console.log(obj.shijian);

/*  Error model    iot.postProps({
                       payload: {LightSwitch:'0'}
                    })*/
/* Correct demonstration , To send attributes , You have to use stringify Talent */
    device.postProps(
        JSON.stringify({
            LightSwitch: lightSwitch
        })
    );
  
  }, 5000);
// user uart data receive 
serial.on('data', function(data) {
  console.log('uart receive data is ' + ArrayToString(data));  
    json = ArrayToString(data);
    // Then parse the string 
    obj = JSON.parse(json); 
// Here are a few key attribute values to send json = '{"uid":"123456","beizhu":"5557589","shijian":"20210919"}'
    device.postProps( 
                  JSON.stringify({
                    uid:obj.uid,beizhu:obj.beizhu,shijian:obj.shijian
                                 })
                   );

});

The key code is after the serial port receives the data , The operation of parsing data .

json = ArrayToString(data);
    // Then parse the string 
    obj = JSON.parse(json); 
     // Here are a few key attribute values to send json = '{"uid":"123456","beizhu":"5557589","shijian":"20210919"}'
    device.postProps( 
                  JSON.stringify({
                    uid:obj.uid,beizhu:obj.beizhu,shijian:obj.shijian
                                 })
                   );

3. Observe the operation effect

First connect the hardware

After the source code provided in this experiment runs , There will be multiple experimental effects , As shown in the figure below . No more screenshots , Interested readers can check out my other HaaS510 article .

  This experiment focuses on the 5 Experimental results , That is, send a message through the upper computer JSON character string , see HaaS510 Whether the value can be parsed and reported to the cloud in real time .

  From the computer to HaaS510 Send data through the user serial port of

{"uid":"888888","beizhu":"666666","shijian":"20210920"}

It can be observed that this data is parsed , Through debugging, the serial port also has corresponding display .

  The data has been uploaded to the cloud . remarks 、 Time 、uid、 The main light switches have been changed accordingly .

  thus , Successful experiment .

Work here , You can use HaaS510 Designed an Internet of things device . Use one MCU Write functional code . Upload the data to the cloud to JSON Format sent to HaaS510, The attribute value reporting function can be realized . What about? , Easy !

If you still haven't mastered everything , Then I suggest you refer to other articles of the author .

Give people roses , Fragrance in hand .

原网站

版权声明
本文为[TMS320VC5257H]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206121345059062.html