# Read feedback data from gauge

**URL:** <https://discourse.processing.org/t/read-feedback-data-from-gauge/36879>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [May 14, 2022, 6:21am UTC](https://discourse.processing.org/t/read-feedback-data-from-gauge/36879 "2022-05-14T06:21:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ardan](https://avatars.discourse-cdn.com/v4/letter/a/8edcca/32.png) [@Ardan](https://discourse.processing.org/u/Ardan)\
**Post date:** [May 14, 2022, 6:21am UTC](https://discourse.processing.org/t/read-feedback-data-from-gauge/36879/1 "2022-05-14T06:21:33Z")

</div>

Hi guys, I want to use processing to read from my gauge, here is my code:

```auto

import processing.serial.*;

////varibles setting
Serial myPort;
String inString;
Table table;
String Force;
String trigger;

void setup() 
{
    ////setup for serial
    printArray(Serial.list());
    myPort = new Serial(this, "COM3", 115200);
    delay(1000);
    
    trigger="?\r";
  } 

////////read buffer
void serialEvent(Serial myPort) {
    inString = myPort.readString();
}

// function 
void draw() {
    //print(trigger);
    myPort.write(trigger);
    Force=inString;
    print(Force);
} // func 

```

when I send “?\r” to my gauge, it is supposed to show its value like this:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/6/0/60e9f80c21fec3bf132a6a9960594348df2558d3.jpeg)

However, in processing, it returns null value like this, do you know why?

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/5/c/5c30059023d50d46c2890c8f7aa2cf450671ebea.jpeg)

---

<div class="post-metadata">

**Author:** ![SomeOne](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/someone/32/8639_2.png) [@SomeOne](https://discourse.processing.org/u/SomeOne)\
**Post date:** [May 14, 2022, 7:20am UTC](https://discourse.processing.org/t/read-feedback-data-from-gauge/36879/2 "2022-05-14T07:20:53Z")

</div>

I think you use inString in draw() cycle before a value has been set to it in serialEvent(). Why don’t you put print statement in serial event? Then it would be certain that inString has a value and doesn’t return null.

```auto
void serialEvent(Serial myPort) {
    inString = myPort.readString();
    print(inString);
}

```

I cannot find any serial devices, so I can’t verify it on my computer.

---

<div class="post-metadata">

**Author:** ![Ardan](https://avatars.discourse-cdn.com/v4/letter/a/8edcca/32.png) [@Ardan](https://discourse.processing.org/u/Ardan)\
**Post date:** [May 14, 2022, 7:47am UTC](https://discourse.processing.org/t/read-feedback-data-from-gauge/36879/3 "2022-05-14T07:47:58Z")

</div>

You are right! I never thought of that because in my other project I print inString in draw() but it seems run well…is it relative to sampling rate?

---

<div class="post-metadata">

**Author:** ![SomeOne](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/someone/32/8639_2.png) [@SomeOne](https://discourse.processing.org/u/SomeOne)\
**Post date:** [May 14, 2022, 7:57am UTC](https://discourse.processing.org/t/read-feedback-data-from-gauge/36879/4 "2022-05-14T07:57:39Z")

</div>

I think sampling rate sets how often computer checks the port. With input the serialEvent() called. I don’t know if it would be called 115200 times a second. Instead input could be buffered to a larger bunch to reduce call ups.

---

<div class="post-metadata">

**Author:** ![Ardan](https://avatars.discourse-cdn.com/v4/letter/a/8edcca/32.png) [@Ardan](https://discourse.processing.org/u/Ardan)\
**Post date:** [May 14, 2022, 8:33am UTC](https://discourse.processing.org/t/read-feedback-data-from-gauge/36879/5 "2022-05-14T08:33:43Z")

</div>

In my opinion , 115200 means the maximum bytes could the gauge exchange with computer in 1 sec, which should be called baud rate. Baud rate , the sampling rate of the gauge and the processing speed of program effect the readings together.I guess in my code, if I separate readString() and print(), it may miss the value. I removed serialEvent () part then move the code into draw(), now it works well. The value of gauge can be read in a high speed and recorded in a .csv to analysis.  
Thanks for your advice! It does help me a lot

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [May 15, 2022, 12:40am UTC](https://discourse.processing.org/t/read-feedback-data-from-gauge/36879/6 "2022-05-15T00:40:53Z")

</div>

Hello @Ardan,

Consider this example:

```auto
import processing.serial.*;

Serial myPort;
String inString = "no inString yet";
String Force = "no Force yet";
int trigger;
boolean data;

void setup() 
  {
  printArray(Serial.list());
  myPort = new Serial(this, "COM5", 115200);
  delay(1000);
  
  // Comment this line to see what happens!
  myPort.bufferUntil('N'); // See reference
  
  trigger = '?'; // This will be cast to an int
 
 frameRate(60); // This is the default.
  
  delay(1000);
  } 

void draw() 
  {
  background(255);
  
  if (data)
    {
    Force = inString;
    println(Force); // Only prints valid data
    data = false;
    }
  
  // Request data every 30 frames
  if (frameCount%30 == 0)
    {
    myPort.write(trigger);    
    }
    
  fill(0);  
  text(Force, 20, 20); // Displays each frame
  }

void serialEvent(Serial myPort) 
  {
  //inString = myPort.readStringUntil('N'); //Aleady buffered!
  inString = myPort.readString();
  if (inString != null) // It should not be null here. Can add other checks too!
    {
    println("Event: ", inString);
    data = true;
    }
  }  

```

The important thing when using serialEvent() is to buffer until a character… I chose ‘N’ since this is what I saw in the picture.  
There may be other characters (whitespace such as /r and /n) but I can’t see them in your picture.  
Do you get a line feed when you don’t use the timestamp?

I wrote some Arduino code to simulate your gauge.

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/d/e/de68fe724315e01f4cc39bf24d3d8dda01c62129.png)

References:

> **[Serial / Libraries](https://processing.org/reference/libraries/serial/index.html)**
>
> Send data between Processing and external hardware through serial communication (RS-232).

Serial communications can be a challenge at first.  
Keep at it!

`:)`
