Read feedback data from gauge

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


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:

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

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.

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

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

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?

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.

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

1 Like

Hello @Ardan,

Consider this example:

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.

References:

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

:)

1 Like