Jump to content
Fórum Script Brasil
  • 0

Converter string to array


Priscilla Fabricio Ramos

Question

Amigos, boa noite !

Estou precisando de uma ajuda.

Estou recendo um objeto da porta serial do meu arduino que se convertido em string fica igual ao código abaixo

let a = data.toString();

Retorno

{"sensor":"dht22","data":[65.4,2
2.3,17.3038,15.51653]}

let b = JSON.stringify(a);

Retorno

"{\"sensor\":\"dht22\",\"data\":[65.4,2"
"2.3,17.3038,15.51653]}"

Preciso transformar isso o (a) ou (b) em um  array no javascript 

Como consigo fazer isso?

 

Segue código na integra

 

var app = require('./config/express')();
var http =  require('http').Server(app);
var io =  require('socket.io')(http);
var serialport = require('serialport');
var cron = require('node-cron');

const porta = 3000
const portSerialName = "COM4";

//configura a serialport
var portSerial = new serialport(portSerialName, {
    baudRate: 9600,
    parser: new serialport.parsers.Readline('\r\n')
});

http.listen(porta, function() {
  console.log(`Escutando a porta: ${porta}`);
  portSerial.on('open',()=>
        console.log(`Escutando a serial porta: ${portSerialName}`)
  );
})

// realiza as leituras da portaserial do arduino
portSerial.on('data', (data)=>{
    //var nowTime = new Date();
    let a = data.toString();
    let b = JSON.stringify(a);

    console.log(`${a}`);

});

 

 

Edited by Priscilla Fabricio Ramos
Link to comment
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152.2k
    • Total Posts
      651.8k
×
×
  • Create New...