ofSerial reading more than 3 digits

Hello, I have problem getting values from arduino to OF.

I’m using ToF Sensor to get the distance value to OF. but I only get 3digit numbers and the values seems to be a bit different compared to values from arduino IDE serial monitor.

my values from Tof Sensor in Arduino IDE has 4digit numbers, 0 to about 4000.

I’ve tried readBytes() from ofSerial documentation but I cant get the values… keeps giving me values of 1 or random number continuously.

I checked the hardware part to see if any wires were unplugged etc. keep getting good values on arduino IDE but not on OF.

please help…

#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){
	ofBackground(255);

	serial.listDevices();
	vector<ofSerialDeviceInfo> deviceList = serial.getDeviceList();
	int baud = 9600;
	serial.setup("COM4", baud);
	ofSetColor(0);
}

//--------------------------------------------------------------
void ofApp::update() {
	

	
	if (serial.available() < 0) {
		byteData = 0;
	}
	else {
		while (serial.available() > 0) {
			
			byteData = serial.readByte();
			
			
		}
	}

	cout << byteData << endl;

	
}

one thing to note is that if you are sending ascii text (vs binary data), a character of the number will look different as a byte

for example, ‘0’ is 48, ‘1’ is 49 etc. if it’s the case that you are sending ascii, it may help you to have a separating character (such as newline) in terms of splitting up what you send, then you will need to construct a string out of the characters and convert it to a number (using ofToInt(…) for example)

(alternatively, you may want to check your baud rate and other serial settings, which can be a common cause of weird data…)

Thank you for replying zach!

so you mean I’m receiving a ascii text of my distance value?

how can I construct a string out of the ascii text(byte) into original integer data?

sorry for keep asking I’m currently learning OF

the arduino serial monitor has mm(millimeter) value which is like below

and the OF

it seems to have a same value, I’ve checked by moving my hand.

but the value crashs after 999 (which is the begin of 4 digits), I think it just prints out first 2digit only if the value is 4 digits long.

both baud rate is set to 115200

my code of arduino

/*
This example takes range measurements with the VL53L1X and displays additional 
details (status and signal/ambient rates) for each measurement, which can help
you determine whether the sensor is operating normally and the reported range is
valid. The range is in units of mm, and the rates are in units of MCPS (mega 
counts per second).
*/

#include <Wire.h>
#include <VL53L1X.h>

VL53L1X sensor;
int data = 0;

void setup()
{
  Serial.begin(115200);
  Wire.begin();
  Wire.setClock(400000); // use 400 kHz I2C

  sensor.setTimeout(500);
  if (!sensor.init())
  {
    Serial.println("Failed to detect and initialize sensor!");
    while (1);
  }
  
  // Use long distance mode and allow up to 50000 us (50 ms) for a measurement.
  // You can change these settings to adjust the performance of the sensor, but
  // the minimum timing budget is 20 ms for short distance mode and 33 ms for
  // medium and long distance modes. See the VL53L1X datasheet for more
  // information on range and timing limits.
  sensor.setDistanceMode(VL53L1X::Long);
  sensor.setMeasurementTimingBudget(50000);

  // Start continuous readings at a rate of one measurement every 50 ms (the
  // inter-measurement period). This period should be at least as long as the
  // timing budget.
  sensor.startContinuous(50);
}

void loop()
{
  sensor.read();
  data = sensor.ranging_data.range_mm;
  Serial.write(data);
  
//  Serial.print("range: ");
//  Serial.print(sensor.ranging_data.range_mm);
//  Serial.print("\tstatus: ");
//  Serial.print(VL53L1X::rangeStatusToString(sensor.ranging_data.range_status));
//  Serial.print("\tpeak signal: ");
//  Serial.print(sensor.ranging_data.peak_signal_count_rate_MCPS);
//  Serial.print("\tambient: ");
//  Serial.print(sensor.ranging_data.ambient_count_rate_MCPS);
//  
//  Serial.println();
}

I am a big fan of @bakercp 's ofxSerial addon. It has some simple examples for reading all bytes.

3 Likes

thank you I actually found my problem by modifying the SerialExamples but this addon could help me too!