简体   繁体   English

将字节数组或字符串值转换为 P5.js 中的浮点数(基于 JavaScript)

[英]Converting byte array or String value to float in P5.js (javascript based)

I have been successful in getting serial data in P5.js sketch using this - GitHub - p5-serial/p5.serialport: Serial Port API and Server for p5.js library as either byte array or string values.我已经成功地使用这个在 P5.js 草图中获取串行数据- GitHub - p5-serial/p5.serialport: Serial Port API and Server for p5.js library 作为字节数组或字符串值。

When I tried converting string to float type using float() – I get a lot of NaN values which shouldn't be coming since data is numerical.当我尝试使用 float() 将字符串转换为浮点类型时——我得到了很多不应该出现的 NaN 值,因为数据是数字的。

Alternatively, I then tried getting data from serial port as byte array.或者,然后我尝试从串行端口获取数据作为字节数组。 I can't find a way to convert this array to my original float value in P5.js.我找不到将这个数组转换为我在 P5.js 中的原始浮点值的方法。

Any ideas on how to convert byte array to float or avoid getting NaN when converting string to float?关于如何将字节数组转换为浮点数或避免在将字符串转换为浮点数时获得 NaN 的任何想法?

Appreciate your time and help.感谢您的时间和帮助。 Thanks.谢谢。

Codes I have tried for conversion:我尝试转换的代码:

  1. From String to float, I used从String到float,我用过

    let incomingData; let dataValue; incomingData = serial.readStringUntil('\n'); dataValue = float(incomingData); //standard method in P5.js -- returns NaN values

I also tried parseFloat() -- https://www.geeksforgeeks.org/javascript-parsefloat-function/我也试过 parseFloat() -- https://www.geeksforgeeks.org/javascript-parsefloat-function/

  dataValue = parseFloat(incomingData);
  //again returns maximum values as NaN
  1. From byte array to float从字节数组到浮点数

    let incomingBytes = []; let dataValue; incomingBytes = serial.readBytesUntil('\n'); dataValue = dataView.getFloat64(bytebuffer); // This javascript method is not supported by P5.js it seems

arduino中的原始数据 P5.js 中的字符串数据 转换为浮点数后的 NaN 转换值数组 - 很多 NaN 未经转换的字符串数据数组

As seen in the last image, there were empty strings or null values coming.如上图所示,出现了空字符串或 null 值。 I just had to put a check for it before doing the conversion.在进行转换之前,我只需要对其进行检查。

Thanks to all the commenters.感谢所有评论者。

 let incomingData;                    //incoming serial value is stored in this variable

 let temparray = [1000];              //temporary array used in updating final array

 let plot_from_here = [1000];         //final array of values to plot
 function setup(){}
 function draw(){}

 function serialEvent() {

  if(serial.available()>0){

   incomingData = serial.readStringUntil('\n');    //read string until next line

    if(incomingData != ""){

     incomingData = trim(incomingData);            //trim white spaces

     arrayCopy(temparray,0, temparray,1,999);      //Copy value from index 0 to index 1

     temparray[0]= float(incomingData);            //Update index 0 of temparray for the incoming value -- the value is converted to float from string type

     plot_from_here = temparray;                   //update values in final array

    print(plot_from_here);

     }
   }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM